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

Committer:
maclobdell
Date:
Thu Dec 22 20:40:29 2016 +0000
Revision:
4:b4ce71619694
Parent:
3:b0ac557b5f0c
hacks to get 32 x 8 led matrix working, need to clean up later

Who changed what in which revision?

UserRevisionLine numberNew contents of line
luizhespanha 0:e81a6274acce 1 /***************************************************
luizhespanha 0:e81a6274acce 2 This is a library for our I2C LED Backpacks
luizhespanha 0:e81a6274acce 3
luizhespanha 0:e81a6274acce 4 Designed specifically to work with the Adafruit LED Matrix backpacks
luizhespanha 0:e81a6274acce 5 ----> http://www.adafruit.com/products/
luizhespanha 0:e81a6274acce 6 ----> http://www.adafruit.com/products/
luizhespanha 0:e81a6274acce 7
luizhespanha 0:e81a6274acce 8 These displays use I2C to communicate, 2 pins are required to
luizhespanha 0:e81a6274acce 9 interface. There are multiple selectable I2C addresses. For backpacks
luizhespanha 0:e81a6274acce 10 with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
luizhespanha 0:e81a6274acce 11 with 3 Address Select pins: 0x70 thru 0x77
luizhespanha 0:e81a6274acce 12
luizhespanha 0:e81a6274acce 13 Adafruit invests time and resources providing this open source code,
luizhespanha 0:e81a6274acce 14 please support Adafruit and open-source hardware by purchasing
luizhespanha 0:e81a6274acce 15 products from Adafruit!
luizhespanha 0:e81a6274acce 16
luizhespanha 0:e81a6274acce 17 Written by Limor Fried/Ladyada for Adafruit Industries.
luizhespanha 0:e81a6274acce 18 BSD license, all text above must be included in any redistribution
luizhespanha 0:e81a6274acce 19 ****************************************************/
luizhespanha 1:f066d5347c60 20
luizhespanha 1:f066d5347c60 21 /*
luizhespanha 1:f066d5347c60 22 * Modified by Luiz Hespanha (http://www.d3.do) 8/16/2013 for use in LPC1768
luizhespanha 1:f066d5347c60 23 */
luizhespanha 0:e81a6274acce 24
luizhespanha 0:e81a6274acce 25 #include "mbed.h"
luizhespanha 0:e81a6274acce 26 #include "Adafruit_LEDBackpack.h"
luizhespanha 0:e81a6274acce 27 #include "Adafruit_GFX.h"
maclobdell 3:b0ac557b5f0c 28 #include "glcdfont.h"
maclobdell 3:b0ac557b5f0c 29
maclobdell 3:b0ac557b5f0c 30 //mpl - something with stdio screwing things up. need to define serial port to use for debug
maclobdell 3:b0ac557b5f0c 31 extern Serial pc;
luizhespanha 0:e81a6274acce 32
luizhespanha 0:e81a6274acce 33 void Adafruit_LEDBackpack::setBrightness(uint8_t b) {
luizhespanha 0:e81a6274acce 34 if (b > 15) b = 15;
luizhespanha 0:e81a6274acce 35 uint8_t c = 0xE0 | b;
luizhespanha 0:e81a6274acce 36 char foo[1];
luizhespanha 0:e81a6274acce 37 foo[0] = c;
luizhespanha 0:e81a6274acce 38 _i2c->write(i2c_addr, foo, 1);
luizhespanha 0:e81a6274acce 39 }
luizhespanha 0:e81a6274acce 40
luizhespanha 0:e81a6274acce 41 void Adafruit_LEDBackpack::blinkRate(uint8_t b) {
luizhespanha 0:e81a6274acce 42 if (b > 3) b = 0; // turn off if not sure
luizhespanha 0:e81a6274acce 43 uint8_t c = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
luizhespanha 0:e81a6274acce 44 char foo[1];
luizhespanha 0:e81a6274acce 45 foo[0] = c;
luizhespanha 0:e81a6274acce 46 _i2c->write(i2c_addr, foo, 1);
luizhespanha 0:e81a6274acce 47 }
luizhespanha 0:e81a6274acce 48
luizhespanha 0:e81a6274acce 49 Adafruit_LEDBackpack::Adafruit_LEDBackpack(I2C *i2c): _i2c(i2c) {
luizhespanha 0:e81a6274acce 50 }
luizhespanha 0:e81a6274acce 51
luizhespanha 0:e81a6274acce 52 void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70) {
luizhespanha 0:e81a6274acce 53 i2c_addr = _addr << 1;
luizhespanha 0:e81a6274acce 54
luizhespanha 0:e81a6274acce 55 char foo[1];
luizhespanha 0:e81a6274acce 56 foo[0] = 0x21;
luizhespanha 0:e81a6274acce 57
luizhespanha 0:e81a6274acce 58 _i2c->write(i2c_addr, foo, 1); // turn on oscillator
luizhespanha 0:e81a6274acce 59
luizhespanha 0:e81a6274acce 60 blinkRate(HT16K33_BLINK_OFF);
luizhespanha 0:e81a6274acce 61
luizhespanha 0:e81a6274acce 62 setBrightness(15); // max brightness
luizhespanha 0:e81a6274acce 63 }
luizhespanha 0:e81a6274acce 64
luizhespanha 0:e81a6274acce 65 void Adafruit_LEDBackpack::writeDisplay(void) {
luizhespanha 0:e81a6274acce 66 char foo[17];
luizhespanha 0:e81a6274acce 67 foo[0] = 0x00;
luizhespanha 0:e81a6274acce 68 int j = 0;
luizhespanha 0:e81a6274acce 69 for (uint8_t i=1; i<=16; i+=2) {
luizhespanha 0:e81a6274acce 70 int x = displaybuffer[j] & 0xFF;
luizhespanha 0:e81a6274acce 71 foo[i] = x;
luizhespanha 0:e81a6274acce 72 int x2 = displaybuffer[j] >> 8;
luizhespanha 0:e81a6274acce 73 foo[i+1] = x2;
luizhespanha 0:e81a6274acce 74 j++;
luizhespanha 0:e81a6274acce 75 }
luizhespanha 0:e81a6274acce 76 _i2c->write(i2c_addr, foo, 17);
luizhespanha 0:e81a6274acce 77 }
luizhespanha 0:e81a6274acce 78
luizhespanha 0:e81a6274acce 79 void Adafruit_LEDBackpack::clear(void) {
maclobdell 3:b0ac557b5f0c 80 for (uint8_t i=0; i<16; i++) {
luizhespanha 0:e81a6274acce 81 displaybuffer[i] = 0;
luizhespanha 0:e81a6274acce 82 }
luizhespanha 0:e81a6274acce 83 }
luizhespanha 0:e81a6274acce 84
luizhespanha 0:e81a6274acce 85 Adafruit_8x8matrix::Adafruit_8x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(8, 8) {
luizhespanha 0:e81a6274acce 86 }
luizhespanha 0:e81a6274acce 87
luizhespanha 0:e81a6274acce 88 void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
luizhespanha 0:e81a6274acce 89 if ((y < 0) || (y >= 8)) return;
luizhespanha 0:e81a6274acce 90 if ((x < 0) || (x >= 8)) return;
luizhespanha 0:e81a6274acce 91
luizhespanha 0:e81a6274acce 92 // check rotation, move pixel around if necessary
luizhespanha 0:e81a6274acce 93 switch (getRotation()) {
luizhespanha 0:e81a6274acce 94 case 1:
luizhespanha 0:e81a6274acce 95 swap(x, y);
luizhespanha 0:e81a6274acce 96 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 97 break;
luizhespanha 0:e81a6274acce 98 case 2:
luizhespanha 0:e81a6274acce 99 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 100 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 101 break;
luizhespanha 0:e81a6274acce 102 case 3:
luizhespanha 0:e81a6274acce 103 swap(x, y);
luizhespanha 0:e81a6274acce 104 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 105 break;
luizhespanha 0:e81a6274acce 106 }
luizhespanha 0:e81a6274acce 107
luizhespanha 0:e81a6274acce 108 // wrap around the x
luizhespanha 0:e81a6274acce 109 x += 7;
luizhespanha 0:e81a6274acce 110 x %= 8;
luizhespanha 0:e81a6274acce 111
luizhespanha 0:e81a6274acce 112
luizhespanha 0:e81a6274acce 113 if (color) {
luizhespanha 0:e81a6274acce 114 displaybuffer[y] |= 1 << x;
luizhespanha 0:e81a6274acce 115 } else {
luizhespanha 0:e81a6274acce 116 displaybuffer[y] &= ~(1 << x);
luizhespanha 0:e81a6274acce 117 }
maclobdell 3:b0ac557b5f0c 118
maclobdell 3:b0ac557b5f0c 119
maclobdell 2:529bad55777e 120 }
maclobdell 2:529bad55777e 121
maclobdell 2:529bad55777e 122
maclobdell 2:529bad55777e 123 Adafruit_16x8matrix::Adafruit_16x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(16, 8) {
maclobdell 2:529bad55777e 124 }
maclobdell 2:529bad55777e 125
maclobdell 2:529bad55777e 126 void Adafruit_16x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
maclobdell 2:529bad55777e 127 if ((y < 0) || (y >= 8)) return;
maclobdell 2:529bad55777e 128 if ((x < 0) || (x >= 16)) return;
maclobdell 3:b0ac557b5f0c 129
maclobdell 3:b0ac557b5f0c 130 // check rotation, move pixel around if necessary
maclobdell 2:529bad55777e 131 switch (getRotation()) {
maclobdell 2:529bad55777e 132 case 1:
maclobdell 2:529bad55777e 133 swap(x, y);
maclobdell 2:529bad55777e 134 x = 16 - x - 1;
maclobdell 2:529bad55777e 135 break;
maclobdell 2:529bad55777e 136 case 2:
maclobdell 2:529bad55777e 137 x = 16 - x - 1;
maclobdell 2:529bad55777e 138 y = 8 - y - 1;
maclobdell 2:529bad55777e 139 break;
maclobdell 2:529bad55777e 140 case 3:
maclobdell 2:529bad55777e 141 swap(x, y);
maclobdell 2:529bad55777e 142 y = 8 - y - 1;
maclobdell 2:529bad55777e 143 break;
maclobdell 2:529bad55777e 144 }
maclobdell 2:529bad55777e 145
maclobdell 2:529bad55777e 146 // wrap around the x
maclobdell 3:b0ac557b5f0c 147 x += 16;
maclobdell 2:529bad55777e 148 x %= 16;
maclobdell 2:529bad55777e 149
maclobdell 2:529bad55777e 150
maclobdell 2:529bad55777e 151 if (color) {
maclobdell 2:529bad55777e 152 displaybuffer[y] |= 1 << x;
maclobdell 2:529bad55777e 153 } else {
maclobdell 2:529bad55777e 154 displaybuffer[y] &= ~(1 << x);
maclobdell 2:529bad55777e 155 }
maclobdell 3:b0ac557b5f0c 156 }
maclobdell 3:b0ac557b5f0c 157
maclobdell 3:b0ac557b5f0c 158 size_t Adafruit_16x8matrix::writeChar(uint8_t c)
maclobdell 3:b0ac557b5f0c 159 {
maclobdell 3:b0ac557b5f0c 160
maclobdell 3:b0ac557b5f0c 161 if (c == '\n')
maclobdell 3:b0ac557b5f0c 162 {
maclobdell 3:b0ac557b5f0c 163 cursor_y += textsize*8;
maclobdell 3:b0ac557b5f0c 164 cursor_x = 0;
maclobdell 3:b0ac557b5f0c 165 }
maclobdell 3:b0ac557b5f0c 166 else if (c == '\r')
maclobdell 3:b0ac557b5f0c 167 cursor_x = 0;
maclobdell 3:b0ac557b5f0c 168 else
maclobdell 3:b0ac557b5f0c 169 {
maclobdell 3:b0ac557b5f0c 170 drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
maclobdell 3:b0ac557b5f0c 171 cursor_x += textsize*6;
maclobdell 3:b0ac557b5f0c 172 // if (wrap && (cursor_x > (_width - textsize*6)))
maclobdell 3:b0ac557b5f0c 173 // {
maclobdell 3:b0ac557b5f0c 174 // cursor_y += textsize*8;
maclobdell 3:b0ac557b5f0c 175 // cursor_x = 0;
maclobdell 3:b0ac557b5f0c 176 //}
maclobdell 3:b0ac557b5f0c 177 }
maclobdell 3:b0ac557b5f0c 178 return 1;
maclobdell 3:b0ac557b5f0c 179 }
maclobdell 3:b0ac557b5f0c 180
maclobdell 3:b0ac557b5f0c 181 void Adafruit_16x8matrix::scrollChar(uint8_t c)
maclobdell 3:b0ac557b5f0c 182 {
maclobdell 3:b0ac557b5f0c 183
maclobdell 3:b0ac557b5f0c 184 if (c == '\n')
maclobdell 3:b0ac557b5f0c 185 {
maclobdell 3:b0ac557b5f0c 186 cursor_y += textsize*8;
maclobdell 3:b0ac557b5f0c 187 cursor_x = 0;
maclobdell 3:b0ac557b5f0c 188 }
maclobdell 3:b0ac557b5f0c 189 else if (c == '\r')
maclobdell 3:b0ac557b5f0c 190 cursor_x = 0;
maclobdell 3:b0ac557b5f0c 191 else
maclobdell 3:b0ac557b5f0c 192 {
maclobdell 3:b0ac557b5f0c 193 for(int i = 0; i < 16; i++)
maclobdell 3:b0ac557b5f0c 194 {
maclobdell 3:b0ac557b5f0c 195 drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
maclobdell 3:b0ac557b5f0c 196 writeDisplay();
maclobdell 3:b0ac557b5f0c 197 Thread::wait(100);
maclobdell 3:b0ac557b5f0c 198 clear();
maclobdell 3:b0ac557b5f0c 199 writeDisplay();
maclobdell 3:b0ac557b5f0c 200 cursor_x++;
maclobdell 3:b0ac557b5f0c 201 }
maclobdell 3:b0ac557b5f0c 202 }
maclobdell 3:b0ac557b5f0c 203 }
maclobdell 3:b0ac557b5f0c 204
maclobdell 3:b0ac557b5f0c 205 // draw a character
maclobdell 3:b0ac557b5f0c 206 void Adafruit_16x8matrix::drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size)
maclobdell 3:b0ac557b5f0c 207 {
maclobdell 3:b0ac557b5f0c 208
maclobdell 3:b0ac557b5f0c 209 //allow clipping
maclobdell 3:b0ac557b5f0c 210 // if(
maclobdell 3:b0ac557b5f0c 211 // (x >= _width) || // Clip right
maclobdell 3:b0ac557b5f0c 212 // (y >= _height) || // Clip bottom
maclobdell 3:b0ac557b5f0c 213 // ((x + 5 * size - 1) < 0) || // Clip left
maclobdell 3:b0ac557b5f0c 214 // ((y + 8 * size - 1) < 0) // Clip top
maclobdell 3:b0ac557b5f0c 215 // )
maclobdell 3:b0ac557b5f0c 216 // return;
maclobdell 3:b0ac557b5f0c 217
maclobdell 3:b0ac557b5f0c 218 for (int8_t i=0; i<6; i++ )
maclobdell 3:b0ac557b5f0c 219 {
maclobdell 3:b0ac557b5f0c 220 uint8_t line = 0;
maclobdell 3:b0ac557b5f0c 221
maclobdell 3:b0ac557b5f0c 222 if (i == 5)
maclobdell 3:b0ac557b5f0c 223 line = 0x0;
maclobdell 3:b0ac557b5f0c 224 else
maclobdell 3:b0ac557b5f0c 225 line = font[(c*5)+i];
maclobdell 3:b0ac557b5f0c 226
maclobdell 3:b0ac557b5f0c 227 for (int8_t j = 0; j<8; j++)
maclobdell 3:b0ac557b5f0c 228 {
maclobdell 3:b0ac557b5f0c 229 if (line & 0x1)
maclobdell 3:b0ac557b5f0c 230 {
maclobdell 3:b0ac557b5f0c 231 if (size == 1) // default size
maclobdell 3:b0ac557b5f0c 232 drawPixel(x+i, y+j, color);
maclobdell 3:b0ac557b5f0c 233 #ifdef WANT_ABSTRACTS
maclobdell 3:b0ac557b5f0c 234 else // big size
maclobdell 3:b0ac557b5f0c 235 fillRect(x+(i*size), y+(j*size), size, size, color);
maclobdell 3:b0ac557b5f0c 236 #endif
maclobdell 3:b0ac557b5f0c 237 }
maclobdell 3:b0ac557b5f0c 238 else if (bg != color)
maclobdell 3:b0ac557b5f0c 239 {
maclobdell 3:b0ac557b5f0c 240 if (size == 1) // default size
maclobdell 3:b0ac557b5f0c 241 drawPixel(x+i, y+j, bg);
maclobdell 3:b0ac557b5f0c 242 #ifdef WANT_ABSTRACTS
maclobdell 3:b0ac557b5f0c 243 else // big size
maclobdell 3:b0ac557b5f0c 244 fillRect(x+i*size, y+j*size, size, size, bg);
maclobdell 3:b0ac557b5f0c 245 #endif
maclobdell 3:b0ac557b5f0c 246 }
maclobdell 3:b0ac557b5f0c 247 line >>= 1;
maclobdell 3:b0ac557b5f0c 248 }
maclobdell 3:b0ac557b5f0c 249 }
luizhespanha 0:e81a6274acce 250 }