-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathx_led.c
589 lines (435 loc) · 15.6 KB
/
x_led.c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* Copyright 2022 u-blox Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file
* @brief This file contains the implementation of the Led API described in
* x_led.h file
*/
#include "x_led.h"
//Zephyr related
#include <zephyr.h>
#include <stdlib.h> //atoi
#include <logging/log.h>
#include <drivers/led.h>
// Application related
#include "x_logging.h"
#include "x_errno.h"
#include "x_system_conf.h"
/* ----------------------------------------------------------------
* DEFINITIONS
* -------------------------------------------------------------- */
// Max brightness for use with xLedSetBrightness function
#define MAX_BRIGHTNESS 100
/* ----------------------------------------------------------------
* TYPE DEFINITIONS
* -------------------------------------------------------------- */
/** Describes the combination of leds needed to turn on to get a
* certain color
*/
typedef struct{
uint8_t ledsNum; /**< Number of Leds needed */
xLedColor_t ledsNeeded[3]; /**< Which leds are needed (max 3) */
}ledColorCombination_t;
/* ----------------------------------------------------------------
* STATIC FUNCTION DECLARATION
* -------------------------------------------------------------- */
/** Thread which controls the blinking/fading effects of Led */
void xLedThread(void);
/** Toggles currently active (in status) led color */
static bool xLedToggle(void);
/** Sets a certain Led color to a given brightness
* (in a scale of 0-100)
*
* @param color Led color
* @param brightness Brightness value: 100 = full on, 0 = off
*/
static void xLedSetBrightness(xLedColor_t color, uint8_t brightness);
/** Get the Led combination needed to achieve a certain Led color
*
* @param color Color to achieve
* @return color conmbination needed
*/
static ledColorCombination_t ledGetColorCombination(xLedColor_t color);
/* ----------------------------------------------------------------
* ZEPHYR RELATED DEFINITIONS/DECLARATIONS
* -------------------------------------------------------------- */
// Related to the Zephyr PWM driver which is used to control
// LED brightness
#if DT_NODE_HAS_STATUS(DT_INST(0, pwm_leds), okay)
#define LED_PWM_NODE_ID DT_INST(0, pwm_leds)
#define LED_PWM_DEV_NAME DEVICE_DT_NAME(LED_PWM_NODE_ID)
#else
#error "No LED PWM device found"
#endif
LOG_MODULE_REGISTER(LOGMOD_NAME_LED, LOG_LEVEL_DBG);
// Thread definition
K_THREAD_DEFINE(xLedThreadId, LED_STACK_SIZE, xLedThread, NULL, NULL, NULL,
LED_PRIORITY, 0, 0);
/* ----------------------------------------------------------------
* GLOBALS
* -------------------------------------------------------------- */
/** Device descriptor */
const struct device *gpLedPWM;
/** Holds Led status */
static xLedStatus gLedStatus = {
.LedIsOn=false,
.color = LedOff,
.mode= LedNormal,
.blinkState.remainingBlinks = -1,
.fadeState.remainingFades = -1 };
/** Holds string representation of supported colors */
const char *const gLedColorStrings[]={
[LedRed] = "red",
[LedGreen] = "green",
[LedBlue] = "blue",
[LedCyan] = "cyan",
[LedPurple] = "purple",
[LedYellow] = "yellow",
[LedWhite] = "white",
[LedOff] = "off"
};
/* ----------------------------------------------------------------
* STATIC FUNCTION IMPLEMENTATION
* -------------------------------------------------------------- */
void xLedThread(void)
{
// initialize at a "random" value that makes sense.
// this won't be used anyway under normal circumstances
uint32_t sleep_period_ms = 100;
while(1){
if( gLedStatus.mode == LedNormal ){
k_thread_suspend(xLedThreadId);
}
// Handle Blinking
if( gLedStatus.mode == LedBlinking ){
if( xLedToggle() ){ //blink led on period
sleep_period_ms = gLedStatus.blinkState.delayOn;
}
else{ // blink led off period
sleep_period_ms = gLedStatus.blinkState.delayOff;
if( gLedStatus.blinkState.remainingBlinks > 0 ){
gLedStatus.blinkState.remainingBlinks--;
}
// blinking complete
if( gLedStatus.blinkState.remainingBlinks == 0 ){
gLedStatus.mode = LedNormal;
gLedStatus.blinkState.delayOn = 0;
gLedStatus.blinkState.delayOff = 0;
k_thread_suspend(xLedThreadId);
}
}
}
// Handle Fading
if( gLedStatus.mode == LedFading ){
xLedSetBrightness( gLedStatus.color, gLedStatus.fadeState.brightness );
// stop fading
if( gLedStatus.fadeState.remainingFades == 0 ){
gLedStatus.mode = LedNormal;
gLedStatus.fadeState.delayIn = 0;
gLedStatus.fadeState.delayOut = 0;
k_thread_suspend(xLedThreadId);
}
//start fading out
if( gLedStatus.fadeState.brightness == 100 ){
gLedStatus.fadeState.directionIn = false;
sleep_period_ms = gLedStatus.fadeState.delayOut;
}
//start fading in
if( gLedStatus.fadeState.brightness == 0 ){
gLedStatus.fadeState.directionIn = true;
sleep_period_ms = gLedStatus.fadeState.delayIn;
gLedStatus.fadeState.remainingFades--;
}
// next step brightness
if( gLedStatus.fadeState.directionIn ){
gLedStatus.fadeState.brightness+=1;
}
else{
gLedStatus.fadeState.brightness-=1;
}
}
k_sleep( K_MSEC( sleep_period_ms ) );
}
}
static void xLedSetBrightness(xLedColor_t color, uint8_t brightness){
ledColorCombination_t comb = ledGetColorCombination( gLedStatus.color );
for( uint8_t x = 0; x < comb.ledsNum ; x++ ){
led_set_brightness( gpLedPWM, comb.ledsNeeded[x], brightness );
}
gLedStatus.LedIsOn = true;
}
static bool xLedToggle(void){
if( gLedStatus.LedIsOn ){
// turn off all leds
led_off( gpLedPWM,LedRed );
led_off( gpLedPWM,LedGreen );
led_off( gpLedPWM,LedBlue );
gLedStatus.LedIsOn = false;
return false;
}
else{
ledColorCombination_t comb = ledGetColorCombination( gLedStatus.color );
for( uint8_t x = 0; x < comb.ledsNum ; x++){
led_on( gpLedPWM, comb.ledsNeeded[x] );
}
gLedStatus.LedIsOn = true;
return true;
}
}
static ledColorCombination_t ledGetColorCombination(xLedColor_t color){
ledColorCombination_t comb={0};
if(color <= LedBlue){
comb.ledsNum=1;
comb.ledsNeeded[0] = color;
}
else if(color == LedWhite){
comb.ledsNum=3;
comb.ledsNeeded[0] = LedRed;
comb.ledsNeeded[1] = LedGreen;
comb.ledsNeeded[2] = LedBlue;
}
// no need to write that exclusively
// else if(color == LedOff){
// comb.ledsNum=0;
// }
// 2 color combinations
else if( color == LedYellow ){
comb.ledsNum=2;
comb.ledsNeeded[0] = LedRed;
comb.ledsNeeded[1] = LedGreen;
}
else if( color == LedPurple ){
comb.ledsNum=2;
comb.ledsNeeded[0] = LedRed;
comb.ledsNeeded[1] = LedBlue;
}
else if( color == LedCyan ){
comb.ledsNum=2;
comb.ledsNeeded[0] = LedGreen;
comb.ledsNeeded[1] = LedBlue;
}
return comb;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTION IMPLEMENTATION
* -------------------------------------------------------------- */
err_code xLedInit(void){
gpLedPWM = device_get_binding(LED_PWM_DEV_NAME);
if (gpLedPWM) {
LOG_INF("Found device %s\r\n", LED_PWM_DEV_NAME);
xLedOff();
return X_ERR_SUCCESS;
} else {
LOG_ERR("Device %s not found\r\n", LED_PWM_DEV_NAME);
return X_ERR_DEVICE_NOT_FOUND; //add error code (init fail)
}
}
void xLedOn(xLedColor_t color){
// define which leds are needed to get the requested color
ledColorCombination_t comb = ledGetColorCombination(color);
//close previously open leds
led_off(gpLedPWM,LedRed);
led_off(gpLedPWM,LedGreen);
led_off(gpLedPWM,LedBlue);
// open the necessary leds to get the requested color
for( uint8_t x = 0; x < comb.ledsNum ; x++){
led_on(gpLedPWM, comb.ledsNeeded[x] );
}
gLedStatus.LedIsOn = true;
gLedStatus.color = color;
gLedStatus.mode = LedNormal;
return;
}
void xLedOff(void){
led_off(gpLedPWM,LedRed);
led_off(gpLedPWM,LedGreen);
led_off(gpLedPWM,LedBlue);
gLedStatus.LedIsOn = false;
gLedStatus.color = LedOff;
gLedStatus.mode = LedNormal;
k_thread_suspend(xLedThreadId);
return;
}
xLedStatus xLedGetStatus(void){
return gLedStatus;
}
err_code xLedBlink(xLedColor_t color, uint32_t delayOn, uint32_t delayOff, uint32_t blinks){
if( blinks == 0 ){
LOG_DBG("Blink: %s, on:%d, off:%d \r\n", gLedColorStrings[color], delayOn, delayOff);
}
else{
LOG_DBG("Blink: %s %d times, on:%d, off:%d \r\n", gLedColorStrings[color], blinks, delayOn, delayOff);
}
// if both delay on and off are zero the xLedThread won't yield
if( ( !delayOn ) || ( !delayOff) ){
LOG_ERR("Delay on or off in blinking cannot be 0 \r\n");
return X_ERR_INVALID_PARAMETER;
}
//reset Led state
xLedOff();
gLedStatus.mode = LedBlinking;
gLedStatus.color = color;
gLedStatus.blinkState.delayOn = delayOn;
gLedStatus.blinkState.delayOff = delayOff;
if( blinks != 0 ){
gLedStatus.blinkState.remainingBlinks = blinks;
}
else{
gLedStatus.blinkState.remainingBlinks = -1; //blink indefinitely
}
// we assign a thread for blinking, because PWM has some
// timing restrictions when the on/off times are big
k_thread_resume(xLedThreadId);
return X_ERR_SUCCESS;
}
err_code xLedFade(xLedColor_t color, uint32_t fade_in_time, uint32_t fade_out_time, uint32_t times){
if( times == 0 ){
LOG_DBG("Fade: %s, on:%d, off:%d \r\n", gLedColorStrings[color], fade_in_time, fade_out_time);
}
else{
LOG_DBG("Blink: %s %d times, on:%d, off:%d \r\n", gLedColorStrings[color], times, fade_in_time, fade_out_time);
}
// if both delay on and off are zero the xLedThread won't yield
if( ( !fade_in_time ) && ( !fade_out_time) ){
LOG_ERR("Fade in and out in fading cannot be both 0 \r\n");
return X_ERR_INVALID_PARAMETER;
}
if( ( fade_in_time < 100 ) || ( fade_out_time < 100 ) ){
LOG_ERR("Fade in or out cannot be less than 100 \r\n");
return X_ERR_INVALID_PARAMETER;
}
//reset Led state
xLedOff();
//calculate fade_in, fade_out step delays
gLedStatus.fadeState.delayIn = (fade_in_time/MAX_BRIGHTNESS);
gLedStatus.fadeState.delayOut = (fade_out_time/MAX_BRIGHTNESS);
gLedStatus.mode = LedFading;
gLedStatus.color = color;
gLedStatus.fadeState.brightness = 0;
gLedStatus.fadeState.directionIn = true;
if( times != 0 ){
//+1 because we subtract 1 every time brightness is 0. We start with brightness 0, so the first
// time at fading startup we miss one time
gLedStatus.fadeState.remainingFades = times + 1;
}
else{
gLedStatus.fadeState.remainingFades = -1; //fade indefinitely
}
// we assign a thread for blinking, because PWM has some timing
// restrictions when the on/off times are big
k_thread_resume(xLedThreadId);
return X_ERR_SUCCESS;
}
err_code xLedResumeStatus(xLedStatus status){
if( ( !status.LedIsOn ) && ( status.mode == LedNormal ) ){
xLedOff();
}
if( status.color > LedOff ){
return X_ERR_INVALID_PARAMETER;
}
//actions
switch(status.mode){
case LedNormal: xLedOn(status.color);
break;
case LedBlinking: xLedBlink( status.color,
status.blinkState.delayOn,
status.blinkState.delayOff,
status.blinkState.remainingBlinks );
break;
case LedFading: xLedFade( status.color,
(status.fadeState.delayIn)*MAX_BRIGHTNESS,
(status.fadeState.delayOut)*MAX_BRIGHTNESS,
status.fadeState.remainingFades );
break;
default: return X_ERR_INVALID_PARAMETER;
}
return X_ERR_SUCCESS;
}
/* ----------------------------------------------------------------
* SHELL COMMANDS IMPLEMENTATION
* -------------------------------------------------------------- */
void xLedOnCmd(const struct shell *shell, size_t argc, char **argv){
if(argc == 1){
// invalid number of parameters
shell_warn(shell, "Please provide also the color \r\n");
return;
}
if(argc > 2){
// invalid number of parameters
shell_warn(shell, "invalid number of parameters \r\n");
return;
}
for(uint8_t x=0; x < LedOff; x++){
if( strcmp( argv[1], gLedColorStrings[x] ) == 0 ){
xLedOn(x);
return;
}
}
// if function reaches this point no valid color string was given as a parameter
shell_error(shell,"No valid color string provided \r\n");
return;
}
void xLedBlinkCmd(const struct shell *shell, size_t argc, char **argv){
if( ( argc < 4 ) || (argc > 5) ){
// invalid number of parameters
shell_warn(shell, "invalid number of parameters: should be <color> <delay on> <delay off> <optional:blinks> \r\n");
return;
}
uint8_t x;
for( x=0; x < LedOff; x++ ){
if( strcmp( argv[1], gLedColorStrings[x] ) == 0 ){
break;
}
}
if( x == LedOff ){
shell_error(shell,"No valid color string provided \r\n");
return;
}
// just blink indefinitely
if( argc == 4 ){
xLedBlink(x,atoi(argv[2]),atoi(argv[3]), 0 );
}
// blink the specified number of times
if( argc == 5 ){
xLedBlink(x,atoi(argv[2]),atoi(argv[3]),atoi(argv[4]));
}
return;
}
void xLedFadeCmd(const struct shell *shell, size_t argc, char **argv){
if( ( argc < 4 ) || (argc > 5) ){
// invalid number of parameters
shell_warn(shell, "invalid number of parameters: should be <color> <delay on> <delay off> <optional:blinks> \r\n");
return;
}
uint8_t x;
for( x=0; x < LedOff; x++ ){
if( strcmp( argv[1], gLedColorStrings[x] ) == 0 ){
break;
}
}
if( x == LedOff ){
shell_error(shell,"No valid color string provided \r\n");
return;
}
// just fade indefinitely
if( argc == 4 ){
xLedFade(x,atoi(argv[2]),atoi(argv[3]), 0 );
}
// fade the specified number of times
if( argc == 5 ){
xLedFade(x,atoi(argv[2]),atoi(argv[3]),atoi(argv[4]));
}
return;
}