this hurts

Dependencies:   FFT

Committer:
annieluo2
Date:
Wed Dec 02 18:02:03 2020 +0000
Revision:
0:d6c9b09b4042
boo

Who changed what in which revision?

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