This is a library for the Adafruit LED Backpacks. This library works with the Adafruit Mini 8x8 and 16x8.

Dependents:   Adafruit_LEDBackpack_16x8_App Adafruit_32x8matrix

Fork of Adafruit_LEDBackpack by Luiz Hespanha

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_LEDBackpack.cpp Source File

Adafruit_LEDBackpack.cpp

00001 /*************************************************** 
00002   This is a library for our I2C LED Backpacks
00003 
00004   Designed specifically to work with the Adafruit LED Matrix backpacks 
00005   ----> http://www.adafruit.com/products/
00006   ----> http://www.adafruit.com/products/
00007 
00008   These displays use I2C to communicate, 2 pins are required to 
00009   interface. There are multiple selectable I2C addresses. For backpacks
00010   with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
00011   with 3 Address Select pins: 0x70 thru 0x77
00012 
00013   Adafruit invests time and resources providing this open source code, 
00014   please support Adafruit and open-source hardware by purchasing 
00015   products from Adafruit!
00016 
00017   Written by Limor Fried/Ladyada for Adafruit Industries.  
00018   BSD license, all text above must be included in any redistribution
00019  ****************************************************/
00020  
00021  /*
00022  *  Modified by Luiz Hespanha (http://www.d3.do) 8/16/2013 for use in LPC1768
00023  */
00024 
00025 #include "mbed.h"
00026 #include "Adafruit_LEDBackpack.h"
00027 #include "Adafruit_GFX.h"
00028 #include "glcdfont.h"
00029 
00030 //mpl - something with stdio screwing things up.  need to define serial port to use for debug
00031 extern Serial pc;
00032 
00033 void Adafruit_LEDBackpack::setBrightness(uint8_t b) {
00034   if (b > 15) b = 15;
00035   uint8_t c = 0xE0 | b;
00036   char foo[1];
00037   foo[0] = c;
00038   _i2c->write(i2c_addr, foo, 1); 
00039 }
00040 
00041 void Adafruit_LEDBackpack::blinkRate(uint8_t b) {
00042   if (b > 3) b = 0; // turn off if not sure
00043   uint8_t c = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
00044   char foo[1];
00045   foo[0] = c;
00046   _i2c->write(i2c_addr, foo, 1);
00047 }
00048 
00049 Adafruit_LEDBackpack::Adafruit_LEDBackpack(I2C *i2c): _i2c(i2c) {
00050 }
00051 
00052 void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70) {
00053   i2c_addr = _addr << 1;
00054 
00055   char foo[1];
00056   foo[0] = 0x21;
00057 
00058   _i2c->write(i2c_addr, foo, 1);  // turn on oscillator
00059 
00060   blinkRate(HT16K33_BLINK_OFF);
00061   
00062   setBrightness(15); // max brightness
00063 }
00064 
00065 void Adafruit_LEDBackpack::writeDisplay(void) {
00066   char foo[17];
00067   foo[0] = 0x00;
00068   int j = 0;
00069   for (uint8_t i=1; i<=16; i+=2) {
00070     int x = displaybuffer[j] & 0xFF;
00071     foo[i] = x;
00072     int x2 = displaybuffer[j] >> 8;
00073     foo[i+1] = x2;
00074     j++;
00075   }
00076   _i2c->write(i2c_addr, foo, 17);
00077 }
00078 
00079 void Adafruit_LEDBackpack::clear(void) {
00080   for (uint8_t i=0; i<16; i++) {
00081     displaybuffer[i] = 0;
00082   }
00083 }
00084 
00085 Adafruit_8x8matrix::Adafruit_8x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(8, 8) {
00086 }
00087 
00088 void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
00089   if ((y < 0) || (y >= 8)) return;
00090   if ((x < 0) || (x >= 8)) return;
00091 
00092  // check rotation, move pixel around if necessary
00093   switch (getRotation()) {
00094   case 1:
00095     swap(x, y);
00096     x = 8 - x - 1;
00097     break;
00098   case 2:
00099     x = 8 - x - 1;
00100     y = 8 - y - 1;
00101     break;
00102   case 3:
00103     swap(x, y);
00104     y = 8 - y - 1;
00105     break;
00106   }
00107 
00108   // wrap around the x
00109   x += 7;
00110   x %= 8;
00111 
00112 
00113   if (color) {
00114     displaybuffer[y] |= 1 << x;
00115   } else {
00116     displaybuffer[y] &= ~(1 << x);
00117   }
00118   
00119 
00120 }
00121 
00122 
00123 Adafruit_16x8matrix::Adafruit_16x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(16, 8) {
00124 }
00125 
00126 void Adafruit_16x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
00127   if ((y < 0) || (y >= 8)) return;
00128   if ((x < 0) || (x >= 16)) return;
00129   
00130  // check rotation, move pixel around if necessary 
00131   switch (getRotation()) {
00132   case 1:
00133     swap(x, y);
00134     x = 16 - x - 1;
00135     break;
00136   case 2:
00137     x = 16 - x - 1;
00138     y = 8 - y - 1;
00139     break;
00140   case 3:
00141     swap(x, y);
00142     y = 8 - y - 1;
00143     break;
00144   }
00145 
00146   // wrap around the x
00147   x += 16;
00148   x %= 16;
00149 
00150 
00151   if (color) {
00152     displaybuffer[y] |= 1 << x;
00153   } else {
00154     displaybuffer[y] &= ~(1 << x);
00155   }
00156 }
00157 
00158 size_t Adafruit_16x8matrix::writeChar(uint8_t c)
00159 {
00160         
00161     if (c == '\n')
00162     {
00163         cursor_y += textsize*8;
00164         cursor_x = 0;
00165     }
00166     else if (c == '\r')
00167         cursor_x = 0;
00168     else
00169     {
00170         drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
00171         cursor_x += textsize*6;
00172        // if (wrap && (cursor_x > (_width - textsize*6)))
00173        // {
00174        //     cursor_y += textsize*8;
00175        //     cursor_x = 0;
00176         //}
00177     }
00178     return 1;
00179 }
00180 
00181 void Adafruit_16x8matrix::scrollChar(uint8_t c)
00182 {
00183         
00184     if (c == '\n')
00185     {
00186         cursor_y += textsize*8;
00187         cursor_x = 0;
00188     }
00189     else if (c == '\r')
00190         cursor_x = 0;
00191     else
00192     {
00193         for(int i = 0; i < 16; i++)
00194         {
00195             drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
00196             writeDisplay();
00197             Thread::wait(100);
00198             clear();
00199             writeDisplay();
00200             cursor_x++;
00201         }    
00202     }
00203 }
00204 
00205 // draw a character
00206 void Adafruit_16x8matrix::drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size)
00207 {
00208   
00209 //allow clipping   
00210 //    if(
00211 //        (x >= _width) || // Clip right
00212 //        (y >= _height) || // Clip bottom
00213 //        ((x + 5 * size - 1) < 0) || // Clip left
00214 //        ((y + 8 * size - 1) < 0) // Clip top
00215 //        )
00216 //        return;
00217     
00218     for (int8_t i=0; i<6; i++ )
00219     {
00220         uint8_t line = 0;
00221 
00222         if (i == 5) 
00223             line = 0x0;
00224         else 
00225             line = font[(c*5)+i];
00226             
00227         for (int8_t j = 0; j<8; j++)
00228         {
00229             if (line & 0x1)
00230             {
00231                 if (size == 1) // default size
00232                     drawPixel(x+i, y+j, color);
00233 #ifdef WANT_ABSTRACTS
00234                 else // big size
00235                     fillRect(x+(i*size), y+(j*size), size, size, color);
00236 #endif                    
00237             }
00238             else if (bg != color)
00239             {
00240                 if (size == 1) // default size
00241                     drawPixel(x+i, y+j, bg);
00242 #ifdef WANT_ABSTRACTS
00243                 else // big size
00244                     fillRect(x+i*size, y+j*size, size, size, bg);
00245 #endif
00246             }
00247             line >>= 1;
00248         }
00249     }
00250 }