Skip to content

Commit

Permalink
Added setters to influence text behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
squix78 committed Apr 26, 2015
1 parent 57409a9 commit eec08ad
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
48 changes: 43 additions & 5 deletions LedMatrix.cpp
Expand Up @@ -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;
Expand All @@ -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++) {
Expand All @@ -69,25 +105,27 @@ void LedMatrix::commit() {
void LedMatrix::setText(String text) {
myText = text;
myTextOffset = 0;
calculateTextAlignmentOffset();
}

void LedMatrix::setNextText(String nextText) {
myNextText = 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;
Expand All @@ -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]));
}
Expand Down
36 changes: 36 additions & 0 deletions LedMatrix.h
Expand Up @@ -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:
Expand All @@ -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).
*/
Expand Down Expand Up @@ -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();


};

0 comments on commit eec08ad

Please sign in to comment.