Skip to content

Commit

Permalink
feat: RGB leds supports
Browse files Browse the repository at this point in the history
# Conflicts:
#	radio/src/gui/gui_common.cpp
  • Loading branch information
raphaelcoeffic committed Sep 29, 2023
1 parent a60cf67 commit 818273a
Show file tree
Hide file tree
Showing 44 changed files with 752 additions and 113 deletions.
5 changes: 3 additions & 2 deletions radio/src/boards/generic_stm32/module_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "hal/module_port.h"
#include "stm32_serial_driver.h"
#include "stm32_softserial_driver.h"
#include "stm32_dma.h"

#include "module_ports.h"
#include "board.h"
Expand Down Expand Up @@ -86,7 +87,7 @@ static_assert(__STM32_PULSE_IS_TIMER_CHANNEL_SUPPORTED(INTMODULE_TIMER_Channel),
"Unsupported timer channel");

// Make sure the DMA channel is supported
static_assert(__STM32_PULSE_IS_DMA_STREAM_SUPPORTED(INTMODULE_TIMER_DMA_STREAM),
static_assert(__STM32_DMA_IS_STREAM_SUPPORTED(INTMODULE_TIMER_DMA_STREAM),
"Unsupported DMA stream");

#if !defined(INTMODULE_TIMER_DMA_IRQHandler)
Expand Down Expand Up @@ -198,7 +199,7 @@ static_assert(__STM32_PULSE_IS_TIMER_CHANNEL_SUPPORTED(EXTMODULE_TIMER_Channel),
"Unsupported timer channel");

// Make sure the DMA channel is supported
static_assert(__STM32_PULSE_IS_DMA_STREAM_SUPPORTED(EXTMODULE_TIMER_DMA_STREAM_LL),
static_assert(__STM32_DMA_IS_STREAM_SUPPORTED(EXTMODULE_TIMER_DMA_STREAM_LL),
"Unsupported DMA stream");

#if !defined(EXTMODULE_TIMER_DMA_IRQHandler)
Expand Down
115 changes: 115 additions & 0 deletions radio/src/boards/generic_stm32/rgb_leds.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

#include "stm32_pulse_driver.h"
#include "rgb_leds.h"
#include "hal.h"
#include "opentx.h"

#if defined(LED_STRIP_GPIO)

#include "stm32_ws2812.h"
#include "stm32_dma.h"

#if !defined(SIMU)
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
#endif

extern const stm32_pulse_timer_t _led_timer;

static TimerHandle_t rgbLedTimer = nullptr;
static StaticTimer_t rgbLedTimerBuffer;

void rgbSetLedColor(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
{
ws2812_set_color(led, r, g, b);
}

void rgbLedColorApply()
{
ws2812_update(&_led_timer);;
}

static void rgbLedTimerCb(TimerHandle_t xTimer)
{
(void)xTimer;

ws2812_update(&_led_timer);
}

void rgbLedStart()
{
if (!rgbLedTimer) {
rgbLedTimer =
xTimerCreateStatic("rgbLed", LED_STRIP_REFRESH_PERIOD / RTOS_MS_PER_TICK, pdTRUE, (void*)0,
rgbLedTimerCb, &rgbLedTimerBuffer);
}

if (rgbLedTimer) {
if( xTimerStart( rgbLedTimer, 0 ) != pdPASS ) {
/* The timer could not be set into the Active state. */
}
}
}

void rgbLedStop()
{
if (rgbLedTimer) {
if( xTimerStop( rgbLedTimer, 5 / RTOS_MS_PER_TICK ) != pdPASS ) {
/* The timer could not be stopped. */
}
}
}

const stm32_pulse_timer_t _led_timer = {
.GPIOx = LED_STRIP_GPIO,
.GPIO_Pin = LED_STRIP_GPIO_PIN_DATA,
.GPIO_Alternate = LED_STRIP_GPIO_PIN_AF,
.TIMx = LED_STRIP_TIMER,
.TIM_Freq = LED_STRIP_TIMER_FREQ,
.TIM_Channel = LED_STRIP_TIMER_CHANNEL,
.TIM_IRQn = (IRQn_Type)-1,
.DMAx = LED_STRIP_TIMER_DMA,
.DMA_Stream = LED_STRIP_TIMER_DMA_STREAM,
.DMA_Channel = LED_STRIP_TIMER_DMA_CHANNEL,
.DMA_IRQn = LED_STRIP_TIMER_DMA_IRQn,
.DMA_TC_CallbackPtr = nullptr,
};

// Make sure the timer channel is supported
static_assert(__STM32_PULSE_IS_TIMER_CHANNEL_SUPPORTED(LED_STRIP_TIMER_CHANNEL),
"Unsupported timer channel");

// Make sure the DMA channel is supported
static_assert(__STM32_DMA_IS_STREAM_SUPPORTED(LED_STRIP_TIMER_DMA_STREAM),
"Unsupported DMA stream");

#if !defined(LED_STRIP_TIMER_DMA_IRQHandler)
#error "Missing LED_STRIP_TIMER_DMA_IRQHandler definition"
#endif

extern "C" void LED_STRIP_TIMER_DMA_IRQHandler()
{
ws2812_dma_isr(&_led_timer);
}

#endif
29 changes: 29 additions & 0 deletions radio/src/boards/generic_stm32/rgb_leds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

#pragma once

void rgbLedStart();
void rgbLedStop();
void rgbSetLedColor(unsigned char, unsigned char, unsigned char, unsigned char);
void rgbLedColorApply();

// void boardInitRGBLed();
1 change: 1 addition & 0 deletions radio/src/dataconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ enum Functions {
FUNC_SET_SCREEN,
#endif
FUNC_DISABLE_AUDIO_AMP,
FUNC_RGB_LED,
#if defined(DEBUG)
FUNC_TEST, // should remain the last before MAX as not added in Companion
#endif
Expand Down
3 changes: 3 additions & 0 deletions radio/src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "opentx.h"
#include "switches.h"
#include "boards/generic_stm32/rgb_leds.h"

#if defined(COLORLCD)
void setRequestedMainView(uint8_t view);
Expand Down Expand Up @@ -542,6 +543,8 @@ const char* funcGetLabel(uint8_t func)
case FUNC_DISABLE_AUDIO_AMP:
return STR_SF_DISABLE_AUDIO_AMP;
#endif
case FUNC_RGB_LED:
return STR_SF_RGBLEDS;
#if defined(DEBUG)
case FUNC_TEST:
return STR_SF_TEST;
Expand Down
12 changes: 9 additions & 3 deletions radio/src/gui/128x64/model_special_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ void onCustomFunctionsFileSelectionMenu(const char * result)
if (func == FUNC_PLAY_SCRIPT) {
strcpy(directory, SCRIPTS_FUNCS_PATH);
}
else if (func == FUNC_RGB_LED) {
strcpy(directory, SCRIPTS_RGB_PATH);
}
else {
strcpy(directory, SOUNDS_PATH);
strncpy(directory+SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2);
Expand Down Expand Up @@ -305,7 +308,7 @@ void menuSpecialFunctions(event_t event, CustomFunctionData * functions, CustomF
}
#endif
#if defined(SDCARD)
else if (func == FUNC_PLAY_TRACK || func == FUNC_BACKGND_MUSIC || func == FUNC_PLAY_SCRIPT) {
else if (func == FUNC_PLAY_TRACK || func == FUNC_BACKGND_MUSIC || func == FUNC_PLAY_SCRIPT || func==FUNC_RGB_LED) {
if (ZEXIST(cfn->play.name))
lcdDrawSizedText(MODEL_SPECIAL_FUNC_3RD_COLUMN-6, y, cfn->play.name, sizeof(cfn->play.name), attr);
else
Expand All @@ -316,15 +319,18 @@ void menuSpecialFunctions(event_t event, CustomFunctionData * functions, CustomF
if (func==FUNC_PLAY_SCRIPT) {
strcpy(directory, SCRIPTS_FUNCS_PATH);
}
else if (func==FUNC_RGB_LED) {
strcpy(directory, SCRIPTS_RGB_PATH);
}
else {
strcpy(directory, SOUNDS_PATH);
strncpy(directory+SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2);
}
if (sdListFiles(directory, func==FUNC_PLAY_SCRIPT ? SCRIPTS_EXT : SOUNDS_EXT, sizeof(cfn->play.name), cfn->play.name)) {
if (sdListFiles(directory, func==FUNC_PLAY_SCRIPT || func==FUNC_RGB_LED ? SCRIPTS_EXT : SOUNDS_EXT, sizeof(cfn->play.name), cfn->play.name)) {
POPUP_MENU_START(onCustomFunctionsFileSelectionMenu);
}
else {
POPUP_WARNING(func==FUNC_PLAY_SCRIPT ? STR_NO_SCRIPTS_ON_SD : STR_NO_SOUNDS_ON_SD);
POPUP_WARNING(func==FUNC_PLAY_SCRIPT || func==FUNC_RGB_LED ? STR_NO_SCRIPTS_ON_SD : STR_NO_SOUNDS_ON_SD);
}
}
break;
Expand Down
8 changes: 6 additions & 2 deletions radio/src/gui/gui_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ bool isAssignableFunctionAvailable(int function, CustomFunctionData * functions)
#endif
#if !defined(HAPTIC)
case FUNC_HAPTIC:
return false;
#endif
#if !defined(DANGEROUS_MODULE_FUNCTIONS)
case FUNC_RANGECHECK:
Expand All @@ -595,11 +596,14 @@ bool isAssignableFunctionAvailable(int function, CustomFunctionData * functions)
case FUNC_PLAY_SCRIPT:
return false;
#endif
#if !defined(AUDIO_MUTE_GPIO)
case FUNC_DISABLE_AUDIO_AMP:
#if defined(AUDIO_MUTE_GPIO)
return true;
return false;
#endif
#if !defined(LED_STRIP_GPIO)
case FUNC_RGB_LED:
return false;
#endif
default:
return true;
}
Expand Down
5 changes: 0 additions & 5 deletions radio/src/hal/timer_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@

#include <stdint.h>

enum PulseGenerationType {
ETX_PWM=0,
ETX_TOGGLE
};

typedef struct {
uint8_t type;
uint8_t polarity;
uint16_t cmp_val;
} etx_timer_config_t;
Expand Down
56 changes: 55 additions & 1 deletion radio/src/lua/api_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#include "hal/rotary_encoder.h"
#include "switches.h"
#include "input_mapping.h"
#if defined(LED_STRIP_GPIO)
#include "boards/generic_stm32/rgb_leds.h"
#endif


#if defined(LIBOPENUI)
#include "libopenui.h"
Expand Down Expand Up @@ -2772,6 +2776,50 @@ static int luaGetTrainerStatus(lua_State * L)
return 1;
}

#if defined(LED_STRIP_GPIO)
/*luadoc
@function setRGBLedColor(id, rvalue, bvalue, cvalue)
@param id: integer identifying a led in the led chain
@param rvalue: interger, value of red channel
@param gvalue: interger, value of green channel
@param bvalue: interger, value of blue channel
@status current Introduced in 2.10
*/

static int luaSetRgbLedColor(lua_State * L)
{
uint8_t id = luaL_checkunsigned(L, 1);
uint8_t r = luaL_checkunsigned(L, 2);
uint8_t g = luaL_checkunsigned(L, 3);
uint8_t b = luaL_checkunsigned(L, 4);

rgbSetLedColor(id, r, g, b);

return 1;
}

/*luadoc
@function applyRGBLedColors()
Apply RGB led colors previously defined by setRGBLedColor
@status current Introduced in 2.10
*/

static int luaApplyRGBLedColors(lua_State * L)
{

rgbLedColorApply();

return 1;
}

#endif

#define KEY_EVENTS(xxx, yyy) \
{ "EVT_"#xxx"_FIRST", LRO_NUMVAL(EVT_KEY_FIRST(yyy)) }, \
Expand Down Expand Up @@ -2859,6 +2907,10 @@ LROT_BEGIN(etxlib, NULL, 0)
LROT_FUNCENTRY( getSourceIndex, luaGetSourceIndex )
LROT_FUNCENTRY( getSourceName, luaGetSourceName )
LROT_FUNCENTRY( sources, luaSources )
#if defined(LED_STRIP_GPIO)
LROT_FUNCENTRY(setRGBLedColor, luaSetRgbLedColor )
LROT_FUNCENTRY(applyRGBLedColors, luaApplyRGBLedColors )
#endif
LROT_END(etxlib, NULL, 0)

LROT_BEGIN(etxcst, NULL, 0)
Expand Down Expand Up @@ -3196,7 +3248,9 @@ LROT_BEGIN(etxcst, NULL, 0)
LROT_NUMENTRY( PLAY_NOW, PLAY_NOW )
LROT_NUMENTRY( PLAY_BACKGROUND, PLAY_BACKGROUND )
LROT_NUMENTRY( TIMEHOUR, TIMEHOUR )

#if defined(LED_STRIP_GPIO)
LROT_NUMENTRY( LED_STRIP_LENGTH, LED_STRIP_LENGTH )
#endif
LROT_NUMENTRY( UNIT_RAW, UNIT_RAW )
LROT_NUMENTRY( UNIT_VOLTS, UNIT_VOLTS )
LROT_NUMENTRY( UNIT_AMPS, UNIT_AMPS )
Expand Down
2 changes: 1 addition & 1 deletion radio/src/lua/api_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ static int luaModelGetCustomFunction(lua_State *L)
lua_newtable(L);
lua_pushtableinteger(L, "switch", CFN_SWITCH(cfn));
lua_pushtableinteger(L, "func", CFN_FUNC(cfn));
if (CFN_FUNC(cfn) == FUNC_PLAY_TRACK || CFN_FUNC(cfn) == FUNC_BACKGND_MUSIC || CFN_FUNC(cfn) == FUNC_PLAY_SCRIPT) {
if (CFN_FUNC(cfn) == FUNC_PLAY_TRACK || CFN_FUNC(cfn) == FUNC_BACKGND_MUSIC || CFN_FUNC(cfn) == FUNC_PLAY_SCRIPT || CFN_FUNC(cfn) == FUNC_RGB_LED) {
lua_pushtablenstring(L, "name", cfn->play.name);
}
else {
Expand Down
13 changes: 13 additions & 0 deletions radio/src/lua/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,19 @@ static bool luaLoadFunctionScript(uint8_t ref)
return true;
}
}

if (fn -> func == FUNC_RGB_LED && ZEXIST(fn -> play.name)) {
if (luaScriptsCount < MAX_SCRIPTS) {
ScriptInternalData & sid = scriptInternalData[luaScriptsCount++];
sid.reference = ref;
return luaLoadFile(SCRIPTS_RGB_PATH, fn->play.name, sid);
}
else {
POPUP_WARNING(STR_TOO_MANY_LUA_SCRIPTS);
return true;
}
}

return false;
}

Expand Down
Loading

0 comments on commit 818273a

Please sign in to comment.