diff --git a/LedMatrix.cpp b/LedMatrix.cpp index 560339e..936f3d0 100644 --- a/LedMatrix.cpp +++ b/LedMatrix.cpp @@ -9,6 +9,7 @@ LedMatrix::LedMatrix(byte numberOfDevices, byte slaveSelectPin) { myNumberOfDevices = numberOfDevices; mySlaveSelectPin = slaveSelectPin; cols = new byte[numberOfDevices * 8]; + rotatedCols = new byte[numberOfDevices * 8]; } /** @@ -92,11 +93,13 @@ void LedMatrix::calculateTextAlignmentOffset() { void LedMatrix::clear() { for (byte col = 0; col < myNumberOfDevices * 8; col++) { cols[col] = 0; - } - + } } void LedMatrix::commit() { + if ( rotationIsEnabled ) { + rotateLeft(); + } for (byte col = 0; col < myNumberOfDevices * 8; col++) { sendByte(col / 8, col % 8 + 1, cols[col]); } @@ -163,4 +166,19 @@ void LedMatrix::setColumn(int column, byte value) { void LedMatrix::setPixel(byte x, byte y) { bitWrite(cols[x], y, true); +} + +void LedMatrix::setRotation(bool enabled) { + rotationIsEnabled = enabled; +} + +void LedMatrix::rotateLeft() { + for (byte deviceNum = 0; deviceNum < myNumberOfDevices; deviceNum++) { + for(byte posY = 0; posY < 8; posY++) { + for(byte posX = 0; posX < 8; posX++) { + bitWrite(rotatedCols[8 * (deviceNum) + posY], posX, bitRead(cols[8 * (deviceNum) + 7-posX], posY)); + } + } + } + memcpy(cols, rotatedCols, myNumberOfDevices * 8); } \ No newline at end of file diff --git a/LedMatrix.h b/LedMatrix.h index d622e54..ee5e01d 100644 --- a/LedMatrix.h +++ b/LedMatrix.h @@ -116,9 +116,15 @@ class LedMatrix { * Oscilate the text between the two limits. */ void oscillateText(); + + /** + * Enables 90° rotation for each 8x8 matrix. + */ + void setRotation(bool enabled); private: byte* cols; + byte* rotatedCols; byte spiregister[8]; byte spidata[8]; String myText; @@ -130,6 +136,8 @@ class LedMatrix { byte mySlaveSelectPin = 0; byte myCharWidth = 7; byte myTextAlignment = 1; + bool rotationIsEnabled = false; void calculateTextAlignmentOffset(); + void rotateLeft(); }; \ No newline at end of file