Weiyu Liu / Adafruit_Trellis

Dependents:   Adafruit_Trellis_Hello_World

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_Trellis.cpp Source File

Adafruit_Trellis.cpp

00001 #include "mbed.h"
00002 #include "Adafruit_Trellis.h"
00003 
00004 #define HT16K33_BLINK_CMD       0x80
00005 #define HT16K33_BLINK_DISPLAYON 0x01
00006 #define HT16K33_CMD_BRIGHTNESS  0xE0
00007 
00008 /*
00009  These are the lookup tables that convert the LED/button #
00010  to the memory address in the HT16K33 - don't mess with them :)
00011  */
00012 
00013 static const uint8_t ledLUT[16] =
00014 { 0x3A, 0x37, 0x35, 0x34,
00015     0x28, 0x29, 0x23, 0x24,
00016     0x16, 0x1B, 0x11, 0x10,
00017     0x0E, 0x0D, 0x0C, 0x02 },
00018 buttonLUT[16] =
00019 { 0x07, 0x04, 0x02, 0x22,
00020     0x05, 0x06, 0x00, 0x01,
00021     0x03, 0x10, 0x30, 0x21,
00022     0x13, 0x12, 0x11, 0x31 };
00023 
00024 Adafruit_Trellis::Adafruit_Trellis(void) {
00025 }
00026 
00027 void Adafruit_Trellis::begin(I2C *_wire, uint8_t _addr = 0x70) {
00028     // The mbed API uses 8 bit addresses, so make sure to take
00029     // that 7 bit address and left shift it by 1 before passing it.
00030     i2c_addr = (_addr << 1);
00031     // store pointer to the I2C class object
00032     wire = _wire;
00033     
00034     // turn on oscillator
00035     cmd[0] = 0x21;
00036     wire->write(i2c_addr, cmd, 1);
00037     // set blink rate
00038     blinkRate(HT16K33_BLINK_OFF);
00039     // set brightness
00040     setBrightness(15); // max brightness
00041     // setup interrupt, active low
00042     cmd[0] = 0xA1;
00043     wire->write(i2c_addr, cmd, 1);
00044 }
00045 
00046 /*
00047  Helper button functions, the data is updated every readSwitches() call!
00048  */
00049 
00050 bool Adafruit_Trellis::isKeyPressed(uint8_t k) {
00051     if (k > 15) return false;
00052     k = buttonLUT[k];
00053     return (keys[k>>4] & _BV(k & 0x0F));
00054 }
00055 bool Adafruit_Trellis::wasKeyPressed(uint8_t k) {
00056     if (k > 15) return false;
00057     k = buttonLUT[k];
00058     return (lastkeys[k>>4] & _BV(k & 0x0F));
00059 }
00060 
00061 bool Adafruit_Trellis::justPressed(uint8_t k) {
00062     return (isKeyPressed(k) & !wasKeyPressed(k));
00063 }
00064 bool Adafruit_Trellis::justReleased(uint8_t k) {
00065     return (!isKeyPressed(k) & wasKeyPressed(k));
00066 }
00067 
00068 /*
00069  Helper LED functions, the data is written on writeDisplay()
00070  */
00071 
00072 
00073 bool Adafruit_Trellis::isLED(uint8_t x) {
00074     if (x > 15) return false;
00075     x = ledLUT[x];
00076     return ((displaybuffer[x >> 4] & _BV(x & 0x0F)) > 0);
00077 }
00078 void Adafruit_Trellis::setLED(uint8_t x) {
00079     if (x > 15) return;
00080     x = ledLUT[x];
00081     displaybuffer[x >> 4] |= _BV(x & 0x0F);
00082 }
00083 void Adafruit_Trellis::clrLED(uint8_t x) {
00084     if (x > 15) return;
00085     x = ledLUT[x];
00086     displaybuffer[x >> 4] &= ~_BV(x & 0x0F);
00087 }
00088 
00089 
00090 /*
00091  Gets the switch memory data and updates the last/current read
00092  */
00093 
00094 bool Adafruit_Trellis::readSwitches(void) {
00095     memcpy(lastkeys, keys, sizeof(keys));
00096     
00097     cmd[0] = 0x40;
00098     wire->write(i2c_addr, cmd, 1);
00099     wire->read(i2c_addr, cmd, 6);
00100     
00101     for (uint8_t i=0; i<6; i++)
00102     keys[i] = cmd[i];
00103     
00104     for (uint8_t i=0; i<6; i++) {
00105         if (lastkeys[i] != keys[i]) {
00106             for (uint8_t j=0; j<6; j++) {
00107                 //Serial.print(keys[j], HEX); Serial.print(" ");
00108             }
00109             //Serial.println();
00110             return true;
00111         }
00112     }
00113     return false;
00114 }
00115 
00116 void Adafruit_Trellis::setBrightness(uint8_t b) {
00117     if (b > 15) b = 15;
00118     cmd[0] = HT16K33_CMD_BRIGHTNESS | b;
00119     wire->write(i2c_addr, cmd, 1);
00120 }
00121 
00122 void Adafruit_Trellis::blinkRate(uint8_t b) {
00123     if (b > 3) b = 0; // turn off if not sure
00124     cmd[0] = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
00125     wire->write(i2c_addr, cmd, 1);
00126 }
00127 
00128 
00129 void Adafruit_Trellis::writeDisplay(void) {
00130     cmd[0] = 0x00;
00131     for (uint8_t i=0; i<8; i++) {
00132         cmd[2*i+1] = displaybuffer[i] & 0xFF;
00133         cmd[2*i+2] = displaybuffer[i] >> 8;
00134     }
00135     wire->write(i2c_addr, cmd, 17);
00136     
00137 }
00138 
00139 void Adafruit_Trellis::clear(void) {
00140     memset(displaybuffer, 0, sizeof(displaybuffer));
00141 }
00142 
00143