Marton Szafian / Adafruit_LEDBackpack

Fork of Adafruit_LEDBackpack by Luiz Hespanha

Committer:
GuitarCraftMiner
Date:
Tue Mar 01 19:55:49 2016 +0000
Revision:
2:403251cd6392
Parent:
1:f066d5347c60
Added code to make the library display colours with Adafruit 8x8 Bicolor Led Matrix

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
luizhespanha 0:e81a6274acce 81 Adafruit_8x8matrix::Adafruit_8x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(8, 8) {
luizhespanha 0:e81a6274acce 82 }
luizhespanha 0:e81a6274acce 83
luizhespanha 0:e81a6274acce 84 void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
luizhespanha 0:e81a6274acce 85 if ((y < 0) || (y >= 8)) return;
luizhespanha 0:e81a6274acce 86 if ((x < 0) || (x >= 8)) return;
luizhespanha 0:e81a6274acce 87
luizhespanha 0:e81a6274acce 88 // check rotation, move pixel around if necessary
luizhespanha 0:e81a6274acce 89 switch (getRotation()) {
luizhespanha 0:e81a6274acce 90 case 1:
luizhespanha 0:e81a6274acce 91 swap(x, y);
luizhespanha 0:e81a6274acce 92 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 93 break;
luizhespanha 0:e81a6274acce 94 case 2:
luizhespanha 0:e81a6274acce 95 x = 8 - x - 1;
luizhespanha 0:e81a6274acce 96 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 97 break;
luizhespanha 0:e81a6274acce 98 case 3:
luizhespanha 0:e81a6274acce 99 swap(x, y);
luizhespanha 0:e81a6274acce 100 y = 8 - y - 1;
luizhespanha 0:e81a6274acce 101 break;
luizhespanha 0:e81a6274acce 102 }
luizhespanha 0:e81a6274acce 103
luizhespanha 0:e81a6274acce 104 // wrap around the x
luizhespanha 0:e81a6274acce 105 x += 7;
luizhespanha 0:e81a6274acce 106 x %= 8;
luizhespanha 0:e81a6274acce 107
luizhespanha 0:e81a6274acce 108
GuitarCraftMiner 2:403251cd6392 109 if (color == LED_GREEN) {
GuitarCraftMiner 2:403251cd6392 110 // Turn on green LED.
luizhespanha 0:e81a6274acce 111 displaybuffer[y] |= 1 << x;
GuitarCraftMiner 2:403251cd6392 112 // Turn off red LED.
GuitarCraftMiner 2:403251cd6392 113 displaybuffer[y] &= ~(1 << (x+8));
GuitarCraftMiner 2:403251cd6392 114 } else if (color == LED_RED) {
GuitarCraftMiner 2:403251cd6392 115 // Turn on red LED.
GuitarCraftMiner 2:403251cd6392 116 displaybuffer[y] |= 1 << (x+8);
GuitarCraftMiner 2:403251cd6392 117 // Turn off green LED.
luizhespanha 0:e81a6274acce 118 displaybuffer[y] &= ~(1 << x);
GuitarCraftMiner 2:403251cd6392 119 } else if (color == LED_YELLOW) {
GuitarCraftMiner 2:403251cd6392 120 // Turn on green and red LED.
GuitarCraftMiner 2:403251cd6392 121 displaybuffer[y] |= (1 << (x+8)) | (1 << x);
GuitarCraftMiner 2:403251cd6392 122 } else if (color == LED_OFF) {
GuitarCraftMiner 2:403251cd6392 123 // Turn off green and red LED.
GuitarCraftMiner 2:403251cd6392 124 displaybuffer[y] &= ~(1 << x) & ~(1 << (x+8));
luizhespanha 0:e81a6274acce 125 }
luizhespanha 0:e81a6274acce 126 }