-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarLED.cpp
421 lines (355 loc) · 10.9 KB
/
BarLED.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
/******************************************************************************
===============================================================================
===============================================================================
10 Segment Bar LED Library - Implementation C File
BarLED.cpp
Version 1.0.0
by Chris Formeister
COPYRIGHT (c) 2024, FRC Industries & Chris Formeister. All Rights Reserved.
==============================================================================
==============================================================================
******************************************************************************/
#include "BarLED.h"
// Constructor
BarLED::BarLED() {
memset(&_config, 0, sizeof(ChipConfig));
_config.patternSpeed = 100; // Default 100ms
_config.brightness = 255; // Full brightness
_config.debugMode = false;
}
// Destructor
BarLED::~BarLED() {
if(_config.states) {
free(_config.states);
_config.states = nullptr;
}
}
// Initialize the LED driver
uint8_t BarLED::begin(uint8_t dataPin, uint8_t clockPin, uint8_t latchPin,
uint8_t type, uint8_t pinsPerChip, uint8_t numberOfChips) {
// Validate parameters
if(type > TYPE_WS2803) {
setError(ERROR_INVALID_CHIP);
return ERROR_INVALID_CHIP;
}
if(pinsPerChip == 0 || numberOfChips == 0) {
setError(ERROR_INVALID_PINS);
return ERROR_INVALID_PINS;
}
// Store configuration
_config.dataPin = dataPin;
_config.clockPin = clockPin;
_config.latchPin = latchPin;
_config.deviceType = type;
_config.numPins = pinsPerChip;
_config.numChips = numberOfChips;
// Allocate state memory
if(_config.states) {
free(_config.states);
}
_config.states = (uint16_t*)malloc(numberOfChips * sizeof(uint16_t));
if(!_config.states) {
return ERROR_INVALID_PINS;
}
memset(_config.states, 0, numberOfChips * sizeof(uint16_t));
// Configure pins
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(latchPin, LOW);
// Initial chip setup based on type
switch(type) {
case TYPE_WS2803:
// WS2803 needs a reset pulse
digitalWrite(latchPin, HIGH);
delayMicroseconds(500);
digitalWrite(latchPin, LOW);
break;
}
return ERROR_NONE;
}
// Set LED states or pattern
void BarLED::setBars(uint32_t pinCombination, uint8_t pattern) {
if(pattern > PATTERN_ALTERNATING) {
setError(ERROR_INVALID_PATTERN);
return;
}
if(pattern != PATTERN_NONE) {
_config.currentPattern = pattern;
_config.patternStep = 0;
_config.repeatCount = 0;
_config.lastUpdate = millis();
memset(_config.states, 0, _config.numChips * sizeof(uint16_t));
} else {
// Process direct LED control
_config.currentPattern = PATTERN_NONE;
memset(_config.states, 0, _config.numChips * sizeof(uint16_t));
// Handle single pin
if(pinCombination < (_config.numPins * _config.numChips + 1)) {
if(pinCombination > 0) {
uint8_t chip = (pinCombination - 1) / _config.numPins;
uint8_t pin = (pinCombination - 1) % _config.numPins;
_config.states[chip] |= (1 << pin);
}
}
// Handle combinations
else {
while(pinCombination > 0) {
uint8_t digit = pinCombination % 10;
if(digit > 0 && digit <= (_config.numPins * _config.numChips)) {
uint8_t chip = (digit - 1) / _config.numPins;
uint8_t pin = (digit - 1) % _config.numPins;
_config.states[chip] |= (1 << pin);
}
pinCombination /= 10;
}
}
}
sendData();
}
// Clear all LEDs
void BarLED::clearAll() {
_config.currentPattern = PATTERN_NONE;
memset(_config.states, 0, _config.numChips * sizeof(uint16_t));
sendData();
}
// Set all LEDs
void BarLED::setAll() {
_config.currentPattern = PATTERN_NONE;
for(uint8_t i = 0; i < _config.numChips; i++) {
_config.states[i] = 0xFFFF;
}
sendData();
}
// Update pattern animation
void BarLED::update() {
if(_config.currentPattern == PATTERN_NONE || _config.patternPaused) {
return;
}
uint32_t now = millis();
if(now - _config.lastUpdate < _config.patternSpeed) {
return;
}
_config.lastUpdate = now;
updatePattern();
}
// Set pattern animation speed
void BarLED::setPatternSpeed(uint16_t speedMs) {
_config.patternSpeed = speedMs;
}
// Stop current pattern
void BarLED::stopPattern() {
_config.currentPattern = PATTERN_NONE;
memset(_config.states, 0, _config.numChips * sizeof(uint16_t));
sendData();
}
// Pause pattern
void BarLED::pausePattern() {
_config.patternPaused = true;
}
// Resume pattern
void BarLED::resumePattern() {
_config.patternPaused = false;
_config.lastUpdate = millis();
}
// Set pattern repeat count
void BarLED::setPatternRepeat(uint8_t times) {
_config.maxRepeats = times;
_config.repeatCount = 0;
}
// Check if pattern is complete
bool BarLED::isPatternComplete() {
return _config.currentPattern == PATTERN_NONE ||
(_config.maxRepeats > 0 && _config.repeatCount >= _config.maxRepeats);
}
// Get current pattern
uint8_t BarLED::getCurrentPattern() {
return _config.currentPattern;
}
// Set LED brightness
void BarLED::setBrightness(uint8_t brightness) {
_config.brightness = brightness;
if(_config.deviceType == TYPE_MBI5027 || _config.deviceType == TYPE_WS2803) {
sendData();
}
}
// Get last error
uint8_t BarLED::getLastError() {
return _config.lastError;
}
// Set debug mode
void BarLED::setDebugMode(bool enable) {
_config.debugMode = enable;
}
// Private: Send data to chips
void BarLED::sendData() {
digitalWrite(_config.latchPin, LOW);
for(int chip = _config.numChips - 1; chip >= 0; chip--) {
switch(_config.deviceType) {
case TYPE_74HC595:
shiftOut(_config.dataPin, _config.clockPin, MSBFIRST, (_config.states[chip] >> 8) & 0xFF);
shiftOut(_config.dataPin, _config.clockPin, MSBFIRST, _config.states[chip] & 0xFF);
break;
case TYPE_MBI5027:
shiftBits(_config.states[chip], 16);
break;
case TYPE_WS2803:
// WS2803 needs specific timing
for(int i = 17; i >= 0; i--) {
digitalWrite(_config.dataPin, (_config.states[chip] >> i) & 1);
digitalWrite(_config.clockPin, HIGH);
delayMicroseconds(1); // WS2803 timing requirement
digitalWrite(_config.clockPin, LOW);
}
break;
}
}
// Handle chip-specific latch timing
switch(_config.deviceType) {
case TYPE_WS2803:
delayMicroseconds(500);
break;
}
digitalWrite(_config.latchPin, HIGH);
}
// Private: Update pattern animation
void BarLED::updatePattern() {
if(_config.currentPattern == PATTERN_NONE) return;
memset(_config.states, 0, _config.numChips * sizeof(uint16_t));
uint16_t patternState = generatePatternState(_config.currentPattern, _config.patternStep);
// Distribute pattern state across chips
for(uint8_t chip = 0; chip < _config.numChips; chip++) {
_config.states[chip] = (patternState >> (chip * _config.numPins)) & ((1 << _config.numPins) - 1);
}
sendData();
_config.patternStep++;
uint16_t maxSteps = _config.numPins * _config.numChips;
if(_config.patternStep >= maxSteps) {
_config.patternStep = 0;
_config.repeatCount++;
if(_config.maxRepeats > 0 && _config.repeatCount >= _config.maxRepeats) {
stopPattern();
}
}
}
// Private: Generate pattern state
uint16_t BarLED::generatePatternState(uint8_t pattern, uint8_t step) {
uint16_t state = 0;
uint16_t totalPins = _config.numPins * _config.numChips;
switch(pattern) {
case PATTERN_INCREMENT:
state = 1 << step;
break;
case PATTERN_DECREMENT:
state = 1 << (totalPins - 1 - step);
break;
case PATTERN_CHASE:
state = 1 << (step % totalPins);
break;
case PATTERN_GROW:
for(uint8_t i = 0; i <= step; i++) {
state |= (1 << i);
}
break;
case PATTERN_SHRINK:
for(uint8_t i = step; i < totalPins; i++) {
state |= (1 << i);
}
break;
case PATTERN_HEARTBEAT: {
uint8_t halfStep = totalPins / 2;
if(step < halfStep) {
for(uint8_t i = 0; i <= step; i++) {
state |= (1 << i);
state |= (1 << (totalPins - 1 - i));
}
} else {
for(uint8_t i = 0; i < (totalPins - step + halfStep); i++) {
state |= (1 << i);
state |= (1 << (totalPins - 1 - i));
}
}
break;
}
case PATTERN_BOUNCE: {
uint8_t bouncePos;
if(step < totalPins) {
bouncePos = step;
}
else {
bouncePos = (2 * totalPins - step - 2);
}
state = 1 << bouncePos;
break;
}
case PATTERN_RANDOM:
state = random(1 << totalPins);
break;
case PATTERN_WAVE: {
for(uint8_t i = 0; i < totalPins; i++) {
if((i + step) % 3 == 0) {
state |= (1 << i);
}
}
break;
}
case PATTERN_ALTERNATING: {
state = (step % 2) ? 0x5555 : 0xAAAA;
state &= ((1 << totalPins) - 1);
break;
}
}
return state;
}
// Private: Shift out bits with timing control
void BarLED::shiftBits(uint16_t data, uint8_t bits) {
for(int8_t i = bits - 1; i >= 0; i--) {
digitalWrite(_config.dataPin, (data >> i) & 1);
digitalWrite(_config.clockPin, HIGH);
// Add device-specific timing delays if needed
switch(_config.deviceType) {
case TYPE_MBI5027:
delayMicroseconds(1);
break;
}
digitalWrite(_config.clockPin, LOW);
}
}
// Private: Set error code and debug print
void BarLED::setError(uint8_t error) {
_config.lastError = error;
if(_config.debugMode) {
debugPrint("Error: ");
switch(error) {
case ERROR_INVALID_CHIP:
debugPrint("Invalid chip type");
break;
case ERROR_INVALID_PINS:
debugPrint("Invalid pin configuration");
break;
case ERROR_INVALID_PATTERN:
debugPrint("Invalid pattern");
break;
}
}
}
// Private: Debug print helper
void BarLED::debugPrint(const char* message) {
if(_config.debugMode) {
Serial.println(message);
}
}
// Private: Validate configuration
bool BarLED::validateConfig() {
if(_config.deviceType > TYPE_WS2803) {
setError(ERROR_INVALID_CHIP);
return false;
}
if(!_config.states) {
setError(ERROR_INVALID_PINS);
return false;
}
return true;
}