This is a library for the Adafruit LED Backpacks. This library works with the Adafruit Mini 8x8.

Dependents:   CompSci335Project TritiumSim

How to use this library:

main.cpp

#include "mbed.h"
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

I2C i2c(p9, p10);

Adafruit_8x8matrix matrix = Adafruit_8x8matrix(&i2c);

int main() {
    matrix.begin(0x70);
    while(1) {
        matrix.clear();
        for (int i = 0; i < 8; i++) {
          for (int j = 0; j < 8; j++) {
              matrix.drawPixel(i, j, LED_ON);  
          }
        }
        matrix.writeDisplay();  // write the changes we just made to the display
        wait(60);
    }
}
Committer:
luizhespanha
Date:
Fri Aug 16 15:44:44 2013 +0000
Revision:
1:f066d5347c60
Parent:
0:e81a6274acce
Add licence informations

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
luizhespanha 0:e81a6274acce 109 if (color) {
luizhespanha 0:e81a6274acce 110 displaybuffer[y] |= 1 << x;
luizhespanha 0:e81a6274acce 111 } else {
luizhespanha 0:e81a6274acce 112 displaybuffer[y] &= ~(1 << x);
luizhespanha 0:e81a6274acce 113 }
luizhespanha 0:e81a6274acce 114 }