Mbed driver for adafruit trellis.
Dependents: Adafruit_Trellis_Hello_World
Adafruit_Trellis.cpp@0:bbc12ba2cb6c, 2016-03-13 (annotated)
- Committer:
- wliu88
- Date:
- Sun Mar 13 04:59:13 2016 +0000
- Revision:
- 0:bbc12ba2cb6c
Adafruit_Trellis library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wliu88 | 0:bbc12ba2cb6c | 1 | #include "mbed.h" |
wliu88 | 0:bbc12ba2cb6c | 2 | #include "Adafruit_Trellis.h" |
wliu88 | 0:bbc12ba2cb6c | 3 | |
wliu88 | 0:bbc12ba2cb6c | 4 | #define HT16K33_BLINK_CMD 0x80 |
wliu88 | 0:bbc12ba2cb6c | 5 | #define HT16K33_BLINK_DISPLAYON 0x01 |
wliu88 | 0:bbc12ba2cb6c | 6 | #define HT16K33_CMD_BRIGHTNESS 0xE0 |
wliu88 | 0:bbc12ba2cb6c | 7 | |
wliu88 | 0:bbc12ba2cb6c | 8 | /* |
wliu88 | 0:bbc12ba2cb6c | 9 | These are the lookup tables that convert the LED/button # |
wliu88 | 0:bbc12ba2cb6c | 10 | to the memory address in the HT16K33 - don't mess with them :) |
wliu88 | 0:bbc12ba2cb6c | 11 | */ |
wliu88 | 0:bbc12ba2cb6c | 12 | |
wliu88 | 0:bbc12ba2cb6c | 13 | static const uint8_t ledLUT[16] = |
wliu88 | 0:bbc12ba2cb6c | 14 | { 0x3A, 0x37, 0x35, 0x34, |
wliu88 | 0:bbc12ba2cb6c | 15 | 0x28, 0x29, 0x23, 0x24, |
wliu88 | 0:bbc12ba2cb6c | 16 | 0x16, 0x1B, 0x11, 0x10, |
wliu88 | 0:bbc12ba2cb6c | 17 | 0x0E, 0x0D, 0x0C, 0x02 }, |
wliu88 | 0:bbc12ba2cb6c | 18 | buttonLUT[16] = |
wliu88 | 0:bbc12ba2cb6c | 19 | { 0x07, 0x04, 0x02, 0x22, |
wliu88 | 0:bbc12ba2cb6c | 20 | 0x05, 0x06, 0x00, 0x01, |
wliu88 | 0:bbc12ba2cb6c | 21 | 0x03, 0x10, 0x30, 0x21, |
wliu88 | 0:bbc12ba2cb6c | 22 | 0x13, 0x12, 0x11, 0x31 }; |
wliu88 | 0:bbc12ba2cb6c | 23 | |
wliu88 | 0:bbc12ba2cb6c | 24 | Adafruit_Trellis::Adafruit_Trellis(void) { |
wliu88 | 0:bbc12ba2cb6c | 25 | } |
wliu88 | 0:bbc12ba2cb6c | 26 | |
wliu88 | 0:bbc12ba2cb6c | 27 | void Adafruit_Trellis::begin(I2C *_wire, uint8_t _addr = 0x70) { |
wliu88 | 0:bbc12ba2cb6c | 28 | // The mbed API uses 8 bit addresses, so make sure to take |
wliu88 | 0:bbc12ba2cb6c | 29 | // that 7 bit address and left shift it by 1 before passing it. |
wliu88 | 0:bbc12ba2cb6c | 30 | i2c_addr = (_addr << 1); |
wliu88 | 0:bbc12ba2cb6c | 31 | // store pointer to the I2C class object |
wliu88 | 0:bbc12ba2cb6c | 32 | wire = _wire; |
wliu88 | 0:bbc12ba2cb6c | 33 | |
wliu88 | 0:bbc12ba2cb6c | 34 | // turn on oscillator |
wliu88 | 0:bbc12ba2cb6c | 35 | cmd[0] = 0x21; |
wliu88 | 0:bbc12ba2cb6c | 36 | wire->write(i2c_addr, cmd, 1); |
wliu88 | 0:bbc12ba2cb6c | 37 | // set blink rate |
wliu88 | 0:bbc12ba2cb6c | 38 | blinkRate(HT16K33_BLINK_OFF); |
wliu88 | 0:bbc12ba2cb6c | 39 | // set brightness |
wliu88 | 0:bbc12ba2cb6c | 40 | setBrightness(15); // max brightness |
wliu88 | 0:bbc12ba2cb6c | 41 | // setup interrupt, active low |
wliu88 | 0:bbc12ba2cb6c | 42 | cmd[0] = 0xA1; |
wliu88 | 0:bbc12ba2cb6c | 43 | wire->write(i2c_addr, cmd, 1); |
wliu88 | 0:bbc12ba2cb6c | 44 | } |
wliu88 | 0:bbc12ba2cb6c | 45 | |
wliu88 | 0:bbc12ba2cb6c | 46 | /* |
wliu88 | 0:bbc12ba2cb6c | 47 | Helper button functions, the data is updated every readSwitches() call! |
wliu88 | 0:bbc12ba2cb6c | 48 | */ |
wliu88 | 0:bbc12ba2cb6c | 49 | |
wliu88 | 0:bbc12ba2cb6c | 50 | bool Adafruit_Trellis::isKeyPressed(uint8_t k) { |
wliu88 | 0:bbc12ba2cb6c | 51 | if (k > 15) return false; |
wliu88 | 0:bbc12ba2cb6c | 52 | k = buttonLUT[k]; |
wliu88 | 0:bbc12ba2cb6c | 53 | return (keys[k>>4] & _BV(k & 0x0F)); |
wliu88 | 0:bbc12ba2cb6c | 54 | } |
wliu88 | 0:bbc12ba2cb6c | 55 | bool Adafruit_Trellis::wasKeyPressed(uint8_t k) { |
wliu88 | 0:bbc12ba2cb6c | 56 | if (k > 15) return false; |
wliu88 | 0:bbc12ba2cb6c | 57 | k = buttonLUT[k]; |
wliu88 | 0:bbc12ba2cb6c | 58 | return (lastkeys[k>>4] & _BV(k & 0x0F)); |
wliu88 | 0:bbc12ba2cb6c | 59 | } |
wliu88 | 0:bbc12ba2cb6c | 60 | |
wliu88 | 0:bbc12ba2cb6c | 61 | bool Adafruit_Trellis::justPressed(uint8_t k) { |
wliu88 | 0:bbc12ba2cb6c | 62 | return (isKeyPressed(k) & !wasKeyPressed(k)); |
wliu88 | 0:bbc12ba2cb6c | 63 | } |
wliu88 | 0:bbc12ba2cb6c | 64 | bool Adafruit_Trellis::justReleased(uint8_t k) { |
wliu88 | 0:bbc12ba2cb6c | 65 | return (!isKeyPressed(k) & wasKeyPressed(k)); |
wliu88 | 0:bbc12ba2cb6c | 66 | } |
wliu88 | 0:bbc12ba2cb6c | 67 | |
wliu88 | 0:bbc12ba2cb6c | 68 | /* |
wliu88 | 0:bbc12ba2cb6c | 69 | Helper LED functions, the data is written on writeDisplay() |
wliu88 | 0:bbc12ba2cb6c | 70 | */ |
wliu88 | 0:bbc12ba2cb6c | 71 | |
wliu88 | 0:bbc12ba2cb6c | 72 | |
wliu88 | 0:bbc12ba2cb6c | 73 | bool Adafruit_Trellis::isLED(uint8_t x) { |
wliu88 | 0:bbc12ba2cb6c | 74 | if (x > 15) return false; |
wliu88 | 0:bbc12ba2cb6c | 75 | x = ledLUT[x]; |
wliu88 | 0:bbc12ba2cb6c | 76 | return ((displaybuffer[x >> 4] & _BV(x & 0x0F)) > 0); |
wliu88 | 0:bbc12ba2cb6c | 77 | } |
wliu88 | 0:bbc12ba2cb6c | 78 | void Adafruit_Trellis::setLED(uint8_t x) { |
wliu88 | 0:bbc12ba2cb6c | 79 | if (x > 15) return; |
wliu88 | 0:bbc12ba2cb6c | 80 | x = ledLUT[x]; |
wliu88 | 0:bbc12ba2cb6c | 81 | displaybuffer[x >> 4] |= _BV(x & 0x0F); |
wliu88 | 0:bbc12ba2cb6c | 82 | } |
wliu88 | 0:bbc12ba2cb6c | 83 | void Adafruit_Trellis::clrLED(uint8_t x) { |
wliu88 | 0:bbc12ba2cb6c | 84 | if (x > 15) return; |
wliu88 | 0:bbc12ba2cb6c | 85 | x = ledLUT[x]; |
wliu88 | 0:bbc12ba2cb6c | 86 | displaybuffer[x >> 4] &= ~_BV(x & 0x0F); |
wliu88 | 0:bbc12ba2cb6c | 87 | } |
wliu88 | 0:bbc12ba2cb6c | 88 | |
wliu88 | 0:bbc12ba2cb6c | 89 | |
wliu88 | 0:bbc12ba2cb6c | 90 | /* |
wliu88 | 0:bbc12ba2cb6c | 91 | Gets the switch memory data and updates the last/current read |
wliu88 | 0:bbc12ba2cb6c | 92 | */ |
wliu88 | 0:bbc12ba2cb6c | 93 | |
wliu88 | 0:bbc12ba2cb6c | 94 | bool Adafruit_Trellis::readSwitches(void) { |
wliu88 | 0:bbc12ba2cb6c | 95 | memcpy(lastkeys, keys, sizeof(keys)); |
wliu88 | 0:bbc12ba2cb6c | 96 | |
wliu88 | 0:bbc12ba2cb6c | 97 | cmd[0] = 0x40; |
wliu88 | 0:bbc12ba2cb6c | 98 | wire->write(i2c_addr, cmd, 1); |
wliu88 | 0:bbc12ba2cb6c | 99 | wire->read(i2c_addr, cmd, 6); |
wliu88 | 0:bbc12ba2cb6c | 100 | |
wliu88 | 0:bbc12ba2cb6c | 101 | for (uint8_t i=0; i<6; i++) |
wliu88 | 0:bbc12ba2cb6c | 102 | keys[i] = cmd[i]; |
wliu88 | 0:bbc12ba2cb6c | 103 | |
wliu88 | 0:bbc12ba2cb6c | 104 | for (uint8_t i=0; i<6; i++) { |
wliu88 | 0:bbc12ba2cb6c | 105 | if (lastkeys[i] != keys[i]) { |
wliu88 | 0:bbc12ba2cb6c | 106 | for (uint8_t j=0; j<6; j++) { |
wliu88 | 0:bbc12ba2cb6c | 107 | //Serial.print(keys[j], HEX); Serial.print(" "); |
wliu88 | 0:bbc12ba2cb6c | 108 | } |
wliu88 | 0:bbc12ba2cb6c | 109 | //Serial.println(); |
wliu88 | 0:bbc12ba2cb6c | 110 | return true; |
wliu88 | 0:bbc12ba2cb6c | 111 | } |
wliu88 | 0:bbc12ba2cb6c | 112 | } |
wliu88 | 0:bbc12ba2cb6c | 113 | return false; |
wliu88 | 0:bbc12ba2cb6c | 114 | } |
wliu88 | 0:bbc12ba2cb6c | 115 | |
wliu88 | 0:bbc12ba2cb6c | 116 | void Adafruit_Trellis::setBrightness(uint8_t b) { |
wliu88 | 0:bbc12ba2cb6c | 117 | if (b > 15) b = 15; |
wliu88 | 0:bbc12ba2cb6c | 118 | cmd[0] = HT16K33_CMD_BRIGHTNESS | b; |
wliu88 | 0:bbc12ba2cb6c | 119 | wire->write(i2c_addr, cmd, 1); |
wliu88 | 0:bbc12ba2cb6c | 120 | } |
wliu88 | 0:bbc12ba2cb6c | 121 | |
wliu88 | 0:bbc12ba2cb6c | 122 | void Adafruit_Trellis::blinkRate(uint8_t b) { |
wliu88 | 0:bbc12ba2cb6c | 123 | if (b > 3) b = 0; // turn off if not sure |
wliu88 | 0:bbc12ba2cb6c | 124 | cmd[0] = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1); |
wliu88 | 0:bbc12ba2cb6c | 125 | wire->write(i2c_addr, cmd, 1); |
wliu88 | 0:bbc12ba2cb6c | 126 | } |
wliu88 | 0:bbc12ba2cb6c | 127 | |
wliu88 | 0:bbc12ba2cb6c | 128 | |
wliu88 | 0:bbc12ba2cb6c | 129 | void Adafruit_Trellis::writeDisplay(void) { |
wliu88 | 0:bbc12ba2cb6c | 130 | cmd[0] = 0x00; |
wliu88 | 0:bbc12ba2cb6c | 131 | for (uint8_t i=0; i<8; i++) { |
wliu88 | 0:bbc12ba2cb6c | 132 | cmd[2*i+1] = displaybuffer[i] & 0xFF; |
wliu88 | 0:bbc12ba2cb6c | 133 | cmd[2*i+2] = displaybuffer[i] >> 8; |
wliu88 | 0:bbc12ba2cb6c | 134 | } |
wliu88 | 0:bbc12ba2cb6c | 135 | wire->write(i2c_addr, cmd, 17); |
wliu88 | 0:bbc12ba2cb6c | 136 | |
wliu88 | 0:bbc12ba2cb6c | 137 | } |
wliu88 | 0:bbc12ba2cb6c | 138 | |
wliu88 | 0:bbc12ba2cb6c | 139 | void Adafruit_Trellis::clear(void) { |
wliu88 | 0:bbc12ba2cb6c | 140 | memset(displaybuffer, 0, sizeof(displaybuffer)); |
wliu88 | 0:bbc12ba2cb6c | 141 | } |
wliu88 | 0:bbc12ba2cb6c | 142 | |
wliu88 | 0:bbc12ba2cb6c | 143 |