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

Fork of Adafruit_LEDBackpack by Luiz Hespanha

Committer:
sknn
Date:
Tue Mar 24 17:35:22 2015 +0000
Revision:
2:caaae61819a4
Parent:
1:f066d5347c60
Now this library works with the Adafruit 16x8 LED Matrix backpacks, too.

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"
luizhespanha 0:e81a6274acce 28
luizhespanha 0:e81a6274acce 29 void Adafruit_LEDBackpack::setBrightness(uint8_t b) {
luizhespanha 0:e81a6274acce 30 if (b > 15) b = 15;
luizhespanha 0:e81a6274acce 31 uint8_t c = 0xE0 | b;
luizhespanha 0:e81a6274acce 32 char foo[1];
luizhespanha 0:e81a6274acce 33 foo[0] = c;
luizhespanha 0:e81a6274acce 34 _i2c->write(i2c_addr, foo, 1);
luizhespanha 0:e81a6274acce 35 }
luizhespanha 0:e81a6274acce 36
luizhespanha 0:e81a6274acce 37 void Adafruit_LEDBackpack::blinkRate(uint8_t b) {
luizhespanha 0:e81a6274acce 38 if (b > 3) b = 0; // turn off if not sure
luizhespanha 0:e81a6274acce 39 uint8_t c = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
luizhespanha 0:e81a6274acce 40 char foo[1];
luizhespanha 0:e81a6274acce 41 foo[0] = c;
luizhespanha 0:e81a6274acce 42 _i2c->write(i2c_addr, foo, 1);
luizhespanha 0:e81a6274acce 43 }
luizhespanha 0:e81a6274acce 44
luizhespanha 0:e81a6274acce 45 Adafruit_LEDBackpack::Adafruit_LEDBackpack(I2C *i2c): _i2c(i2c) {
luizhespanha 0:e81a6274acce 46 }
luizhespanha 0:e81a6274acce 47
luizhespanha 0:e81a6274acce 48 void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70) {
luizhespanha 0:e81a6274acce 49 i2c_addr = _addr << 1;
luizhespanha 0:e81a6274acce 50
luizhespanha 0:e81a6274acce 51 char foo[1];
luizhespanha 0:e81a6274acce 52 foo[0] = 0x21;
luizhespanha 0:e81a6274acce 53
luizhespanha 0:e81a6274acce 54 _i2c->write(i2c_addr, foo, 1); // turn on oscillator
luizhespanha 0:e81a6274acce 55
luizhespanha 0:e81a6274acce 56 blinkRate(HT16K33_BLINK_OFF);
luizhespanha 0:e81a6274acce 57
luizhespanha 0:e81a6274acce 58 setBrightness(15); // max brightness
luizhespanha 0:e81a6274acce 59 }
luizhespanha 0:e81a6274acce 60
luizhespanha 0:e81a6274acce 61 void Adafruit_LEDBackpack::writeDisplay(void) {
luizhespanha 0:e81a6274acce 62 char foo[17];
luizhespanha 0:e81a6274acce 63 foo[0] = 0x00;
luizhespanha 0:e81a6274acce 64 int j = 0;
luizhespanha 0:e81a6274acce 65 for (uint8_t i=1; i<=16; i+=2) {
luizhespanha 0:e81a6274acce 66 int x = displaybuffer[j] & 0xFF;
luizhespanha 0:e81a6274acce 67 foo[i] = x;
luizhespanha 0:e81a6274acce 68 int x2 = displaybuffer[j] >> 8;
luizhespanha 0:e81a6274acce 69 foo[i+1] = x2;
luizhespanha 0:e81a6274acce 70 j++;
luizhespanha 0:e81a6274acce 71 }
luizhespanha 0:e81a6274acce 72 _i2c->write(i2c_addr, foo, 17);
luizhespanha 0:e81a6274acce 73 }
luizhespanha 0:e81a6274acce 74
luizhespanha 0:e81a6274acce 75 void Adafruit_LEDBackpack::clear(void) {
luizhespanha 0:e81a6274acce 76 for (uint8_t i=0; i<8; i++) {
luizhespanha 0:e81a6274acce 77 displaybuffer[i] = 0;
luizhespanha 0:e81a6274acce 78 }
luizhespanha 0:e81a6274acce 79 }
luizhespanha 0:e81a6274acce 80
sknn 2:caaae61819a4 81 Adafruit_8x16matrix::Adafruit_8x16matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(8, 16) {
sknn 2:caaae61819a4 82 }
sknn 2:caaae61819a4 83
sknn 2:caaae61819a4 84 void Adafruit_8x16matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
sknn 2:caaae61819a4 85 if ((y < 0) || (y >= 8)) return;
sknn 2:caaae61819a4 86 if ((x < 0) || (x >= 16)) return;
sknn 2:caaae61819a4 87
sknn 2:caaae61819a4 88 // check rotation, move pixel around if necessary
sknn 2:caaae61819a4 89 switch (getRotation()) {
sknn 2:caaae61819a4 90 case 2:
sknn 2:caaae61819a4 91 swap(x, y);
sknn 2:caaae61819a4 92 x = 16 - x - 1;
sknn 2:caaae61819a4 93 break;
sknn 2:caaae61819a4 94 case 3:
sknn 2:caaae61819a4 95 x = 16 - x - 1;
sknn 2:caaae61819a4 96 y = 8 - y - 1;
sknn 2:caaae61819a4 97 break;
sknn 2:caaae61819a4 98 case 0:
sknn 2:caaae61819a4 99 swap(x, y);
sknn 2:caaae61819a4 100 y = 8 - y - 1;
sknn 2:caaae61819a4 101 break;
sknn 2:caaae61819a4 102 }
sknn 2:caaae61819a4 103
sknn 2:caaae61819a4 104 if (color) {
sknn 2:caaae61819a4 105 displaybuffer[y] |= 1 << x;
sknn 2:caaae61819a4 106 } else {
sknn 2:caaae61819a4 107 displaybuffer[y] &= ~(1 << x);
sknn 2:caaae61819a4 108 }
sknn 2:caaae61819a4 109 }
sknn 2:caaae61819a4 110
luizhespanha 0:e81a6274acce 111 Adafruit_8x8matrix::Adafruit_8x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(8, 8) {
luizhespanha 0:e81a6274acce 112 }
luizhespanha 0:e81a6274acce 113
luizhespanha 0:e81a6274acce 114 void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
luizhespanha 0:e81a6274acce 115 if ((y < 0) || (y >= 8)) return;
luizhespanha 0:e81a6274acce 116 if ((x < 0) || (x >= 8)) return;
luizhespanha 0:e81a6274acce 117
luizhespanha 0:e81a6274acce 118 // check rotation, move pixel around if necessary
luizhespanha 0:e81a6274acce 119 switch (getRotation()) {
luizhespanha 0:e81a6274acce 120 case 1:
luizhespanha 0:e81a6274acce 121 swap(x, y);
luizhespanha 0:e81a6274acce 122 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 123 break;
luizhespanha 0:e81a6274acce 124 case 2:
luizhespanha 0:e81a6274acce 125 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 126 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 127 break;
luizhespanha 0:e81a6274acce 128 case 3:
luizhespanha 0:e81a6274acce 129 swap(x, y);
luizhespanha 0:e81a6274acce 130 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 131 break;
luizhespanha 0:e81a6274acce 132 }
luizhespanha 0:e81a6274acce 133
luizhespanha 0:e81a6274acce 134 // wrap around the x
luizhespanha 0:e81a6274acce 135 x += 7;
luizhespanha 0:e81a6274acce 136 x %= 8;
luizhespanha 0:e81a6274acce 137
luizhespanha 0:e81a6274acce 138
luizhespanha 0:e81a6274acce 139 if (color) {
luizhespanha 0:e81a6274acce 140 displaybuffer[y] |= 1 << x;
luizhespanha 0:e81a6274acce 141 } else {
luizhespanha 0:e81a6274acce 142 displaybuffer[y] &= ~(1 << x);
luizhespanha 0:e81a6274acce 143 }
luizhespanha 0:e81a6274acce 144 }