Skip to content

Commit 5bb7612

Browse files
3djc3djcrotorman
authored
chore(radio): save some ram in radios with RGB LEDs (#5973)
Co-authored-by: 3djc <3djc@gh.com> Co-authored-by: Risto <rotorman@users.noreply.github.com>
1 parent 538c5fc commit 5bb7612

File tree

8 files changed

+50
-60
lines changed

8 files changed

+50
-60
lines changed

radio/src/boards/generic_stm32/rgb_leds.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include <FreeRTOS/include/timers.h>
3737
#endif
3838

39+
static uint8_t _led_colors[WS2812_BYTES_PER_LED * LED_STRIP_LENGTH];
40+
3941
extern const stm32_pulse_timer_t _led_timer;
4042

4143
static TimerHandle_t rgbLedTimer = nullptr;
@@ -55,15 +57,22 @@ uint32_t rgbGetLedColor(uint8_t led)
5557
return ws2812_get_color(led);
5658
}
5759

58-
5960
bool rgbGetState(uint8_t led)
6061
{
6162
return ws2812_get_state(led);
6263
}
6364

6465
void rgbLedColorApply()
6566
{
66-
ws2812_update(&_led_timer);;
67+
ws2812_update(&_led_timer);
68+
}
69+
70+
void rgbLedClearAll()
71+
{
72+
for (uint8_t i = 0; i < LED_STRIP_LENGTH; i++) {
73+
ws2812_set_color(i, 0, 0, 0);
74+
}
75+
ws2812_update(&_led_timer);
6776
}
6877

6978
static void rgbLedTimerCb(TimerHandle_t xTimer)
@@ -111,6 +120,12 @@ const stm32_pulse_timer_t _led_timer = {
111120
.DMA_TC_CallbackPtr = nullptr,
112121
};
113122

123+
void rgbLedInit()
124+
{
125+
ws2812_init(&_led_timer, _led_colors, LED_STRIP_LENGTH, WS2812_GRB);
126+
rgbLedClearAll();
127+
}
128+
114129
// Make sure the timer channel is supported
115130
static_assert(__STM32_PULSE_IS_TIMER_CHANNEL_SUPPORTED(LED_STRIP_TIMER_CHANNEL),
116131
"Unsupported timer channel");
@@ -128,4 +143,4 @@ extern "C" void LED_STRIP_TIMER_DMA_IRQHandler()
128143
ws2812_dma_isr(&_led_timer);
129144
}
130145

131-
#endif
146+
#endif

radio/src/boards/generic_stm32/rgb_leds.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@
2121

2222
#pragma once
2323

24+
#include <stdint.h>
25+
26+
void rgbLedInit();
2427
void rgbLedStart();
2528
void rgbLedStop();
26-
void rgbSetLedColor(unsigned char, unsigned char, unsigned char, unsigned char);
29+
30+
void rgbSetLedColor(uint8_t led, uint8_t r, uint8_t g, uint8_t b);
2731
uint32_t rgbGetLedColor(uint8_t led);
28-
bool rgbGetState(unsigned char);
29-
void rgbLedColorApply();
32+
33+
void rgbLedClearAll();
34+
35+
bool rgbGetState(uint8_t led);
36+
void rgbLedColorApply();

radio/src/targets/common/arm/stm32/stm32_ws2812.cpp

+6-12
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232
#include <string.h>
3333

3434
// Pixel values
35-
static uint8_t _led_colors[WS2812_BYTES_PER_LED * WS2812_MAX_LEDS];
36-
37-
// Timer used
38-
// static const stm32_pulse_timer_t* _led_timer;
35+
static uint8_t* _led_colors = nullptr;
3936

4037
// LED strip length
4138
static uint8_t _led_strip_len;
@@ -200,20 +197,17 @@ static void _init_timer(const stm32_pulse_timer_t* tim)
200197
NVIC_SetPriority(tim->DMA_IRQn, WS2812_DMA_IRQ_PRIO);
201198
}
202199

