1717#include <zephyr/shell/shell.h>
1818#include <zephyr/drivers/led_strip.h>
1919#include <string.h>
20- #include <stdlib.h>
2120#define LOG_LEVEL LOG_LEVEL_DGB
2221LOG_MODULE_REGISTER (badge_shell );
2322
@@ -27,12 +26,11 @@ LOG_MODULE_REGISTER(badge_shell);
2726#endif
2827
2928#define STRIP_NUM_PIXELS DT_PROP(STRIP_NODE, chain_length)
30- #define LED_DELAY K_MSEC(600)
3129#define RGB (_r , _g , _b ) ((struct led_rgb) { .r = (_r), .g = (_g), .b = (_b)})
3230static const struct device * const strip = DEVICE_DT_GET (STRIP_NODE );
33-
3431static struct led_rgb pixels [STRIP_NUM_PIXELS ];
35-
32+ static struct k_work led_blink_work ;
33+ static int led_index = 0 ;
3634static void set_led_color (struct led_rgb color )
3735{
3836 for (int i = 0 ; i < STRIP_NUM_PIXELS ; i ++ ){
@@ -41,41 +39,102 @@ static void set_led_color(struct led_rgb color)
4139 int rc = led_strip_update_rgb (strip , pixels , STRIP_NUM_PIXELS );
4240 if (rc ){
4341 LOG_ERR ("led strip update failed: %d" ,rc );
44- }else {
45- LOG_INF ("LED color set: R=%u G=%u B=%u" , color .r , color .g , color .b );
4642 }
4743}
44+ static struct led_rgb colors [] = {
45+ {0x0F , 0x00 , 0x00 }, // Red
46+ {0x00 , 0x0F , 0x00 }, // green
47+ {0x00 , 0x00 , 0x0F } // blue
48+ };
4849
50+ void led_blink_work_handler (struct k_work * work )
51+ {
52+ set_led_color (colors [led_index ]); // LED color change
53+ led_index ++ ;
54+ if (led_index >= ARRAY_SIZE (colors ))
55+ led_index = 0 ;
56+ }
57+ void led_blink_timer_handler (struct k_timer * timer_id )
58+ {
59+ k_work_submit (& led_blink_work );
60+ }
61+ // Timer define
62+ static K_TIMER_DEFINE (led_blink_timer , led_blink_timer_handler , NULL) ;
63+ static struct led_rgb blink_color ;
64+ static bool blinking = false;
65+
66+ // cmd_led
4967static int cmd_led (const struct shell * shell , size_t argc , char * * argv )
5068{
51- if (argc != 4 ){
52- shell_print (shell , "Usage: led <red> <green> <blue> (each 0-255)" );
53- return - EINVAL ;
54- }
69+ if (argc < 2 ) {
70+ shell_print (shell , "Usage:" );
71+ shell_print (shell , " led <r> <g> <b> set led to a particular color" );
72+ shell_print (shell , " led <r> <g> <b> <delay> Blink color every <delay> ms" );
73+ shell_print (shell , " led off Turn off LED" );
74+ return - EINVAL ;
75+ }
5576
56- char * endptr ;
57- long r = strtol (argv [1 ], & endptr , 10 );
58- if (* endptr != '\0' || r < 0 || r > 255 ) {
59- shell_error (shell , "Invalid red value (0-255)" );
60- return - EINVAL ;
61- }
62- long g = strtol (argv [2 ], & endptr , 10 );
63- if (* endptr != '\0' || g < 0 || g > 255 ) {
64- shell_error (shell , "Invalid green value (0-255)" );
65- return - EINVAL ;
66- }
67- long b = strtol (argv [3 ], & endptr , 10 );
68- if (* endptr != '\0' || b < 0 || b > 255 ) {
69- shell_error (shell , "Invalid blue value (0-255)" );
70- return - EINVAL ;
71- }
77+ // Case 1: "led off"
78+ if (strcmp (argv [1 ], "off" ) == 0 ) {
79+ k_timer_stop (& led_blink_timer );
80+ blinking = false;
81+ set_led_color (RGB (0 , 0 , 0 ));
82+ shell_print (shell , "LED turned off" );
83+ return 0 ;
84+ }
7285
73- set_led_color (RGB ((uint8_t )r , (uint8_t )g , (uint8_t )b ));
74- return 0 ;
75- }
76- SHELL_CMD_REGISTER (led , NULL , "Set LED color (r, g, b)" , cmd_led );
86+ // Must have at least 4 args for RGB
87+ if (argc < 4 ) {
88+ shell_error (shell , "Usage: led <r> <g> <b> [delay_ms]" );
89+ return - EINVAL ;
90+ }
7791
92+ char * endptr ;
93+ long r = strtol (argv [1 ], & endptr , 10 );
94+ if (* endptr != '\0' || r < 0 || r > 255 ) {
95+ shell_error (shell , "Invalid red value (0-255)" );
96+ return - EINVAL ;
97+ }
98+ long g = strtol (argv [2 ], & endptr , 10 );
99+ if (* endptr != '\0' || g < 0 || g > 255 ) {
100+ shell_error (shell , "Invalid green value (0-255)" );
101+ return - EINVAL ;
102+ }
103+ long b = strtol (argv [3 ], & endptr , 10 );
104+ if (* endptr != '\0' || b < 0 || b > 255 ) {
105+ shell_error (shell , "Invalid blue value (0-255)" );
106+ return - EINVAL ;
107+ }
108+
109+ struct led_rgb color = RGB ((uint8_t )r , (uint8_t )g , (uint8_t )b );
110+
111+ // Case 2: "led R G B" (static color)
112+ if (argc == 4 ) {
113+ k_timer_stop (& led_blink_timer );
114+ blinking = false;
115+ set_led_color (color );
116+ shell_print (shell , "LED set to R=%ld G=%ld B=%ld" , r , g , b );
117+ return 0 ;
118+ }
78119
120+ // Case 3: "led R G B delay" (blink)
121+ long delay = strtol (argv [4 ], & endptr , 10 );
122+ if (* endptr != '\0' || delay <= 0 ) {
123+ shell_error (shell , "Invalid delay (ms)" );
124+ return - EINVAL ;
125+ }
126+
127+ blink_color = color ;
128+ blinking = true;
129+ k_timer_stop (& led_blink_timer );
130+ k_timer_start (& led_blink_timer , K_MSEC (delay ), K_MSEC (delay ));
131+
132+ shell_print (shell , "LED blinking R=%ld G=%ld B=%ld every %ld ms" , r , g , b , delay );
133+ return 0 ;
134+ }
135+
136+ // Register shell command
137+ SHELL_CMD_REGISTER (led , NULL , "LED control: led <r> <g> <b> [delay] | led off" , cmd_led );
79138static const struct smf_state badge_states [];
80139
81140enum badge_state {
@@ -106,7 +165,6 @@ struct s_object {
106165 enum badge_event event ;
107166}s_obj ;
108167
109- //LOG_MODULE_REGISTER(BADGE, LOG_LEVEL_DBG);
110168const struct device * display_dev ;
111169
112170// Define work queue for display operations
@@ -387,10 +445,8 @@ void badge_init_entry(void* arg)
387445 LOG_INF ("Badge init entry" );
388446 if (!device_is_ready (strip )){
389447 LOG_ERR ("LED strip not ready" );
390- return ;
448+ return ;
391449 }
392- LOG_INF ("LED strip ready. Use shell command: led r/g/b" );
393- set_led_color (RGB (0x0F , 0x0F , 0x0F )); // Default: white
394450 // Initialize display work queue
395451 k_work_queue_init (& display_work_q );
396452 k_work_queue_start (& display_work_q , display_stack , K_THREAD_STACK_SIZEOF (display_stack ),
@@ -455,7 +511,6 @@ enum smf_state_result badge_idle_run(void* arg)
455511 LOG_INF ("Unhandled event in idle state %d" , obj -> event );
456512 break ;
457513 }
458- return 0 ;
459514}
460515
461516void badge_idle_exit (void * arg )
@@ -468,7 +523,7 @@ void badge_error_entry(void* arg)
468523 LOG_INF ("Badge error entry" );
469524}
470525
471- enum smf_state_result badge_error_run (void * arg )
526+ void badge_error_run (void * arg )
472527{
473528
474529 struct s_object * obj = (struct s_object * )arg ;
@@ -483,7 +538,6 @@ enum smf_state_result badge_error_run(void* arg)
483538 LOG_INF ("Unhandled event in error state %d" , obj -> event );
484539 break ;
485540 }
486- return 0 ;
487541}
488542
489543void badge_error_exit (void * arg )
@@ -526,7 +580,10 @@ int main(void)
526580{
527581 int32_t ret ;
528582 int rc ;
529-
583+
584+ set_led_color (RGB (0x0F , 0x0F , 0x0F )); // Default: white
585+ k_work_init (& led_blink_work , led_blink_work_handler );
586+ k_timer_stop (& led_blink_timer );
530587 smf_set_initial (SMF_CTX (& s_obj ), & badge_states [BADGE_STATE_INIT ]);
531588 while (1 ) {
532589 rc = k_msgq_get (& event_msgq , & s_obj .event , K_NO_WAIT );
0 commit comments