うおーるぼっとLEDのワークショップ用

Dependencies:   mbed

Committer:
jksoft
Date:
Sat Feb 04 03:33:08 2017 +0000
Revision:
3:b707c09b4433
Parent:
0:9fcc79b3f00e
????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:9fcc79b3f00e 1 /***************************************************
jksoft 0:9fcc79b3f00e 2 This is a library for our I2C LED Backpacks
jksoft 0:9fcc79b3f00e 3
jksoft 0:9fcc79b3f00e 4 Designed specifically to work with the Adafruit LED Matrix backpacks
jksoft 0:9fcc79b3f00e 5 ----> http://www.adafruit.com/products/
jksoft 0:9fcc79b3f00e 6 ----> http://www.adafruit.com/products/
jksoft 0:9fcc79b3f00e 7
jksoft 0:9fcc79b3f00e 8 These displays use I2C to communicate, 2 pins are required to
jksoft 0:9fcc79b3f00e 9 interface. There are multiple selectable I2C addresses. For backpacks
jksoft 0:9fcc79b3f00e 10 with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
jksoft 0:9fcc79b3f00e 11 with 3 Address Select pins: 0x70 thru 0x77
jksoft 0:9fcc79b3f00e 12
jksoft 0:9fcc79b3f00e 13 Adafruit invests time and resources providing this open source code,
jksoft 0:9fcc79b3f00e 14 please support Adafruit and open-source hardware by purchasing
jksoft 0:9fcc79b3f00e 15 products from Adafruit!
jksoft 0:9fcc79b3f00e 16
jksoft 0:9fcc79b3f00e 17 Written by Limor Fried/Ladyada for Adafruit Industries.
jksoft 0:9fcc79b3f00e 18 BSD license, all text above must be included in any redistribution
jksoft 0:9fcc79b3f00e 19 ****************************************************/
jksoft 0:9fcc79b3f00e 20
jksoft 0:9fcc79b3f00e 21 /*
jksoft 0:9fcc79b3f00e 22 * Modified by Luiz Hespanha (http://www.d3.do) 8/16/2013 for use in LPC1768
jksoft 0:9fcc79b3f00e 23 */
jksoft 0:9fcc79b3f00e 24
jksoft 0:9fcc79b3f00e 25 #include "mbed.h"
jksoft 0:9fcc79b3f00e 26 #include "Adafruit_LEDBackpack.h"
jksoft 0:9fcc79b3f00e 27 #include "Adafruit_GFX.h"
jksoft 0:9fcc79b3f00e 28
jksoft 0:9fcc79b3f00e 29 void Adafruit_LEDBackpack::setBrightness(uint8_t b) {
jksoft 0:9fcc79b3f00e 30 if (b > 15) b = 15;
jksoft 0:9fcc79b3f00e 31 uint8_t c = 0xE0 | b;
jksoft 0:9fcc79b3f00e 32 char foo[1];
jksoft 0:9fcc79b3f00e 33 foo[0] = c;
jksoft 0:9fcc79b3f00e 34 _i2c->write(i2c_addr, foo, 1);
jksoft 0:9fcc79b3f00e 35 }
jksoft 0:9fcc79b3f00e 36
jksoft 0:9fcc79b3f00e 37 void Adafruit_LEDBackpack::blinkRate(uint8_t b) {
jksoft 0:9fcc79b3f00e 38 if (b > 3) b = 0; // turn off if not sure
jksoft 0:9fcc79b3f00e 39 uint8_t c = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
jksoft 0:9fcc79b3f00e 40 char foo[1];
jksoft 0:9fcc79b3f00e 41 foo[0] = c;
jksoft 0:9fcc79b3f00e 42 _i2c->write(i2c_addr, foo, 1);
jksoft 0:9fcc79b3f00e 43 }
jksoft 0:9fcc79b3f00e 44
jksoft 0:9fcc79b3f00e 45 Adafruit_LEDBackpack::Adafruit_LEDBackpack(I2C *i2c): _i2c(i2c) {
jksoft 0:9fcc79b3f00e 46 }
jksoft 0:9fcc79b3f00e 47
jksoft 0:9fcc79b3f00e 48 void Adafruit_LEDBackpack::begin(uint8_t _addr = 0x70) {
jksoft 0:9fcc79b3f00e 49 i2c_addr = _addr << 1;
jksoft 0:9fcc79b3f00e 50
jksoft 0:9fcc79b3f00e 51 char foo[1];
jksoft 0:9fcc79b3f00e 52 foo[0] = 0x21;
jksoft 0:9fcc79b3f00e 53
jksoft 0:9fcc79b3f00e 54 _i2c->write(i2c_addr, foo, 1); // turn on oscillator
jksoft 0:9fcc79b3f00e 55
jksoft 0:9fcc79b3f00e 56 blinkRate(HT16K33_BLINK_OFF);
jksoft 0:9fcc79b3f00e 57
jksoft 0:9fcc79b3f00e 58 setBrightness(15); // max brightness
jksoft 0:9fcc79b3f00e 59 }
jksoft 0:9fcc79b3f00e 60
jksoft 0:9fcc79b3f00e 61 void Adafruit_LEDBackpack::writeDisplay(void) {
jksoft 0:9fcc79b3f00e 62 char foo[17];
jksoft 0:9fcc79b3f00e 63 foo[0] = 0x00;
jksoft 0:9fcc79b3f00e 64 int j = 0;
jksoft 0:9fcc79b3f00e 65 for (uint8_t i=1; i<=16; i+=2) {
jksoft 0:9fcc79b3f00e 66 int x = displaybuffer[j] & 0xFF;
jksoft 0:9fcc79b3f00e 67 foo[i] = x;
jksoft 0:9fcc79b3f00e 68 int x2 = displaybuffer[j] >> 8;
jksoft 0:9fcc79b3f00e 69 foo[i+1] = x2;
jksoft 0:9fcc79b3f00e 70 j++;
jksoft 0:9fcc79b3f00e 71 }
jksoft 0:9fcc79b3f00e 72 _i2c->write(i2c_addr, foo, 17);
jksoft 0:9fcc79b3f00e 73 }
jksoft 0:9fcc79b3f00e 74
jksoft 0:9fcc79b3f00e 75 void Adafruit_LEDBackpack::clear(void) {
jksoft 0:9fcc79b3f00e 76 for (uint8_t i=0; i<16; i++) {
jksoft 0:9fcc79b3f00e 77 displaybuffer[i] = 0;
jksoft 0:9fcc79b3f00e 78 }
jksoft 0:9fcc79b3f00e 79 }
jksoft 0:9fcc79b3f00e 80
jksoft 0:9fcc79b3f00e 81 Adafruit_8x8matrix::Adafruit_8x8matrix(I2C *i2c) : Adafruit_LEDBackpack(i2c), Adafruit_GFX(16, 16) {
jksoft 0:9fcc79b3f00e 82 }
jksoft 0:9fcc79b3f00e 83
jksoft 0:9fcc79b3f00e 84 void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
jksoft 0:9fcc79b3f00e 85 if ((y < 0) || (y >= 16)) return;
jksoft 0:9fcc79b3f00e 86 if ((x < 0) || (x >= 16)) return;
jksoft 0:9fcc79b3f00e 87
jksoft 0:9fcc79b3f00e 88 // check rotation, move pixel around if necessary
jksoft 0:9fcc79b3f00e 89 switch (getRotation()) {
jksoft 0:9fcc79b3f00e 90 case 0:
jksoft 0:9fcc79b3f00e 91 if(y>=8){
jksoft 0:9fcc79b3f00e 92 y+=16;
jksoft 0:9fcc79b3f00e 93 }
jksoft 0:9fcc79b3f00e 94 if(x>=8){
jksoft 0:9fcc79b3f00e 95 x-=8;
jksoft 0:9fcc79b3f00e 96 y+=8;
jksoft 0:9fcc79b3f00e 97 }
jksoft 0:9fcc79b3f00e 98 swap(x,y);
jksoft 0:9fcc79b3f00e 99 break;
jksoft 0:9fcc79b3f00e 100 case 1:
jksoft 0:9fcc79b3f00e 101 swap(x, y);
jksoft 0:9fcc79b3f00e 102 x = 8 - x - 1;
jksoft 0:9fcc79b3f00e 103 break;
jksoft 0:9fcc79b3f00e 104 case 2:
jksoft 0:9fcc79b3f00e 105 x = 8 - x - 1;
jksoft 0:9fcc79b3f00e 106 y = 8 - y - 1;
jksoft 0:9fcc79b3f00e 107 break;
jksoft 0:9fcc79b3f00e 108 case 3:
jksoft 0:9fcc79b3f00e 109 swap(x, y);
jksoft 0:9fcc79b3f00e 110 y = 8 - y - 1;
jksoft 0:9fcc79b3f00e 111 break;
jksoft 0:9fcc79b3f00e 112 }
jksoft 0:9fcc79b3f00e 113
jksoft 0:9fcc79b3f00e 114 #if 0
jksoft 0:9fcc79b3f00e 115 // wrap around the x
jksoft 0:9fcc79b3f00e 116 x += 7;
jksoft 0:9fcc79b3f00e 117 x %= 8;
jksoft 0:9fcc79b3f00e 118 #endif
jksoft 0:9fcc79b3f00e 119
jksoft 0:9fcc79b3f00e 120
jksoft 0:9fcc79b3f00e 121 if (color) {
jksoft 0:9fcc79b3f00e 122 displaybuffer[y] |= 1 << x;
jksoft 0:9fcc79b3f00e 123 } else {
jksoft 0:9fcc79b3f00e 124 displaybuffer[y] &= ~(1 << x);
jksoft 0:9fcc79b3f00e 125 }
jksoft 0:9fcc79b3f00e 126 }