Library to drive a pair of Adafruit 16x8 led matrices (powered by Adafruit LED Backpack) rotated end to end and stuck together to make a larger matrix.

Dependencies:   Adafruit_LEDBackpack

Dependents:   Adafruit_LEDBackpack_32x8_App RubeGoldberg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_32x8matrix.cpp Source File

Adafruit_32x8matrix.cpp

00001 #include "mbed.h"
00002 
00003 #include "Adafruit_32x8matrix.h"
00004 
00005 Adafruit_32x8matrix::Adafruit_32x8matrix(I2C *i2c, uint8_t i2c_addr, uint8_t i2c_addr2, uint8_t rotation, uint8_t rotation2, uint8_t brightness) 
00006 
00007     : _i2c(i2c), _matrix(_i2c), _matrix2(_i2c), _i2c_addr(i2c_addr), _i2c_addr2(i2c_addr2), _rotation(rotation), _rotation2(rotation2), _brightness(brightness)  
00008 
00009 {
00010               
00011     _matrix2.begin(_i2c_addr2);
00012     _matrix2.setBrightness(_brightness);
00013     _matrix2.setRotation(_rotation2);
00014     _matrix2.clear();
00015     _matrix2.writeDisplay();
00016         
00017     _matrix.begin(_i2c_addr);
00018     _matrix.setBrightness(_brightness);
00019     _matrix.setRotation(_rotation);                   
00020     _matrix.clear();
00021     _matrix.writeDisplay();
00022 
00023 
00024 }
00025 
00026 void Adafruit_32x8matrix::scrollText(char * buffer, uint8_t buf_len, uint8_t speed)
00027 {
00028     
00029     // code inspired by LOLShield library 
00030     int xoff=31;// set offset to the right end of the screen - must be signed
00031     for(int i=0; i< (31 + buf_len*6 +10); i++){ //scrolling loop
00032          for(int j=0; j<buf_len; j++){ //loop over all of the chars in the text
00033             if(xoff > 15){
00034                 _matrix2.drawChar(xoff + j*6 - 16, 0, buffer[j], 1, 0, 1);
00035             }else
00036             {
00037                 _matrix.drawChar(xoff + j*6, 0, buffer[j], 1, 0, 1);
00038                 _matrix2.drawChar(xoff + j*6 - 16, 0, buffer[j], 1, 0, 1);
00039             }    
00040         }
00041         xoff--; // decrement x offset
00042                 
00043         _matrix.writeDisplay();
00044         _matrix2.writeDisplay();
00045         Thread::wait(1000/speed);
00046         _matrix.clear();
00047         _matrix2.clear();
00048     }    
00049 }    
00050 
00051 
00052 void Adafruit_32x8matrix::showText(char * buffer, uint8_t buf_len, uint8_t speed)
00053 {
00054      for(int j=0; j<buf_len; j++){ //loop over all of the chars in the text
00055         _matrix.drawChar(j*6, 0, buffer[j], 1, 0, 1);
00056         _matrix2.drawChar(j*6 - 16, 0, buffer[j], 1, 0, 1);
00057     }
00058     _matrix.writeDisplay();
00059     _matrix2.writeDisplay();
00060     Thread::wait(1000/speed);
00061     _matrix.clear();
00062     _matrix2.clear();
00063  
00064 } 
00065 
00066 void Adafruit_32x8matrix::playText(char * buffer, uint8_t buf_len, uint8_t speed)
00067 {
00068      char words[16][16];   //16 words max, 16 char max in each word
00069      int word_count = 0;
00070 
00071      for(int k=0; k<16; k++){  //for each word, up to 16
00072       for(int j=0; j<16; j++){
00073         words[k][j] = '\0';    //clear all chars
00074       }
00075      }
00076 
00077      int k = 0;  //word index
00078      int m = 0;  //char index in each word
00079      for(int j=0; j<buf_len; j++){//loop over all of the chars in the text
00080             if (buffer[j] == ' '){//if space, stop building the word up
00081               //not required - words[k][m+1] = '\0';  //terminate the string
00082               //printf("found word: %s\r\n",words[k]);
00083               k++; //go to next word
00084               m = 0;
00085             }
00086             else{
00087               if(m < 16) {  //limit chars to 16
00088                   words[k][m] = buffer[j];  //build up the string of chars to form words
00089                   m++;  //got to next char
00090               }    
00091             }
00092             if(k >= 16){   //reached max words
00093                break;
00094             }
00095             
00096      }
00097      //last word may not have a space at the end that identifies it, so add one to the count
00098      if(k < 16)
00099      {
00100         k++;
00101      }
00102      
00103      //k is the word count now
00104      word_count = k;
00105     // printf("words found = %d\r\n",word_count);
00106     /* for(int n=0; n<k; n++){
00107          printf("%s\r\n",words[n]);
00108        }
00109     */
00110 
00111      //for each word in the string, show it
00112      for(int p=0; p<word_count; p++){
00113        if (strlen(words[p]) > 5)   //can fit about 5 chars on the screen at once, if greater use scroll.
00114        {
00115          this->scrollText(words[p],strlen(words[p]), 100);
00116        }
00117        else{
00118          this->showText(words[p],strlen(words[p]), 1);
00119        }
00120      }
00121 
00122 
00123 }