1+ #include " fnLedStrip.h"
2+ #include " fnSystem.h"
3+ #include " ../../include/pinmap.h"
4+
5+ #ifdef ESP_PLATFORM
6+ #if defined(PIN_LED_STRIP) && defined(LED_STRIP_COUNT)
7+ #include " led_strip.h"
8+ static led_strip_handle_t strip;
9+ #define HAVE_LED_STRIP
10+ #endif // defined(PIN_LED_STRIP) && defined(LED_STRIP_COUNT)
11+ #endif // ESP_PLATFORM
12+
13+ #define BLINKING_TIME 100
14+
15+
16+ void LedStripManager::setup ()
17+ {
18+ r = g = b = 0 ;
19+ #ifdef HAVE_LED_STRIP
20+ // LED strip general initialization, according to your led board design
21+ led_strip_config_t strip_config = {
22+ .strip_gpio_num = PIN_LED_STRIP, // The GPIO that connected to the LED strip's data line
23+ .max_leds = LED_STRIP_COUNT, // The number of LEDs in the strip,
24+ .led_model = LED_MODEL_WS2812, // LED strip model
25+ .color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB, // The color order of the strip: GRB
26+ .flags = {
27+ .invert_out = false , // don't invert the output signal
28+ }
29+ };
30+
31+ // LED strip backend configuration: RMT
32+ led_strip_rmt_config_t rmt_config = {
33+ .clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption
34+ .resolution_hz = (10 * 1000 * 1000 ), // RMT counter clock frequency - 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
35+ .mem_block_symbols = 0 , // let the driver choose a proper memory block size automatically
36+ .flags = {
37+ .with_dma = 0 , // Using DMA can improve performance when driving more LEDs
38+ }
39+ };
40+
41+ // LED Strip object handle
42+ ESP_ERROR_CHECK (led_strip_new_rmt_device (&strip_config, &rmt_config, &strip));
43+
44+ for (int i = 0 ; i < LED_STRIP_COUNT; i++)
45+ led_strip_set_pixel (strip, i, r, g, b);
46+
47+ led_strip_refresh (strip);
48+ #endif
49+ }
50+
51+ void LedStripManager::set (eLedID id, bool on)
52+ {
53+ #ifdef HAVE_LED_STRIP
54+ switch (id)
55+ {
56+ case LED_STRIP_BUS:
57+ g = on?brightness:0 ;
58+ break ;
59+ case LED_STRIP_BT:
60+ r = on?brightness:0 ;
61+ break ;
62+ case LED_STRIP_WIFI:
63+ b = on?brightness:0 ;
64+ break ;
65+ };
66+ for (int i = 0 ; i < LED_STRIP_COUNT; i++)
67+ led_strip_set_pixel (strip, i, r, g, b);
68+
69+ led_strip_refresh (strip);
70+ #endif
71+ }
72+
73+ void LedStripManager::toggle (eLedID id)
74+ {
75+ #ifdef HAVE_LED_STRIP
76+ switch (id)
77+ {
78+ case LED_STRIP_BUS:
79+ g = brightness-g;
80+ break ;
81+ case LED_STRIP_BT:
82+ r = brightness-r;
83+ break ;
84+ case LED_STRIP_WIFI:
85+ b = brightness-b;
86+ break ;
87+ };
88+ for (int i = 0 ; i < LED_STRIP_COUNT; i++)
89+ led_strip_set_pixel (strip, i, r, g, b);
90+
91+ led_strip_refresh (strip);
92+ #endif
93+ }
94+
95+ void LedStripManager::blink (eLedID led, int count)
96+ {
97+ for (int i = 0 ; i < count; i++)
98+ {
99+ toggle (led);
100+ fnSystem.delay (BLINKING_TIME);
101+ toggle (led);
102+ if (i < count - 1 )
103+ fnSystem.delay (BLINKING_TIME);
104+ }
105+ }
106+
107+ bool LedStripManager::present ()
108+ {
109+ #ifdef HAVE_LED_STRIP
110+ return true ;
111+ #else
112+ return false ;
113+ #endif
114+ }
115+
116+ LedStripManager fnLedStrip;
0 commit comments