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:
luizhespanha
Date:
Fri Aug 16 15:41:01 2013 +0000
Revision:
0:e81a6274acce
Child:
1:f066d5347c60
This is a library for the Adafruit LED Backpacks; This library works with the Adafruit Mini 8x8.

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 0:e81a6274acce 20
luizhespanha 0:e81a6274acce 21 #include "mbed.h"
luizhespanha 0:e81a6274acce 22 #include "Adafruit_LEDBackpack.h"
luizhespanha 0:e81a6274acce 23 #include "Adafruit_GFX.h"
luizhespanha 0:e81a6274acce 24
luizhespanha 0:e81a6274acce 25 void Adafruit_LEDBackpack::setBrightness(uint8_t b) {
luizhespanha 0:e81a6274acce 26 if (b > 15) b = 15;
luizhespanha 0:e81a6274acce 27 uint8_t c = 0xE0 | b;
luizhespanha 0:e81a6274acce 28 char foo[1];
luizhespanha 0:e81a6274acce 29 foo[0] = c;
luizhespanha 0:e81a6274acce 30 _i2c->write(i2c_addr, foo, 1);
luizhespanha 0:e81a6274acce 31 }
luizhespanha 0:e81a6274acce 32
luizhespanha 0:e81a6274acce 33 void Adafruit_LEDBackpack::blinkRate(uint8_t b) {
luizhespanha 0:e81a6274acce 34 if (b > 3) b = 0; // turn off if not sure
luizhespanha 0:e81a6274acce 35 uint8_t c = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
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 Adafruit_LEDBackpack::Adafruit_LEDBackpack(I2C *i2c): _i2c(i2c) {
luizhespanha 0:e81a6274acce 42 }
luizhespanha 0:e81a6274acce 43
luizhespanha 0:e81a6274acce 44 void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70) {
luizhespanha 0:e81a6274acce 45 i2c_addr = _addr << 1;
luizhespanha 0:e81a6274acce 46
luizhespanha 0:e81a6274acce 47 char foo[1];
luizhespanha 0:e81a6274acce 48 foo[0] = 0x21;
luizhespanha 0:e81a6274acce 49
luizhespanha 0:e81a6274acce 50 _i2c->write(i2c_addr, foo, 1); // turn on oscillator
luizhespanha 0:e81a6274acce 51
luizhespanha 0:e81a6274acce 52 blinkRate(HT16K33_BLINK_OFF);
luizhespanha 0:e81a6274acce 53
luizhespanha 0:e81a6274acce 54 setBrightness(15); // max brightness
luizhespanha 0:e81a6274acce 55 }
luizhespanha 0:e81a6274acce 56
luizhespanha 0:e81a6274acce 57 void Adafruit_LEDBackpack::writeDisplay(void) {
luizhespanha 0:e81a6274acce 58 char foo[17];
luizhespanha 0:e81a6274acce 59 foo[0] = 0x00;
luizhespanha 0:e81a6274acce 60 int j = 0;
luizhespanha 0:e81a6274acce 61 for (uint8_t i=1; i<=16; i+=2) {
luizhespanha 0:e81a6274acce 62 int x = displaybuffer[j] & 0xFF;
luizhespanha 0:e81a6274acce 63 foo[i] = x;
luizhespanha 0:e81a6274acce 64 int x2 = displaybuffer[j] >> 8;
luizhespanha 0:e81a6274acce 65 foo[i+1] = x2;
luizhespanha 0:e81a6274acce 66 j++;
luizhespanha 0:e81a6274acce 67 }
luizhespanha 0:e81a6274acce 68 _i2c->write(i2c_addr, foo, 17);
luizhespanha 0:e81a6274acce 69 }
luizhespanha 0:e81a6274acce 70
luizhespanha 0:e81a6274acce 71 void Adafruit_LEDBackpack::clear(void) {
luizhespanha 0:e81a6274acce 72 for (uint8_t i=0; i<8; i++) {
luizhespanha 0:e81a6274acce 73 displaybuffer[i] = 0;
luizhespanha 0:e81a6274acce 74 }
luizhespanha 0:e81a6274acce 75 }
luizhespanha 0:e81a6274acce 76
luizhespanha 0:e81a6274acce 77 Adafruit_8x8matrix::Adafruit_8x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(8, 8) {
luizhespanha 0:e81a6274acce 78 }
luizhespanha 0:e81a6274acce 79
luizhespanha 0:e81a6274acce 80 void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
luizhespanha 0:e81a6274acce 81 if ((y < 0) || (y >= 8)) return;
luizhespanha 0:e81a6274acce 82 if ((x < 0) || (x >= 8)) return;
luizhespanha 0:e81a6274acce 83
luizhespanha 0:e81a6274acce 84 // check rotation, move pixel around if necessary
luizhespanha 0:e81a6274acce 85 switch (getRotation()) {
luizhespanha 0:e81a6274acce 86 case 1:
luizhespanha 0:e81a6274acce 87 swap(x, y);
luizhespanha 0:e81a6274acce 88 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 89 break;
luizhespanha 0:e81a6274acce 90 case 2:
luizhespanha 0:e81a6274acce 91 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 92 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 93 break;
luizhespanha 0:e81a6274acce 94 case 3:
luizhespanha 0:e81a6274acce 95 swap(x, y);
luizhespanha 0:e81a6274acce 96 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 97 break;
luizhespanha 0:e81a6274acce 98 }
luizhespanha 0:e81a6274acce 99
luizhespanha 0:e81a6274acce 100 // wrap around the x
luizhespanha 0:e81a6274acce 101 x += 7;
luizhespanha 0:e81a6274acce 102 x %= 8;
luizhespanha 0:e81a6274acce 103
luizhespanha 0:e81a6274acce 104
luizhespanha 0:e81a6274acce 105 if (color) {
luizhespanha 0:e81a6274acce 106 displaybuffer[y] |= 1 << x;
luizhespanha 0:e81a6274acce 107 } else {
luizhespanha 0:e81a6274acce 108 displaybuffer[y] &= ~(1 << x);
luizhespanha 0:e81a6274acce 109 }
luizhespanha 0:e81a6274acce 110 }