diff --git a/LedMatrix.cpp b/LedMatrix.cpp index d811f94..ec1f18c 100644 --- a/LedMatrix.cpp +++ b/LedMatrix.cpp @@ -29,6 +29,7 @@ void LedMatrix::init() { } + void LedMatrix::sendByte (const byte device, const byte reg, const byte data) { int offset=device; int maxbytes=myNumberOfDevices; @@ -50,8 +51,43 @@ void LedMatrix::sendByte (const byte device, const byte reg, const byte data) { digitalWrite (mySlaveSelectPin, HIGH); } // end of sendByte + +void LedMatrix::sendByte (const byte reg, const byte data) { + for(byte device = 0; device < myNumberOfDevices; device++) { + sendByte(device, reg, data); + } +} + +void LedMatrix::setIntensity(const byte intensity) { + sendByte(MAX7219_REG_INTENSITY, intensity); +} + +void LedMatrix::setCharWidth(byte charWidth) { + myCharWidth = charWidth; +} + +void LedMatrix::setTextAlignment(byte textAlignment) { + myTextAlignment = textAlignment; + calculateTextAlignmentOffset(); +} - +void LedMatrix::calculateTextAlignmentOffset() { + switch(myTextAlignment) { + case TEXT_ALIGN_LEFT: + myTextAlignmentOffset = 0; + break; + case TEXT_ALIGN_LEFT_END: + myTextAlignmentOffset = myNumberOfDevices * 8; + break; + case TEXT_ALIGN_RIGHT: + myTextAlignmentOffset = myText.length() * myCharWidth - myNumberOfDevices * 8; + break; + case TEXT_ALIGN_RIGHT_END: + myTextAlignmentOffset = - myText.length() * myCharWidth; + break; + } + +} void LedMatrix::clear() { for (byte col = 0; col < myNumberOfDevices * 8; col++) { @@ -69,6 +105,7 @@ void LedMatrix::commit() { void LedMatrix::setText(String text) { myText = text; myTextOffset = 0; + calculateTextAlignmentOffset(); } void LedMatrix::setNextText(String nextText) { @@ -76,18 +113,19 @@ void LedMatrix::setNextText(String nextText) { } void LedMatrix::scrollTextRight() { - myTextOffset = (myTextOffset + 1) % ((int)myText.length() * 7 - 5); + myTextOffset = (myTextOffset + 1) % ((int)myText.length() * myCharWidth - 5); } void LedMatrix::scrollTextLeft() { - myTextOffset = (myTextOffset - 1) % ((int)myText.length() * 7 + myNumberOfDevices * 8); + myTextOffset = (myTextOffset - 1) % ((int)myText.length() * myCharWidth + myNumberOfDevices * 8); if (myTextOffset == 0 && myNextText != NULL) { myText = myNextText; myNextText = NULL; + calculateTextAlignmentOffset(); } } void LedMatrix::oscillateText() { - int maxColumns = (int)myText.length() * 7; + int maxColumns = (int)myText.length() * myCharWidth; int maxDisplayColumns = myNumberOfDevices * 8; if (maxDisplayColumns > maxColumns) { return; @@ -107,7 +145,7 @@ void LedMatrix::drawText() { for (int i = 0; i < myText.length(); i++) { letter = myText.charAt(i); for (byte col = 0; col < 8; col++) { - position = i * 7 + col + myTextOffset + myNumberOfDevices * 8; + position = i * myCharWidth + col + myTextOffset + myTextAlignmentOffset; if (position >= 0 && position < myNumberOfDevices * 8) { setColumn(position, pgm_read_byte (&cp437_font [letter] [col])); } diff --git a/LedMatrix.h b/LedMatrix.h index 09e6ce2..19b04eb 100644 --- a/LedMatrix.h +++ b/LedMatrix.h @@ -16,6 +16,11 @@ #define MAX7219_REG_SHUTDOWN 0xC #define MAX7219_REG_DISPLAYTEST 0xF +#define TEXT_ALIGN_LEFT 0 // Text is aligned to left side of the display +#define TEXT_ALIGN_LEFT_END 1 // Beginning of text is just outside the right end of the display +#define TEXT_ALIGN_RIGHT 2 // End of text is aligned to the right of the display +#define TEXT_ALIGN_RIGHT_END 3 // End of text is just outside the left side of the display + class LedMatrix { public: @@ -32,11 +37,36 @@ class LedMatrix { */ void init(); + /** + * Sets the intensity on all devices. + * intensity: 0-15 + */ + void setIntensity(byte intensity); + + /** + * Sets the width in pixels for one character. + * Default is 7. + */ + void setCharWidth(byte charWidth); + + /** + * Sets the text alignment. + * Default is TEXT_ALIGN_LEFT_END. + * + */ + void setTextAlignment(byte textAlignment); + /** * Send a byte to a specific device. */ void sendByte (const byte device, const byte reg, const byte data); + + /** + * Send a byte to all devices (convenience method). + */ + void sendByte (const byte reg, const byte data); + /** * Turn on pixel at position (x,y). */ @@ -95,8 +125,14 @@ class LedMatrix { String myText; String myNextText; int myTextOffset = 1; + int myTextAlignmentOffset = 0; int increment = -1; byte myNumberOfDevices = 0; byte mySlaveSelectPin = 0; + byte myCharWidth = 7; + byte myTextAlignment = 1; + + void calculateTextAlignmentOffset(); + };