Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 81 additions & 14 deletions examples/LCDemo7Segment/LCDemo7Segment.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@

/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD
***** Check pin numbers will probably not work with your hardware *****
We have only a single MAX72XX.
*/
LedControl lc=LedControl(12,11,10,1);
#define TRACE_ON

#define LC_DATA_PIN 8
#define LC_LOAD_PIN 7
#define LC_CLK_PIN 6
#define LC_MODULE_COUNT 1

LedControl lc=LedControl(LC_DATA_PIN,LC_CLK_PIN,LC_LOAD_PIN,LC_MODULE_COUNT);
bool rotate=false;

/* we always wait a bit between updates of the display */
unsigned long delaytime=250;
unsigned long delaytime=500;

void setup() {
#ifdef TRACE_ON
char compile_signature[] = "--- START (Build: " __DATE__ " " __TIME__ ") ---";
Serial.begin(9600);
Serial.println(compile_signature);
#endif
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
Expand All @@ -26,30 +36,67 @@ void setup() {
lc.clearDisplay(0);
}

/* Loop over demonstration sequences. Comment unnecessary if u like*/
void loop() {

writeArduinoOn7Segment(); // expects 1 digit
scrollDigits(); // expects 4 digits
writeArduinoString(); // expects 8 digits
writeGoStringOnPositions(); // expects 8 digits
traverseCodepage(); // expects 8 digits
rotate=!rotate;
lc.setRotate180(0,rotate);
}


/*
This method will display the characters for the
word "Arduino" one after the other on digit 0.
*/

void writeArduinoOn7Segment() {
lc.clearDisplay(0);
lc.setChar(0,0,'a',false);
delay(delaytime);
lc.setRow(0,0,0x05);
lc.setChar(0,0,'r',false);
delay(delaytime);
lc.setChar(0,0,'d',false);
delay(delaytime);
lc.setRow(0,0,0x1c);
lc.setChar(0,0,'u',false);
delay(delaytime);
lc.setRow(0,0,B00010000);
lc.setChar(0,0,'i',false);
delay(delaytime);
lc.setRow(0,0,0x15);
lc.setChar(0,0,'n',false);
delay(delaytime);
lc.setRow(0,0,0x1D);
lc.setChar(0,0,'o',false);
delay(delaytime);
lc.clearDisplay(0);
delay(delaytime);
}

/*
This method will display the String
"Arduino" on a 8 digit 7 Seg display
*/
void writeArduinoString() {
lc.setString(0,7,"Arduino",0x02);
delay(delaytime*5);
}

/*
This method will display the String
"Go" at different locations and demonstrate how the other digirs are preseved
*/

void writeGoStringOnPositions() {
lc.setString(0,7,"________",0x55);
for(int i=1;i<8;i++)
{
lc.setString(0,i,"Go",0x80);
delay(delaytime);
}
}

/*
This method will scroll all the hexa-decimal
numbers and letters on the display. You will need at least
Expand All @@ -67,7 +114,27 @@ void scrollDigits() {
delay(delaytime);
}

void loop() {
writeArduinoOn7Segment();
scrollDigits();
/*
This method will travers over the whole codepage in 4 byte steps
showing the characters on the rightside and the offset in decimal on the left
*/

void traverseCodepage()
{
String offsetAsString;
lc.clearDisplay(0);
lc.setString(0,7,"Codepage",0);
delay(delaytime*2);
lc.clearDisplay(0);
for (int i=0;i<128;i+=4) {
offsetAsString=String(i);
lc.setString(0,4+offsetAsString.length(),offsetAsString,0);
lc.setChar(0,3,i,false);
lc.setChar(0,2,i+1,false);
lc.setChar(0,1,i+2,false);
lc.setChar(0,0,i+3,false);
delay(delaytime*4);
}
}


1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ setRow KEYWORD2
setColumn KEYWORD2
setDigit KEYWORD2
setChar KEYWORD2
setString KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
48 changes: 41 additions & 7 deletions src/LedControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#define OP_SHUTDOWN 12
#define OP_DISPLAYTEST 15

LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices) {
LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices, byte rotate180Flags) {
SPI_MOSI=dataPin;
SPI_CLK=clkPin;
SPI_CS=csPin;
Expand All @@ -57,6 +57,7 @@ LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices) {
SPI_MOSI=dataPin;
for(int i=0;i<64;i++)
status[i]=0x00;
rotate180=rotate180Flags;
for(int i=0;i<maxDevices;i++) {
spiTransfer(i,OP_DISPLAYTEST,0);
//scanlimit is set to max on startup
Expand Down Expand Up @@ -162,6 +163,10 @@ void LedControl::setDigit(int addr, int digit, byte value, boolean dp) {
return;
offset=addr*8;
v=pgm_read_byte_near(charTable + value);
if(bitRead(rotate180,addr)) {
digit=7-digit;
v=(B01110000&(v<<3))|(B00001110&(v>>3))|(B00000001&v); //Rotate pabcdefg -> pdefabcg
}
if(dp)
v|=B10000000;
status[offset+digit]=v;
Expand All @@ -183,29 +188,58 @@ void LedControl::setChar(int addr, int digit, char value, boolean dp) {
index=32;
}
v=pgm_read_byte_near(charTable + index);
if(bitRead(rotate180,addr)) {
digit=7-digit;
v=(B01110000&(v<<3))|(B00001110&(v>>3))|(B00000001&v); //Rotate pabcdefg -> pdefabcg
}
if(dp)
v|=B10000000;
status[offset+digit]=v;
spiTransfer(addr, digit+1,v);
}

void LedControl::setString(int addr, int digit, String theString,uint8_t dotpattern)
{

if(addr<0 || addr>=maxDevices)
return;
if(digit<0 || digit>7)
return;
for(int i=0;i<theString.length();i++)
{
setChar(addr,digit,theString.charAt(i),(0x80&dotpattern)!=0);
dotpattern<<=1;
if(--digit<0)
{
digit=7;
if(++addr>=maxDevices) return;
}
}
}

void LedControl::spiTransfer(int addr, volatile byte opcode, volatile byte data) {
//Create an array with the data to shift out

int offset=addr*2;
int maxbytes=maxDevices*2;
int maxbytes=maxDevices*2; // two bytes (opcode+data) for every device

for(int i=0;i<maxbytes;i++)
spidata[i]=(byte)0;
//put our device data into the array
//Prepare array with the data to shift out
memset(spidata,0,maxbytes);

//put our device data into the array (other devices get 0x0000)
spidata[offset+1]=opcode;
spidata[offset]=data;

//enable the line
digitalWrite(SPI_CS,LOW);
//Now shift out the data
//Now shift out the data (Backwards order necessary)
for(int i=maxbytes;i>0;i--)
shiftOut(SPI_MOSI,SPI_CLK,MSBFIRST,spidata[i-1]);
//latch the data onto the display
digitalWrite(SPI_CS,HIGH);
}

void LedControl::setRotate180(int addr, boolean isRotated) {
bitWrite(rotate180,addr,isRotated);
};


Loading