Port of the Adafruit_IS31FL3731 library for Arduino. Enables control of the feather Charliewing LED Matrix peripheral board. Makes use of the I2Cdev library for I2C.
Dependencies: I2Cdev_MAX32630FTHR
Defaulty uses pinmap for the Maxim 32630FTHR Pegasus board. For a different board, change pin definitions in I2Cdev.h
Adafruit_IS31FL3731.cpp@0:9a73d45a17de, 2018-03-13 (annotated)
- Committer:
- DVLevine
- Date:
- Tue Mar 13 01:14:21 2018 +0000
- Revision:
- 0:9a73d45a17de
- Child:
- 3:0da61b470b86
Enable usage of Charlieplex LED Matrix feather wing with MAX32630FTHR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DVLevine | 0:9a73d45a17de | 1 | #include "Adafruit_IS31FL3731.h" |
DVLevine | 0:9a73d45a17de | 2 | #include "Adafruit_GFX.h" |
DVLevine | 0:9a73d45a17de | 3 | #ifndef _swap_int16_t |
DVLevine | 0:9a73d45a17de | 4 | #define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; } |
DVLevine | 0:9a73d45a17de | 5 | #endif |
DVLevine | 0:9a73d45a17de | 6 | |
DVLevine | 0:9a73d45a17de | 7 | /* Constructor */ |
DVLevine | 0:9a73d45a17de | 8 | /*Adafruit_IS31FL3731::Adafruit_IS31FL3731(uint8_t x, uint8_t y) : Adafruit_GFX(x, y) { |
DVLevine | 0:9a73d45a17de | 9 | //myI2C = theI2C; |
DVLevine | 0:9a73d45a17de | 10 | I2Cdev* myDevI2C = new I2Cdev(); |
DVLevine | 0:9a73d45a17de | 11 | myI2C = myDevI2C; |
DVLevine | 0:9a73d45a17de | 12 | } |
DVLevine | 0:9a73d45a17de | 13 | |
DVLevine | 0:9a73d45a17de | 14 | Adafruit_IS31FL3731_Wing::Adafruit_IS31FL3731_Wing() : Adafruit_IS31FL3731(15, 7) { |
DVLevine | 0:9a73d45a17de | 15 | //myI2C = theI2C; |
DVLevine | 0:9a73d45a17de | 16 | //myDevI2C |
DVLevine | 0:9a73d45a17de | 17 | I2Cdev* myDevI2C = new I2Cdev(); |
DVLevine | 0:9a73d45a17de | 18 | myI2C = myDevI2C; |
DVLevine | 0:9a73d45a17de | 19 | }*/ |
DVLevine | 0:9a73d45a17de | 20 | |
DVLevine | 0:9a73d45a17de | 21 | bool Adafruit_IS31FL3731::begin(uint8_t addr) { |
DVLevine | 0:9a73d45a17de | 22 | //Wire.begin(); |
DVLevine | 0:9a73d45a17de | 23 | |
DVLevine | 0:9a73d45a17de | 24 | |
DVLevine | 0:9a73d45a17de | 25 | _i2caddr = addr; |
DVLevine | 0:9a73d45a17de | 26 | _frame = 0; |
DVLevine | 0:9a73d45a17de | 27 | |
DVLevine | 0:9a73d45a17de | 28 | // shutdown |
DVLevine | 0:9a73d45a17de | 29 | writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00); |
DVLevine | 0:9a73d45a17de | 30 | |
DVLevine | 0:9a73d45a17de | 31 | //delay(10); |
DVLevine | 0:9a73d45a17de | 32 | wait(0.01); |
DVLevine | 0:9a73d45a17de | 33 | |
DVLevine | 0:9a73d45a17de | 34 | |
DVLevine | 0:9a73d45a17de | 35 | // out of shutdown |
DVLevine | 0:9a73d45a17de | 36 | writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01); |
DVLevine | 0:9a73d45a17de | 37 | |
DVLevine | 0:9a73d45a17de | 38 | // picture mode |
DVLevine | 0:9a73d45a17de | 39 | writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE); |
DVLevine | 0:9a73d45a17de | 40 | |
DVLevine | 0:9a73d45a17de | 41 | displayFrame(_frame); |
DVLevine | 0:9a73d45a17de | 42 | |
DVLevine | 0:9a73d45a17de | 43 | // all LEDs on & 0 PWM |
DVLevine | 0:9a73d45a17de | 44 | clear(); // set each led to 0 PWM |
DVLevine | 0:9a73d45a17de | 45 | |
DVLevine | 0:9a73d45a17de | 46 | for (uint8_t f=0; f<8; f++) { |
DVLevine | 0:9a73d45a17de | 47 | for (uint8_t i=0; i<=0x11; i++) |
DVLevine | 0:9a73d45a17de | 48 | writeRegister8(f, i, 0xff); // each 8 LEDs on |
DVLevine | 0:9a73d45a17de | 49 | } |
DVLevine | 0:9a73d45a17de | 50 | |
DVLevine | 0:9a73d45a17de | 51 | audioSync(false); |
DVLevine | 0:9a73d45a17de | 52 | |
DVLevine | 0:9a73d45a17de | 53 | return true; |
DVLevine | 0:9a73d45a17de | 54 | } |
DVLevine | 0:9a73d45a17de | 55 | |
DVLevine | 0:9a73d45a17de | 56 | void Adafruit_IS31FL3731::clear(void) { |
DVLevine | 0:9a73d45a17de | 57 | // all LEDs on & 0 PWM |
DVLevine | 0:9a73d45a17de | 58 | |
DVLevine | 0:9a73d45a17de | 59 | selectBank(_frame); |
DVLevine | 0:9a73d45a17de | 60 | |
DVLevine | 0:9a73d45a17de | 61 | uint8_t nemo[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
DVLevine | 0:9a73d45a17de | 62 | |
DVLevine | 0:9a73d45a17de | 63 | for (uint8_t i=0; i<6; i++) { |
DVLevine | 0:9a73d45a17de | 64 | // Wire.beginTransmission(_i2caddr); |
DVLevine | 0:9a73d45a17de | 65 | // Wire.write((byte) 0x24+i*24); |
DVLevine | 0:9a73d45a17de | 66 | // write 24 bytes at once |
DVLevine | 0:9a73d45a17de | 67 | myI2C->writeBytes(_i2caddr, 0x24+i*24, 24, nemo); |
DVLevine | 0:9a73d45a17de | 68 | |
DVLevine | 0:9a73d45a17de | 69 | /* for (uint8_t j=0; j<24; j++) { |
DVLevine | 0:9a73d45a17de | 70 | // Wire.write((byte) 0); |
DVLevine | 0:9a73d45a17de | 71 | myI2C->writeByte(_i2caddr, (unsigned char)0x24+i*24, (unsigned char)0); |
DVLevine | 0:9a73d45a17de | 72 | |
DVLevine | 0:9a73d45a17de | 73 | }*/ |
DVLevine | 0:9a73d45a17de | 74 | // Wire.endTransmission(); |
DVLevine | 0:9a73d45a17de | 75 | } |
DVLevine | 0:9a73d45a17de | 76 | } |
DVLevine | 0:9a73d45a17de | 77 | |
DVLevine | 0:9a73d45a17de | 78 | void Adafruit_IS31FL3731::setLEDPWM(uint8_t lednum, uint8_t pwm, uint8_t bank) { |
DVLevine | 0:9a73d45a17de | 79 | if (lednum >= 144) return; |
DVLevine | 0:9a73d45a17de | 80 | writeRegister8(bank, 0x24+lednum, pwm); |
DVLevine | 0:9a73d45a17de | 81 | } |
DVLevine | 0:9a73d45a17de | 82 | |
DVLevine | 0:9a73d45a17de | 83 | |
DVLevine | 0:9a73d45a17de | 84 | void Adafruit_IS31FL3731_Wing::drawPixel(int16_t x, int16_t y, uint16_t color) { |
DVLevine | 0:9a73d45a17de | 85 | // check rotation, move pixel around if necessary |
DVLevine | 0:9a73d45a17de | 86 | //myboss->printf("WING %i x %i y %i color\n",x,y,color); |
DVLevine | 0:9a73d45a17de | 87 | switch (getRotation()) { |
DVLevine | 0:9a73d45a17de | 88 | case 1: |
DVLevine | 0:9a73d45a17de | 89 | _swap_int16_t(x, y); |
DVLevine | 0:9a73d45a17de | 90 | x = 15 - x - 1; |
DVLevine | 0:9a73d45a17de | 91 | break; |
DVLevine | 0:9a73d45a17de | 92 | case 2: |
DVLevine | 0:9a73d45a17de | 93 | x = 15 - x - 1; |
DVLevine | 0:9a73d45a17de | 94 | y = 7 - y - 1; |
DVLevine | 0:9a73d45a17de | 95 | break; |
DVLevine | 0:9a73d45a17de | 96 | case 3: |
DVLevine | 0:9a73d45a17de | 97 | _swap_int16_t(x, y); |
DVLevine | 0:9a73d45a17de | 98 | y = 9 - y - 1; |
DVLevine | 0:9a73d45a17de | 99 | break; |
DVLevine | 0:9a73d45a17de | 100 | } |
DVLevine | 0:9a73d45a17de | 101 | |
DVLevine | 0:9a73d45a17de | 102 | // charlie wing is smaller: |
DVLevine | 0:9a73d45a17de | 103 | if ((x < 0) || (x >= 16) || (y < 0) || (y >= 7)) return; |
DVLevine | 0:9a73d45a17de | 104 | |
DVLevine | 0:9a73d45a17de | 105 | if (x > 7) { |
DVLevine | 0:9a73d45a17de | 106 | x=15-x; |
DVLevine | 0:9a73d45a17de | 107 | y += 8; |
DVLevine | 0:9a73d45a17de | 108 | } else { |
DVLevine | 0:9a73d45a17de | 109 | y = 7-y; |
DVLevine | 0:9a73d45a17de | 110 | } |
DVLevine | 0:9a73d45a17de | 111 | |
DVLevine | 0:9a73d45a17de | 112 | _swap_int16_t(x, y); |
DVLevine | 0:9a73d45a17de | 113 | |
DVLevine | 0:9a73d45a17de | 114 | if (color > 255) color = 255; // PWM 8bit max |
DVLevine | 0:9a73d45a17de | 115 | |
DVLevine | 0:9a73d45a17de | 116 | setLEDPWM(x + y*16, color, _frame); |
DVLevine | 0:9a73d45a17de | 117 | return; |
DVLevine | 0:9a73d45a17de | 118 | } |
DVLevine | 0:9a73d45a17de | 119 | |
DVLevine | 0:9a73d45a17de | 120 | |
DVLevine | 0:9a73d45a17de | 121 | |
DVLevine | 0:9a73d45a17de | 122 | void Adafruit_IS31FL3731::drawPixel(int16_t x, int16_t y, uint16_t color) { |
DVLevine | 0:9a73d45a17de | 123 | // check rotation, move pixel around if necessary |
DVLevine | 0:9a73d45a17de | 124 | // myboss->printf("%i x %i y %i color\n",x,y,color); |
DVLevine | 0:9a73d45a17de | 125 | switch (getRotation()) { |
DVLevine | 0:9a73d45a17de | 126 | case 1: |
DVLevine | 0:9a73d45a17de | 127 | _swap_int16_t(x, y); |
DVLevine | 0:9a73d45a17de | 128 | x = 16 - x - 1; |
DVLevine | 0:9a73d45a17de | 129 | break; |
DVLevine | 0:9a73d45a17de | 130 | case 2: |
DVLevine | 0:9a73d45a17de | 131 | x = 16 - x - 1; |
DVLevine | 0:9a73d45a17de | 132 | y = 9 - y - 1; |
DVLevine | 0:9a73d45a17de | 133 | break; |
DVLevine | 0:9a73d45a17de | 134 | case 3: |
DVLevine | 0:9a73d45a17de | 135 | _swap_int16_t(x, y); |
DVLevine | 0:9a73d45a17de | 136 | y = 9 - y - 1; |
DVLevine | 0:9a73d45a17de | 137 | break; |
DVLevine | 0:9a73d45a17de | 138 | } |
DVLevine | 0:9a73d45a17de | 139 | |
DVLevine | 0:9a73d45a17de | 140 | if ((x < 0) || (x >= 16)) return; |
DVLevine | 0:9a73d45a17de | 141 | if ((y < 0) || (y >= 9)) return; |
DVLevine | 0:9a73d45a17de | 142 | if (color > 255) color = 255; // PWM 8bit max |
DVLevine | 0:9a73d45a17de | 143 | |
DVLevine | 0:9a73d45a17de | 144 | setLEDPWM(x + y*16, color, _frame); |
DVLevine | 0:9a73d45a17de | 145 | return; |
DVLevine | 0:9a73d45a17de | 146 | } |
DVLevine | 0:9a73d45a17de | 147 | |
DVLevine | 0:9a73d45a17de | 148 | void Adafruit_IS31FL3731::setFrame(uint8_t f) { |
DVLevine | 0:9a73d45a17de | 149 | _frame = f; |
DVLevine | 0:9a73d45a17de | 150 | } |
DVLevine | 0:9a73d45a17de | 151 | |
DVLevine | 0:9a73d45a17de | 152 | void Adafruit_IS31FL3731::displayFrame(uint8_t f) { |
DVLevine | 0:9a73d45a17de | 153 | if (f > 7) f = 0; |
DVLevine | 0:9a73d45a17de | 154 | writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_PICTUREFRAME, f); |
DVLevine | 0:9a73d45a17de | 155 | } |
DVLevine | 0:9a73d45a17de | 156 | |
DVLevine | 0:9a73d45a17de | 157 | |
DVLevine | 0:9a73d45a17de | 158 | void Adafruit_IS31FL3731::selectBank(uint8_t b) { |
DVLevine | 0:9a73d45a17de | 159 | myI2C->writeByte(_i2caddr, ISSI_COMMANDREGISTER, b); |
DVLevine | 0:9a73d45a17de | 160 | /*Wire.beginTransmission(_i2caddr); |
DVLevine | 0:9a73d45a17de | 161 | Wire.write((byte)ISSI_COMMANDREGISTER); |
DVLevine | 0:9a73d45a17de | 162 | Wire.write(b); |
DVLevine | 0:9a73d45a17de | 163 | Wire.endTransmission();*/ |
DVLevine | 0:9a73d45a17de | 164 | } |
DVLevine | 0:9a73d45a17de | 165 | |
DVLevine | 0:9a73d45a17de | 166 | void Adafruit_IS31FL3731::audioSync(bool sync) { |
DVLevine | 0:9a73d45a17de | 167 | if (sync) { |
DVLevine | 0:9a73d45a17de | 168 | writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_AUDIOSYNC, 0x1); |
DVLevine | 0:9a73d45a17de | 169 | } else { |
DVLevine | 0:9a73d45a17de | 170 | writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_AUDIOSYNC, 0x0); |
DVLevine | 0:9a73d45a17de | 171 | } |
DVLevine | 0:9a73d45a17de | 172 | } |
DVLevine | 0:9a73d45a17de | 173 | |
DVLevine | 0:9a73d45a17de | 174 | /*************/ |
DVLevine | 0:9a73d45a17de | 175 | void Adafruit_IS31FL3731::writeRegister8(uint8_t b, uint8_t reg, uint8_t data) { |
DVLevine | 0:9a73d45a17de | 176 | selectBank(b); |
DVLevine | 0:9a73d45a17de | 177 | |
DVLevine | 0:9a73d45a17de | 178 | myI2C->writeByte(_i2caddr, reg, data); |
DVLevine | 0:9a73d45a17de | 179 | |
DVLevine | 0:9a73d45a17de | 180 | |
DVLevine | 0:9a73d45a17de | 181 | /*Wire.beginTransmission(_i2caddr); |
DVLevine | 0:9a73d45a17de | 182 | Wire.write((byte)reg); |
DVLevine | 0:9a73d45a17de | 183 | Wire.write((byte)data); |
DVLevine | 0:9a73d45a17de | 184 | Wire.endTransmission();*/ |
DVLevine | 0:9a73d45a17de | 185 | //Serial.print("$"); Serial.print(reg, HEX); |
DVLevine | 0:9a73d45a17de | 186 | //Serial.print(" = 0x"); Serial.println(data, HEX); |
DVLevine | 0:9a73d45a17de | 187 | } |
DVLevine | 0:9a73d45a17de | 188 | |
DVLevine | 0:9a73d45a17de | 189 | uint8_t Adafruit_IS31FL3731::readRegister8(uint8_t bank, uint8_t reg) { |
DVLevine | 0:9a73d45a17de | 190 | uint8_t x; |
DVLevine | 0:9a73d45a17de | 191 | uint8_t status; |
DVLevine | 0:9a73d45a17de | 192 | |
DVLevine | 0:9a73d45a17de | 193 | selectBank(bank); |
DVLevine | 0:9a73d45a17de | 194 | /* |
DVLevine | 0:9a73d45a17de | 195 | Wire.beginTransmission(_i2caddr); |
DVLevine | 0:9a73d45a17de | 196 | Wire.write((byte)reg); |
DVLevine | 0:9a73d45a17de | 197 | Wire.endTransmission(); |
DVLevine | 0:9a73d45a17de | 198 | |
DVLevine | 0:9a73d45a17de | 199 | Wire.beginTransmission(_i2caddr); |
DVLevine | 0:9a73d45a17de | 200 | Wire.requestFrom(_i2caddr, (byte)1); |
DVLevine | 0:9a73d45a17de | 201 | x = Wire.read();*/ |
DVLevine | 0:9a73d45a17de | 202 | |
DVLevine | 0:9a73d45a17de | 203 | status = myI2C->readByte(_i2caddr, reg, &x); |
DVLevine | 0:9a73d45a17de | 204 | |
DVLevine | 0:9a73d45a17de | 205 | //Wire.endTransmission(); |
DVLevine | 0:9a73d45a17de | 206 | |
DVLevine | 0:9a73d45a17de | 207 | // Serial.print("$"); Serial.print(reg, HEX); |
DVLevine | 0:9a73d45a17de | 208 | // Serial.print(": 0x"); Serial.println(x, HEX); |
DVLevine | 0:9a73d45a17de | 209 | |
DVLevine | 0:9a73d45a17de | 210 | return x; |
DVLevine | 0:9a73d45a17de | 211 | } |