-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRMTT_Matrix.cpp
555 lines (500 loc) · 15.7 KB
/
RMTT_Matrix.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include <Arduino.h>
#include <Wire.h>
#include "RMTT_Libs.h"
// This file is copied from github.
// https://github.com/kkostyan/is31fl3733.git
static IS31FL3733 LEDMatrix;
uint8_t I2CWriteMulti(uint8_t address, uint8_t reg, uint8_t *src, uint8_t count);
uint8_t I2CReadMulti(uint8_t address, uint8_t reg, uint8_t *dst, uint8_t count);
void RMTT_Matrix::Init(uint8_t gcc)
{
LEDMatrix.address = 0x50;
LEDMatrix.i2c_write_reg = I2CWriteMulti;
LEDMatrix.i2c_read_reg = I2CReadMulti;
#ifdef __RMTT_MATRIX_IIC_DEBUG__
Serial.printf("Ardress 0x%x \r\n", LEDMatrix.address);
#endif
IS31FL3733_Init(&LEDMatrix);
// Set Global Current Control.
IS31FL3733_SetGCC(&LEDMatrix, gcc);
IS31FL3733_SetLEDState (&LEDMatrix, IS31FL3733_CS, IS31FL3733_SW, IS31FL3733_LED_STATE_ON);
}
void RMTT_Matrix::SetGCC(uint8_t gcc)
{
RMTT_I2C_BUSY_LOCK();
// Set Global Current Control.
IS31FL3733_SetGCC(&LEDMatrix, gcc);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::SetLEDStatus(uint8_t cs, uint8_t sw, IS31FL3733_LED_STATE state)
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_SetLEDState (&LEDMatrix, cs, sw, state);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::SetLEDPWM(uint8_t cs, uint8_t sw, uint8_t value)
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_SetLEDPWM (&LEDMatrix, cs, sw, value);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::SetAllPWM(uint8_t *val)
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_SetPWM (&LEDMatrix, (uint8_t*)val);
RMTT_I2C_BUSY_UNLOCK();
}
uint8_t RMTT_Matrix::ReadCommonReg (uint8_t reg_addr)
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_ReadCommonReg (&LEDMatrix, reg_addr);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::WriteCommonReg (uint8_t reg_addr, uint8_t reg_value)
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_WriteCommonReg (&LEDMatrix, reg_addr, reg_value);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::SetLEDMode (uint8_t cs, uint8_t sw, IS31FL3733_LED_MODE mode)
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_SetLEDMode (&LEDMatrix, cs, sw, mode);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::ConfigABM (IS31FL3733_ABM_NUM n, IS31FL3733_ABM *config)
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_ConfigABM (&LEDMatrix, n, config);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::StartABM ()
{
RMTT_I2C_BUSY_LOCK();
IS31FL3733_StartABM (&LEDMatrix);
RMTT_I2C_BUSY_UNLOCK();
}
void RMTT_Matrix::On()
{
pinMode(5,OUTPUT);
digitalWrite(5, HIGH);
}
void RMTT_Matrix::Off()
{
pinMode(5,OUTPUT);
digitalWrite(5, LOW);
}
/**************************IS31FL3733 C Driver***********************************************/
// Write an arbitrary number of bytes from the given array to the sensor,
// starting at the given register
uint8_t I2CWriteMulti(uint8_t address, uint8_t reg, uint8_t *src, uint8_t count)
{
uint8_t I2C_status = 0;
#ifdef __RMTT_MATRIX_IIC_DEBUG__
Serial.printf("I2C Write Ardress 0x%x Reg: 0x%x count: 0x%x\r\n", address, reg, count);
#endif
Wire.beginTransmission(address);
Wire.write(reg);
while (count-- > 0)
{
#ifdef __RMTT_MATRIX_IIC_DEBUG__
Serial.printf("0x%x ", *src);
#endif
Wire.write(*(src++));
}
#ifdef __RMTT_MATRIX_IIC_DEBUG__
Serial.println();
#endif
I2C_status = Wire.endTransmission();
#ifdef __RMTT_MATRIX_IIC_DEBUG__
Serial.printf("I2C_status %d \r\n", I2C_status);
#endif
return I2C_status;
}
// Read an arbitrary number of bytes from the sensor, starting at the given
// register, into the given array
uint8_t I2CReadMulti(uint8_t address, uint8_t reg, uint8_t *dst, uint8_t count)
{
uint8_t I2C_status = 0;
#ifdef __RMTT_MATRIX_IIC_DEBUG__
uint8_t read_len = count;
uint8_t *read_ptr = dst;
Serial.printf("I2C Read Ardress 0x%x Reg: 0x%x count: 0x%x\r\n", address, reg, count);
#endif
Wire.beginTransmission(address);
Wire.write(reg);
I2C_status = Wire.endTransmission();
Wire.requestFrom(address, count);
while (count-- > 0)
{
*(dst++) = Wire.read();
}
#ifdef __RMTT_MATRIX_IIC_DEBUG__
for(int i = 0; i < read_len; i++)
{
Serial.printf("0x%x ", read_ptr[i]);
}
Serial.println();
Serial.printf("I2C_status %d \r\n", I2C_status);
#endif
return I2C_status;
}
uint8_t IS31FL3733_ReadCommonReg (IS31FL3733 *device, uint8_t reg_addr)
{
uint8_t reg_value;
// Read value from register.
device->i2c_read_reg (device->address, reg_addr, ®_value, sizeof(uint8_t));
// Return register value.
return reg_value;
}
void IS31FL3733_WriteCommonReg (IS31FL3733 *device, uint8_t reg_addr, uint8_t reg_value)
{
// Write value to register.
device->i2c_write_reg (device->address, reg_addr, ®_value, sizeof(uint8_t));
}
void IS31FL3733_SelectPage (IS31FL3733 *device, uint8_t page)
{
// Unlock Command Register.
IS31FL3733_WriteCommonReg (device, IS31FL3733_PSWL, IS31FL3733_PSWL_ENABLE);
// Select requested page in Command Register.
IS31FL3733_WriteCommonReg (device, IS31FL3733_PSR, page);
}
uint8_t IS31FL3733_ReadPagedReg (IS31FL3733 *device, uint16_t reg_addr)
{
uint8_t reg_value;
// Select register page.
IS31FL3733_SelectPage (device, IS31FL3733_GET_PAGE(reg_addr));
// Read value from register.
device->i2c_read_reg (device->address, IS31FL3733_GET_ADDR(reg_addr), ®_value, sizeof(uint8_t));
// Return register value.
return reg_value;
}
void IS31FL3733_WritePagedReg (IS31FL3733 *device, uint16_t reg_addr, uint8_t reg_value)
{
// Select register page.
IS31FL3733_SelectPage (device, IS31FL3733_GET_PAGE(reg_addr));
// Write value to register.
device->i2c_write_reg (device->address, IS31FL3733_GET_ADDR(reg_addr), ®_value, sizeof(uint8_t));
}
void IS31FL3733_WritePagedRegs (IS31FL3733 *device, uint16_t reg_addr, uint8_t *values, uint8_t count)
{
// Select registers page.
IS31FL3733_SelectPage (device, IS31FL3733_GET_PAGE(reg_addr));
// Write values to registers.
device->i2c_write_reg (device->address, IS31FL3733_GET_ADDR(reg_addr), values, count);
}
void IS31FL3733_Init (IS31FL3733 *device)
{
// Read reset register to reset device.
IS31FL3733_ReadPagedReg (device, IS31FL3733_RESET);
// Clear software reset in configuration register.
IS31FL3733_WritePagedReg (device, IS31FL3733_CR, IS31FL3733_CR_SSD);
// Clear state of all LEDs in internal buffer and sync buffer to device.
IS31FL3733_SetLEDState (device, IS31FL3733_CS, IS31FL3733_SW, IS31FL3733_LED_STATE_OFF);
}
void IS31FL3733_SetGCC (IS31FL3733 *device, uint8_t gcc)
{
// Write gcc value to Global Current Control (GCC) register.
IS31FL3733_WritePagedReg (device, IS31FL3733_GCC, gcc);
}
void IS31FL3733_SetSWPUR (IS31FL3733 *device, IS31FL3733_RESISTOR resistor)
{
// Write resistor value to SWPUR register.
IS31FL3733_WritePagedReg (device, IS31FL3733_SWPUR, resistor);
}
void IS31FL3733_SetCSPDR (IS31FL3733 *device, IS31FL3733_RESISTOR resistor)
{
// Write resistor value to CSPDR register.
IS31FL3733_WritePagedReg (device, IS31FL3733_CSPDR, resistor);
}
void IS31FL3733_SetLEDState (IS31FL3733 *device, uint8_t cs, uint8_t sw, IS31FL3733_LED_STATE state)
{
uint8_t offset;
// Check SW boundaries.
if (sw < IS31FL3733_SW)
{
// Check CS boundaries.
if (cs < IS31FL3733_CS)
{
// Set state of individual LED.
// Calculate LED bit offset.
offset = (sw << 1) + (cs / 8);
// Update state of LED in internal buffer.
if (state == IS31FL3733_LED_STATE_OFF)
{
// Clear bit for selected LED.
device->leds[offset] &= ~(0x01 << (cs % 8));
}
else
{
// Set bit for selected LED.
device->leds[offset] |= 0x01 << (cs % 8);
}
// Write updated LED state to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDONOFF + offset, device->leds[offset]);
}
else
{
// Set state of full row selected by SW.
// Calculate row offset.
offset = sw << 1;
// Update state of row LEDs in internal buffer.
if (state == IS31FL3733_LED_STATE_OFF)
{
// Clear 16 bits for selected row LEDs.
device->leds[offset ] = 0x00;
device->leds[offset + 1] = 0x00;
}
else
{
// Set 16 bits for selected row LEDs.
device->leds[offset ] = 0xFF;
device->leds[offset + 1] = 0xFF;
}
// Write updated LEDs state to device registers.
IS31FL3733_WritePagedRegs (device, IS31FL3733_LEDONOFF + offset, &device->leds[offset], IS31FL3733_CS / 8);
}
}
else
{
// Check CS boundaries.
if (cs < IS31FL3733_CS)
{
// Set state of full column selected by CS.
for (sw = 0; sw < IS31FL3733_SW; sw++)
{
// Calculate LED bit offset.
offset = (sw << 1) + (cs / 8);
// Update state of LED in internal buffer.
if (state == IS31FL3733_LED_STATE_OFF)
{
// Clear bit for selected LED.
device->leds[offset] &= ~(0x01 << (cs % 8));
}
else
{
// Set bit for selected LED.
device->leds[offset] |= 0x01 << (cs % 8);
}
// Write updated LED state to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDONOFF + offset, device->leds[offset]);
}
}
else
{
// Set state of all LEDs.
for (sw = 0; sw < IS31FL3733_SW; sw++)
{
// Update state of all LEDs in internal buffer.
if (state == IS31FL3733_LED_STATE_OFF)
{
// Clear all bits.
device->leds[(sw << 1) ] = 0x00;
device->leds[(sw << 1) + 1] = 0x00;
}
else
{
// Set all bits.
device->leds[(sw << 1) ] = 0xFF;
device->leds[(sw << 1) + 1] = 0xFF;
}
}
// Write updated LEDs state to device registers.
IS31FL3733_WritePagedRegs (device, IS31FL3733_LEDONOFF, device->leds, IS31FL3733_SW * IS31FL3733_CS / 8);
}
}
}
void IS31FL3733_SetLEDPWM (IS31FL3733 *device, uint8_t cs, uint8_t sw, uint8_t value)
{
uint8_t offset;
// Check SW boundaries.
if (sw < IS31FL3733_SW)
{
// Check CS boundaries.
if (cs < IS31FL3733_CS)
{
// Set PWM of individual LED.
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED PWM value to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDPWM + offset, value);
}
else
{
// Set PWM of full row selected by SW.
for (cs = 0; cs < IS31FL3733_CS; cs++)
{
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED PWM value to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDPWM + offset, value);
}
}
}
else
{
// Check CS boundaries.
if (cs < IS31FL3733_CS)
{
// Set PWM of full column selected by CS.
for (sw = 0; sw < IS31FL3733_SW; sw++)
{
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED PWM value to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDPWM + offset, value);
}
}
else
{
// Set PWM of all LEDs.
for (sw = 0; sw < IS31FL3733_SW; sw++)
{
for (cs = 0; cs < IS31FL3733_CS; cs++)
{
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED PWM value to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDPWM + offset, value);
}
}
}
}
}
IS31FL3733_LED_STATUS IS31FL3733_GetLEDStatus (IS31FL3733 *device, uint8_t cs, uint8_t sw)
{
uint8_t offset;
// Check CS and SW boundaries.
if ((cs < IS31FL3733_CS) && (sw < IS31FL3733_SW))
{
// Calculate LED bit offset.
offset = (sw << 1) + (cs / 8);
// Get Open status from device register.
if (IS31FL3733_ReadPagedReg (device, IS31FL3733_LEDOPEN + offset) & (0x01 << (cs % 8)))
{
return IS31FL3733_LED_STATUS_OPEN;
}
// Get Short status from device register.
if (IS31FL3733_ReadPagedReg (device, IS31FL3733_LEDSHORT + offset) & (0x01 << (cs % 8)))
{
return IS31FL3733_LED_STATUS_SHORT;
}
}
else
{
// Unknown status for nonexistent LED.
return IS31FL3733_LED_STATUS_UNKNOWN;
}
return IS31FL3733_LED_STATUS_NORMAL;
}
void IS31FL3733_SetState (IS31FL3733 *device, uint8_t *states)
{
uint8_t sw;
uint8_t cs;
uint8_t offset;
// Set state of all LEDs.
for (sw = 0; sw < IS31FL3733_SW; sw++)
{
for (cs = 0; cs < IS31FL3733_CS; cs++)
{
// Calculate LED bit offset.
offset = (sw << 1) + (cs / 8);
// Update state of LED in internal buffer.
if (states[sw * IS31FL3733_CS + cs] == 0)
{
// Clear bit for selected LED.
device->leds[offset] &= ~(0x01 << (cs % 8));
}
else
{
// Set bit for selected LED.
device->leds[offset] |= 0x01 << (cs % 8);
}
}
}
// Write updated LEDs state to device registers.
IS31FL3733_WritePagedRegs (device, IS31FL3733_LEDONOFF, device->leds, IS31FL3733_SW * IS31FL3733_CS / 8);
}
void IS31FL3733_SetPWM (IS31FL3733 *device, uint8_t *values)
{
// Write LED PWM values to device registers.
IS31FL3733_WritePagedRegs (device, IS31FL3733_LEDPWM, values, IS31FL3733_SW * IS31FL3733_CS / 2);
IS31FL3733_WritePagedRegs (device, IS31FL3733_LEDPWM + IS31FL3733_SW * IS31FL3733_CS / 2, values + IS31FL3733_SW * IS31FL3733_CS / 2, IS31FL3733_SW * IS31FL3733_CS / 2);
}
void IS31FL3733_SetLEDMode (IS31FL3733 *device, uint8_t cs, uint8_t sw, IS31FL3733_LED_MODE mode)
{
uint8_t offset;
// Check SW boundaries.
if (sw < IS31FL3733_SW)
{
// Check CS boundaries.
if (cs < IS31FL3733_CS)
{
// Set mode of individual LED.
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED mode to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDABM + offset, mode);
}
else
{
// Set mode of full row selected by SW.
for (cs = 0; cs < IS31FL3733_CS; cs++)
{
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED mode to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDABM + offset, mode);
}
}
}
else
{
// Check CS boundaries.
if (cs < IS31FL3733_CS)
{
// Set mode of full column selected by CS.
for (sw = 0; sw < IS31FL3733_SW; sw++)
{
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED mode to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDABM + offset, mode);
}
}
else
{
// Set mode of all LEDs.
for (sw = 0; sw < IS31FL3733_SW; sw++)
{
for (cs = 0; cs < IS31FL3733_CS; cs++)
{
// Calculate LED offset.
offset = sw * IS31FL3733_CS + cs;
// Write LED mode to device register.
IS31FL3733_WritePagedReg (device, IS31FL3733_LEDABM + offset, mode);
}
}
}
}
}
void IS31FL3733_ConfigABM (IS31FL3733 *device, IS31FL3733_ABM_NUM n, IS31FL3733_ABM *config)
{
// Set fade in and fade out time.
IS31FL3733_WritePagedReg (device, n, config->T1 | config->T2);
// Set hold and off time.
IS31FL3733_WritePagedReg (device, n + 1, config->T3 | config->T4);
// Set loop begin/end time and high part of loop times.
IS31FL3733_WritePagedReg (device, n + 2, config->Tend | config->Tbegin | ((config->Times >> 8) & 0x0F));
// Set low part of loop times.
IS31FL3733_WritePagedReg (device, n + 3, config->Times & 0xFF);
}
void IS31FL3733_StartABM (IS31FL3733 *device)
{
// Clear B_EN bit in configuration register.
IS31FL3733_WritePagedReg (device, IS31FL3733_CR, IS31FL3733_CR_SSD);
// Set B_EN bit in configuration register.
IS31FL3733_WritePagedReg (device, IS31FL3733_CR, IS31FL3733_CR_BEN | IS31FL3733_CR_SSD);
// Write 0x00 to Time Update Register to update ABM settings.
IS31FL3733_WritePagedReg (device, IS31FL3733_TUR, 0x00);
}