203-
void ws2812_init(const stm32_pulse_timer_t* timer, uint8_t strip_len, uint8_t type)
200+
void ws2812_init(const stm32_pulse_timer_t* timer, uint8_t* strip_colors,
201+
uint8_t strip_len, uint8_t type)
204202
{
205203
WS2812_DBG_INIT;
206204
pulse_inc = IS_TIM_32B_COUNTER_INSTANCE(timer->TIMx) ? 2 : 1;
207205

208-
memset(_led_colors, 0, sizeof(_led_colors));
206+
_led_colors = strip_colors;
207+
_led_strip_len = strip_len;
208+
memset(_led_colors, 0, strip_len * WS2812_BYTES_PER_LED);
209209
memset(_led_dma_buffer, 0, sizeof(_led_dma_buffer));
210210

211-
if (strip_len <= WS2812_MAX_LEDS) {
212-
_led_strip_len = strip_len;
213-
} else {
214-
_led_strip_len = WS2812_MAX_LEDS;
215-
}
216-
217211
_r_offset = (type >> 4) & 0b11;
218212
_g_offset = (type >> 2) & 0b11;
219213
_b_offset = type & 0b11;

radio/src/targets/common/arm/stm32/stm32_ws2812.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,16 @@
3030
// RGB
3131
#define WS2812_BYTES_PER_LED 3
3232

33-
// Maximum number of supported LEDs
34-
#if !defined(WS2812_MAX_LEDS)
35-
# define WS2812_MAX_LEDS 48
36-
#endif
37-
3833
// Number of LED periods used for trailing reset
3934
#if !defined(WS2812_TRAILING_RESET)
4035
# define WS2812_TRAILING_RESET 10
4136
#endif
4237

43-
void ws2812_init(const stm32_pulse_timer_t* timer, uint8_t strip_len, uint8_t type);
38+
void ws2812_init(const stm32_pulse_timer_t* timer, uint8_t* strip_colors,
39+
uint8_t strip_len, uint8_t type);
4440
void ws2812_update(const stm32_pulse_timer_t* timer);
4541
bool ws2812_get_state(uint8_t led);
4642
void ws2812_dma_isr(const stm32_pulse_timer_t* timer);
4743

4844
void ws2812_set_color(uint8_t led, uint8_t r, uint8_t g, uint8_t b);
49-
uint32_t ws2812_get_color(uint8_t led);
45+
uint32_t ws2812_get_color(uint8_t led);

radio/src/targets/horus/board.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,6 @@
5757
#include "csd203_sensor.h"
5858
#endif
5959

60-
#if defined(LED_STRIP_GPIO)
61-
// Common LED driver
62-
extern const stm32_pulse_timer_t _led_timer;
63-
64-
void ledStripOff()
65-
{
66-
for (uint8_t i = 0; i < LED_STRIP_LENGTH; i++) {
67-
ws2812_set_color(i, 0, 0, 0);
68-
}
69-
ws2812_update(&_led_timer);
70-
}
71-
#endif
72-
7360
HardwareOptions hardwareOptions;
7461
bool boardBacklightOn = false;
7562

@@ -144,12 +131,13 @@ uint16_t getSixPosAnalogValue(uint16_t adcValue)
144131
}
145132
if (dirty) {
146133
for (uint8_t i = 0; i < 6; i++) {
147-
if (i == sixPosState)
134+
if (i == sixPosState) {
148135
ws2812_set_color(i, SIXPOS_LED_RED, SIXPOS_LED_GREEN, SIXPOS_LED_BLUE);
149-
else
136+
} else {
150137
ws2812_set_color(i, 0, 0, 0);
138+
}
151139
}
152-
ws2812_update(&_led_timer);
140+
rgbLedColorApply();
153141
}
154142
return (4096/5)*(sixPosState);
155143
}
@@ -232,8 +220,7 @@ void boardInit()
232220
hapticInit();
233221

234222
#if defined(LED_STRIP_GPIO)
235-
ws2812_init(&_led_timer, LED_STRIP_LENGTH, WS2812_GRB);
236-
ledStripOff();
223+
rgbLedInit();
237224
#endif
238225

239226
#if defined(BLUETOOTH)

radio/src/targets/pl18/board.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ void boardInit()
234234
usbInit();
235235

236236
#if defined(LED_STRIP_GPIO)
237-
ws2812_init(&_led_timer, LED_STRIP_LENGTH, WS2812_GRB);
238-
ledStripOff();
237+
rgbLedInit();
239238
#endif
240239

241240
uint32_t press_start = 0;

radio/src/targets/taranis/board.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@
6262

6363
HardwareOptions hardwareOptions;
6464

65-
#if defined(LED_STRIP_GPIO)
66-
extern const stm32_pulse_timer_t _led_timer;
67-
#endif
68-
6965
#if !defined(BOOT)
7066

7167
#if defined(FUNCTION_SWITCHES)
@@ -104,7 +100,7 @@ uint16_t getSixPosAnalogValue(uint16_t adcValue)
104100
else
105101
ws2812_set_color(i, 0, 0, 0);
106102
}
107-
ws2812_update(&_led_timer);
103+
rgbLedColorApply();
108104
}
109105
return (4096/5)*(sixPosState);
110106
}
@@ -244,11 +240,7 @@ void boardInit()
244240
usbInit();
245241

246242
#if defined(LED_STRIP_GPIO)
247-
ws2812_init(&_led_timer, LED_STRIP_LENGTH, WS2812_GRB);
248-
for (uint8_t i = 0; i < LED_STRIP_LENGTH; i++) {
249-
ws2812_set_color(i, 0, 0, 50);
250-
}
251-
ws2812_update(&_led_timer);
243+
rgbLedInit();
252244
#endif
253245

254246
#if defined(HAPTIC)

radio/src/targets/taranis/hal.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -1963,11 +1963,11 @@
19631963

19641964
// LED Strip
19651965
#if defined(RGBLEDS)
1966-
#if defined(RADIO_GX12)
1967-
#define LED_STRIP_LENGTH 8
1968-
#else
1969-
#define LED_STRIP_LENGTH 7
1970-
#endif
1966+
#if defined(RADIO_GX12)
1967+
#define LED_STRIP_LENGTH 8
1968+
#else
1969+
#define LED_STRIP_LENGTH 7
1970+
#endif
19711971
#define LED_STRIP_GPIO GPIO_PIN(GPIOA, 8) // PA.08 / TIM1_CH1
19721972
#define LED_STRIP_GPIO_AF LL_GPIO_AF_1 // TIM1 / TIM2
19731973
#define LED_STRIP_TIMER TIM1

0 commit comments

Comments
 (0)