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

/media/uploads/maclobdell/austin_iot_lab.jpg

Committer:
maclobdell
Date:
Mon Dec 11 19:22:54 2017 +0000
Revision:
2:cdcd2d7d83c3
Parent:
1:ed6764fbda54
added playText function which parses a sentence and either shows the individual words (for short words under 5 chars), or scrolls them for words equal to or greater than 5 chars.  also updated mbed-os library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:acc3c726ffe3 1 #include "mbed.h"
maclobdell 0:acc3c726ffe3 2
maclobdell 0:acc3c726ffe3 3 #include "Adafruit_32x8matrix.h"
maclobdell 0:acc3c726ffe3 4
maclobdell 0:acc3c726ffe3 5 Adafruit_32x8matrix::Adafruit_32x8matrix(I2C *i2c, uint8_t i2c_addr, uint8_t i2c_addr2, uint8_t rotation, uint8_t rotation2, uint8_t brightness)
maclobdell 0:acc3c726ffe3 6
maclobdell 1:ed6764fbda54 7 : _i2c(i2c), _matrix(_i2c), _matrix2(_i2c), _i2c_addr(i2c_addr), _i2c_addr2(i2c_addr2), _rotation(rotation), _rotation2(rotation2), _brightness(brightness)
maclobdell 0:acc3c726ffe3 8
maclobdell 0:acc3c726ffe3 9 {
maclobdell 0:acc3c726ffe3 10
maclobdell 0:acc3c726ffe3 11 _matrix2.begin(_i2c_addr2);
maclobdell 0:acc3c726ffe3 12 _matrix2.setBrightness(_brightness);
maclobdell 0:acc3c726ffe3 13 _matrix2.setRotation(_rotation2);
maclobdell 0:acc3c726ffe3 14 _matrix2.clear();
maclobdell 0:acc3c726ffe3 15 _matrix2.writeDisplay();
maclobdell 0:acc3c726ffe3 16
maclobdell 0:acc3c726ffe3 17 _matrix.begin(_i2c_addr);
maclobdell 0:acc3c726ffe3 18 _matrix.setBrightness(_brightness);
maclobdell 0:acc3c726ffe3 19 _matrix.setRotation(_rotation);
maclobdell 0:acc3c726ffe3 20 _matrix.clear();
maclobdell 0:acc3c726ffe3 21 _matrix.writeDisplay();
maclobdell 0:acc3c726ffe3 22
maclobdell 0:acc3c726ffe3 23
maclobdell 0:acc3c726ffe3 24 }
maclobdell 0:acc3c726ffe3 25
maclobdell 0:acc3c726ffe3 26 void Adafruit_32x8matrix::scrollText(char * buffer, uint8_t buf_len, uint8_t speed)
maclobdell 0:acc3c726ffe3 27 {
maclobdell 0:acc3c726ffe3 28
maclobdell 0:acc3c726ffe3 29 // code inspired by LOLShield library
maclobdell 0:acc3c726ffe3 30 int xoff=31;// set offset to the right end of the screen - must be signed
maclobdell 0:acc3c726ffe3 31 for(int i=0; i< (31 + buf_len*6 +10); i++){ //scrolling loop
maclobdell 0:acc3c726ffe3 32 for(int j=0; j<buf_len; j++){ //loop over all of the chars in the text
maclobdell 0:acc3c726ffe3 33 if(xoff > 15){
maclobdell 0:acc3c726ffe3 34 _matrix2.drawChar(xoff + j*6 - 16, 0, buffer[j], 1, 0, 1);
maclobdell 0:acc3c726ffe3 35 }else
maclobdell 0:acc3c726ffe3 36 {
maclobdell 0:acc3c726ffe3 37 _matrix.drawChar(xoff + j*6, 0, buffer[j], 1, 0, 1);
maclobdell 0:acc3c726ffe3 38 _matrix2.drawChar(xoff + j*6 - 16, 0, buffer[j], 1, 0, 1);
maclobdell 0:acc3c726ffe3 39 }
maclobdell 0:acc3c726ffe3 40 }
maclobdell 0:acc3c726ffe3 41 xoff--; // decrement x offset
maclobdell 0:acc3c726ffe3 42
maclobdell 0:acc3c726ffe3 43 _matrix.writeDisplay();
maclobdell 0:acc3c726ffe3 44 _matrix2.writeDisplay();
maclobdell 0:acc3c726ffe3 45 Thread::wait(1000/speed);
maclobdell 0:acc3c726ffe3 46 _matrix.clear();
maclobdell 0:acc3c726ffe3 47 _matrix2.clear();
maclobdell 0:acc3c726ffe3 48 }
maclobdell 0:acc3c726ffe3 49 }
maclobdell 0:acc3c726ffe3 50
maclobdell 0:acc3c726ffe3 51
maclobdell 0:acc3c726ffe3 52 void Adafruit_32x8matrix::showText(char * buffer, uint8_t buf_len, uint8_t speed)
maclobdell 0:acc3c726ffe3 53 {
maclobdell 0:acc3c726ffe3 54 for(int j=0; j<buf_len; j++){ //loop over all of the chars in the text
maclobdell 0:acc3c726ffe3 55 _matrix.drawChar(j*6, 0, buffer[j], 1, 0, 1);
maclobdell 0:acc3c726ffe3 56 _matrix2.drawChar(j*6 - 16, 0, buffer[j], 1, 0, 1);
maclobdell 0:acc3c726ffe3 57 }
maclobdell 0:acc3c726ffe3 58 _matrix.writeDisplay();
maclobdell 0:acc3c726ffe3 59 _matrix2.writeDisplay();
maclobdell 0:acc3c726ffe3 60 Thread::wait(1000/speed);
maclobdell 0:acc3c726ffe3 61 _matrix.clear();
maclobdell 0:acc3c726ffe3 62 _matrix2.clear();
maclobdell 0:acc3c726ffe3 63
maclobdell 0:acc3c726ffe3 64 }
maclobdell 2:cdcd2d7d83c3 65
maclobdell 2:cdcd2d7d83c3 66 void Adafruit_32x8matrix::playText(char * buffer, uint8_t buf_len, uint8_t speed)
maclobdell 2:cdcd2d7d83c3 67 {
maclobdell 2:cdcd2d7d83c3 68 char words[16][16]; //16 words max, 16 char max in each word
maclobdell 2:cdcd2d7d83c3 69 int word_count = 0;
maclobdell 2:cdcd2d7d83c3 70
maclobdell 2:cdcd2d7d83c3 71 for(int k=0; k<16; k++){ //for each word, up to 16
maclobdell 2:cdcd2d7d83c3 72 for(int j=0; j<16; j++){
maclobdell 2:cdcd2d7d83c3 73 words[k][j] = '\0'; //clear all chars
maclobdell 2:cdcd2d7d83c3 74 }
maclobdell 2:cdcd2d7d83c3 75 }
maclobdell 2:cdcd2d7d83c3 76
maclobdell 2:cdcd2d7d83c3 77 int k = 0; //word index
maclobdell 2:cdcd2d7d83c3 78 int m = 0; //char index in each word
maclobdell 2:cdcd2d7d83c3 79 for(int j=0; j<buf_len; j++){//loop over all of the chars in the text
maclobdell 2:cdcd2d7d83c3 80 if (buffer[j] == ' '){//if space, stop building the word up
maclobdell 2:cdcd2d7d83c3 81 //not required - words[k][m+1] = '\0'; //terminate the string
maclobdell 2:cdcd2d7d83c3 82 //printf("found word: %s\r\n",words[k]);
maclobdell 2:cdcd2d7d83c3 83 k++; //go to next word
maclobdell 2:cdcd2d7d83c3 84 m = 0;
maclobdell 2:cdcd2d7d83c3 85 }
maclobdell 2:cdcd2d7d83c3 86 else{
maclobdell 2:cdcd2d7d83c3 87 if(m < 16) { //limit chars to 16
maclobdell 2:cdcd2d7d83c3 88 words[k][m] = buffer[j]; //build up the string of chars to form words
maclobdell 2:cdcd2d7d83c3 89 m++; //got to next char
maclobdell 2:cdcd2d7d83c3 90 }
maclobdell 2:cdcd2d7d83c3 91 }
maclobdell 2:cdcd2d7d83c3 92 if(k >= 16){ //reached max words
maclobdell 2:cdcd2d7d83c3 93 break;
maclobdell 2:cdcd2d7d83c3 94 }
maclobdell 2:cdcd2d7d83c3 95
maclobdell 2:cdcd2d7d83c3 96 }
maclobdell 2:cdcd2d7d83c3 97 //last word may not have a space at the end that identifies it, so add one to the count
maclobdell 2:cdcd2d7d83c3 98 if(k < 16)
maclobdell 2:cdcd2d7d83c3 99 {
maclobdell 2:cdcd2d7d83c3 100 k++;
maclobdell 2:cdcd2d7d83c3 101 }
maclobdell 2:cdcd2d7d83c3 102
maclobdell 2:cdcd2d7d83c3 103 //k is the word count now
maclobdell 2:cdcd2d7d83c3 104 word_count = k;
maclobdell 2:cdcd2d7d83c3 105 // printf("words found = %d\r\n",word_count);
maclobdell 2:cdcd2d7d83c3 106 /* for(int n=0; n<k; n++){
maclobdell 2:cdcd2d7d83c3 107 printf("%s\r\n",words[n]);
maclobdell 2:cdcd2d7d83c3 108 }
maclobdell 2:cdcd2d7d83c3 109 */
maclobdell 2:cdcd2d7d83c3 110
maclobdell 2:cdcd2d7d83c3 111 //for each word in the string, show it
maclobdell 2:cdcd2d7d83c3 112 for(int p=0; p<word_count; p++){
maclobdell 2:cdcd2d7d83c3 113 if (strlen(words[p]) > 5) //can fit about 5 chars on the screen at once, if greater use scroll.
maclobdell 2:cdcd2d7d83c3 114 {
maclobdell 2:cdcd2d7d83c3 115 this->scrollText(words[p],strlen(words[p]), 100);
maclobdell 2:cdcd2d7d83c3 116 }
maclobdell 2:cdcd2d7d83c3 117 else{
maclobdell 2:cdcd2d7d83c3 118 this->showText(words[p],strlen(words[p]), 1);
maclobdell 2:cdcd2d7d83c3 119 }
maclobdell 2:cdcd2d7d83c3 120 }
maclobdell 2:cdcd2d7d83c3 121
maclobdell 2:cdcd2d7d83c3 122
maclobdell 2:cdcd2d7d83c3 123 }