-
Notifications
You must be signed in to change notification settings - Fork 26
/
Adafruit_IS31FL3731.cpp
273 lines (234 loc) · 8.44 KB
/
Adafruit_IS31FL3731.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <Adafruit_IS31FL3731.h>
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) \
{ \
int16_t t = a; \
a = b; \
b = t; \
}
#endif
/**************************************************************************/
/*!
@brief Constructor for breakout version
@param width Desired width of led display
@param height Desired height of led display
*/
/**************************************************************************/
Adafruit_IS31FL3731::Adafruit_IS31FL3731(uint8_t width, uint8_t height)
: Adafruit_GFX(width, height) {}
/**************************************************************************/
/*!
@brief Constructor for FeatherWing version (15x7 LEDs)
*/
/**************************************************************************/
Adafruit_IS31FL3731_Wing::Adafruit_IS31FL3731_Wing(void)
: Adafruit_IS31FL3731(15, 7) {}
/**************************************************************************/
/*!
@brief Initialize hardware and clear display
@param addr The I2C address we expect to find the chip at
@param theWire The TwoWire I2C bus device to use, defaults to &Wire
@returns True on success, false if chip isnt found
*/
/**************************************************************************/
bool Adafruit_IS31FL3731::begin(uint8_t addr, TwoWire *theWire) {
if (_i2c_dev) {
delete _i2c_dev;
}
_i2c_dev = new Adafruit_I2CDevice(addr, theWire);
if (!_i2c_dev->begin()) {
return false;
}
_i2c_dev->setSpeed(400000);
_frame = 0;
// shutdown
writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
delay(10);
// out of shutdown
writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
// picture mode
writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_CONFIG,
ISSI_REG_CONFIG_PICTUREMODE);
displayFrame(_frame);
// all LEDs on & 0 PWM
clear(); // set each led to 0 PWM
for (uint8_t f = 0; f < 8; f++) {
for (uint8_t i = 0; i <= 0x11; i++)
writeRegister8(f, i, 0xff); // each 8 LEDs on
}
audioSync(false);
return true;
}
/**************************************************************************/
/*!
@brief Sets all LEDs on & 0 PWM for current frame.
*/
/**************************************************************************/
void Adafruit_IS31FL3731::clear(void) {
selectBank(_frame);
uint8_t erasebuf[25];
memset(erasebuf, 0, 25);
for (uint8_t i = 0; i < 6; i++) {
erasebuf[0] = 0x24 + i * 24;
_i2c_dev->write(erasebuf, 25);
}
}
/**************************************************************************/
/*!
@brief Low level accesssor - sets a 8-bit PWM pixel value to a bank location
does not handle rotation, x/y or any rearrangements!
@param lednum The offset into the bank that corresponds to the LED
@param bank The bank/frame we will set the data in
@param pwm brightnes, from 0 (off) to 255 (max on)
*/
/**************************************************************************/
void Adafruit_IS31FL3731::setLEDPWM(uint8_t lednum, uint8_t pwm, uint8_t bank) {
if (lednum >= 144)
return;
writeRegister8(bank, 0x24 + lednum, pwm);
}
/**************************************************************************/
/*!
@brief Adafruit GFX low level accesssor - sets a 8-bit PWM pixel value
handles rotation and pixel arrangement, unlike setLEDPWM
@param x The x position, starting with 0 for left-most side
@param y The y position, starting with 0 for top-most side
@param color Despite being a 16-bit value, takes 0 (off) to 255 (max on)
*/
/**************************************************************************/
void Adafruit_IS31FL3731_Wing::drawPixel(int16_t x, int16_t y, uint16_t color) {
// check rotation, move pixel around if necessary
switch (getRotation()) {
case 1:
_swap_int16_t(x, y);
x = 15 - x - 1;
break;
case 2:
x = 15 - x - 1;
y = 7 - y - 1;
break;
case 3:
_swap_int16_t(x, y);
y = 9 - y - 1;
break;
}
// charlie wing is smaller:
if ((x < 0) || (x >= 16) || (y < 0) || (y >= 7))
return;
if (x > 7) {
x = 15 - x;
y += 8;
} else {
y = 7 - y;
}
_swap_int16_t(x, y);
if (color > 255)
color = 255; // PWM 8bit max
setLEDPWM(x + y * 16, color, _frame);
return;
}
/**************************************************************************/
/*!
@brief Adafruit GFX low level accesssor - sets a 8-bit PWM pixel value
handles rotation and pixel arrangement, unlike setLEDPWM
@param x The x position, starting with 0 for left-most side
@param y The y position, starting with 0 for top-most side
@param color Despite being a 16-bit value, takes 0 (off) to 255 (max on)
*/
/**************************************************************************/
void Adafruit_IS31FL3731::drawPixel(int16_t x, int16_t y, uint16_t color) {
// check rotation, move pixel around if necessary
switch (getRotation()) {
case 1:
_swap_int16_t(x, y);
x = 16 - x - 1;
break;
case 2:
x = 16 - x - 1;
y = 9 - y - 1;
break;
case 3:
_swap_int16_t(x, y);
y = 9 - y - 1;
break;
}
if ((x < 0) || (x >= 16))
return;
if ((y < 0) || (y >= 9))
return;
if (color > 255)
color = 255; // PWM 8bit max
setLEDPWM(x + y * 16, color, _frame);
return;
}
/**************************************************************************/
/*!
@brief Set's this object's frame tracker (does not talk to the chip)
@param frame Ranges from 0 - 7 for the 8 frames
*/
/**************************************************************************/
void Adafruit_IS31FL3731::setFrame(uint8_t frame) { _frame = frame; }
/**************************************************************************/
/*!
@brief Have the chip set the display to the contents of a frame
@param frame Ranges from 0 - 7 for the 8 frames
*/
/**************************************************************************/
void Adafruit_IS31FL3731::displayFrame(uint8_t frame) {
if (frame > 7)
frame = 0;
writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_PICTUREFRAME, frame);
}
/**************************************************************************/
/*!
@brief Switch to a given bank in the chip memory for future reads
@param bank The IS31 bank to switch to
@returns False if I2C command failed to be ack'd
*/
/**************************************************************************/
bool Adafruit_IS31FL3731::selectBank(uint8_t bank) {
uint8_t cmd[2] = {ISSI_COMMANDREGISTER, bank};
return _i2c_dev->write(cmd, 2);
}
/**************************************************************************/
/*!
@brief Enable the audio 'sync' for brightness pulsing (not really tested)
@param sync True to enable, False to disable
*/
/**************************************************************************/
void Adafruit_IS31FL3731::audioSync(bool sync) {
if (sync) {
writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_AUDIOSYNC, 0x1);
} else {
writeRegister8(ISSI_BANK_FUNCTIONREG, ISSI_REG_AUDIOSYNC, 0x0);
}
}
/**************************************************************************/
/*!
@brief Write one byte to a register located in a given bank
@param bank The IS31 bank to write the register location
@param reg the offset into the bank to write
@param data The byte value
@returns False if I2C command failed to be ack'd
*/
/**************************************************************************/
bool Adafruit_IS31FL3731::writeRegister8(uint8_t bank, uint8_t reg,
uint8_t data) {
selectBank(bank);
uint8_t cmd[2] = {reg, data};
return _i2c_dev->write(cmd, 2);
}
/**************************************************************************/
/*!
@brief Read one byte from a register located in a given bank
@param bank The IS31 bank to read the register location
@param reg the offset into the bank to read
@return 1 byte value
*/
/**************************************************************************/
uint8_t Adafruit_IS31FL3731::readRegister8(uint8_t bank, uint8_t reg) {
uint8_t val = 0xFF;
selectBank(bank);
_i2c_dev->write_then_read(®, 1, &val, 1);
return val;
}