Interactive Device Design / Adafruit_LEDBackpack_2

Dependents:   toastboard_integrated

Fork of Adafruit_LEDBackpack_2 by Daniel Drew

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  /*
00026  * Modified by Daniel Drew 11/13/2014 for use with 24ledbar backpack
00027  */
00028 
00029 #include "mbed.h"
00030 #include "Adafruit_LEDBackpack.h"
00031 #include "Adafruit_GFX.h"
00032 
00033 #ifndef _BV
00034   #define _BV(bit) (1<<(bit))
00035 #endif
00036 
00037 void Adafruit_LEDBackpack::setBrightness(uint8_t b) {
00038   if (b > 15) b = 15;
00039   uint8_t c = 0xE0 | b;
00040   char foo[1];
00041   foo[0] = c;
00042   _i2c->write(i2c_addr, foo, 1); 
00043 }
00044 
00045 void Adafruit_LEDBackpack::blinkRate(uint8_t b) {
00046   if (b > 3) b = 0; // turn off if not sure
00047   uint8_t c = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
00048   char foo[1];
00049   foo[0] = c;
00050   _i2c->write(i2c_addr, foo, 1);
00051 }
00052 
00053 Adafruit_LEDBackpack::Adafruit_LEDBackpack(I2C *i2c): _i2c(i2c) {
00054 }
00055 
00056 void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70) {
00057   i2c_addr = _addr << 1;
00058 
00059   char foo[1];
00060   foo[0] = 0x21;
00061 
00062   _i2c->write(i2c_addr, foo, 1);  // turn on oscillator
00063 
00064   blinkRate(HT16K33_BLINK_OFF);
00065   
00066   setBrightness(15); // max brightness
00067 }
00068 
00069 void Adafruit_LEDBackpack::writeDisplay(void) {
00070   char foo[17];
00071   foo[0] = 0x00;
00072   int j = 0;
00073   for (uint8_t i=1; i<=16; i+=2) {
00074     int x = displaybuffer[j] & 0xFF;
00075     foo[i] = x;
00076     int x2 = displaybuffer[j] >> 8;
00077     foo[i+1] = x2;
00078     j++;
00079   }
00080   _i2c->write(i2c_addr, foo, 17);
00081 }
00082 
00083 void Adafruit_LEDBackpack::clear(void) {
00084   for (uint8_t i=0; i<8; i++) {
00085     displaybuffer[i] = 0;
00086   }
00087 }
00088 
00089 Adafruit_8x8matrix::Adafruit_8x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(8, 8) {
00090 }
00091 
00092 void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
00093   if ((y < 0) || (y >= 8)) return;
00094   if ((x < 0) || (x >= 8)) return;
00095 
00096  // check rotation, move pixel around if necessary
00097   switch (getRotation()) {
00098   case 1:
00099     swap(x, y);
00100     x = 8 - x - 1;
00101     break;
00102   case 2:
00103     x = 8 - x - 1;
00104     y = 8 - y - 1;
00105     break;
00106   case 3:
00107     swap(x, y);
00108     y = 8 - y - 1;
00109     break;
00110   }
00111 
00112   // wrap around the x
00113   x += 7;
00114   x %= 8;
00115 
00116 
00117   if (color) {
00118     displaybuffer[y] |= 1 << x;
00119   } else {
00120     displaybuffer[y] &= ~(1 << x);
00121   }
00122 }
00123 
00124 
00125 /******************************* 24 BARGRAPH OBJECT */
00126 
00127 Adafruit_24bargraph::Adafruit_24bargraph(I2C *i2c): Adafruit_LEDBackpack(i2c) {
00128 }
00129 
00130 void Adafruit_24bargraph::setBar(uint8_t bar, uint8_t color) {
00131   uint16_t a, c;
00132  
00133   if (bar < 12)
00134     c = bar / 4;
00135   else 
00136     c = (bar - 12) / 4;
00137 
00138   a = bar % 4;
00139   if (bar >= 12)
00140     a += 4;
00141     
00142   //Serial.print("Ano = "); Serial.print(a); Serial.print(" Cath = "); Serial.println(c);
00143   if (color == LED_RED) {
00144     // Turn on red LED.
00145     displaybuffer[c] |= _BV(a);
00146     // Turn off green LED.
00147     displaybuffer[c] &= ~_BV(a+8);
00148   } else if (color == LED_YELLOW) {
00149     // Turn on red and green LED.
00150     displaybuffer[c] |= _BV(a) | _BV(a+8);
00151   } else if (color == LED_OFF) {
00152     // Turn off red and green LED.
00153     displaybuffer[c] &= ~_BV(a) & ~_BV(a+8);
00154   } else if (color == LED_GREEN) {
00155     // Turn on green LED.
00156     displaybuffer[c] |= _BV(a+8);
00157     // Turn off red LED.
00158     displaybuffer[c] &= ~_BV(a);
00159   } 
00160 }