From 25c02b96f3816a008972fce969d9e5c8d377373b Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 25 Jun 2018 10:10:01 +0200 Subject: [PATCH] add LUA access to serial port (serial2 and USB) --- radio/src/dataconstants.h | 2 +- radio/src/gui/480x272/radio_hardware.cpp | 4 +- radio/src/lua/api_general.cpp | 39 +++++++++++++++++++ .../targets/common/arm/stm32/CMakeLists.txt | 4 +- .../common/arm/stm32/serial2_driver.cpp | 2 + radio/src/translations/cz.h.txt | 6 ++- radio/src/translations/de.h.txt | 4 +- radio/src/translations/en.h.txt | 4 +- radio/src/translations/es.h.txt | 6 ++- radio/src/translations/fi.h.txt | 6 ++- radio/src/translations/fr.h.txt | 6 ++- radio/src/translations/it.h.txt | 6 ++- radio/src/translations/nl.h.txt | 5 ++- radio/src/translations/pl.h.txt | 7 +++- radio/src/translations/pt.h.txt | 6 ++- radio/src/translations/se.h.txt | 6 ++- 16 files changed, 94 insertions(+), 19 deletions(-) diff --git a/radio/src/dataconstants.h b/radio/src/dataconstants.h index 8e1a46e90..6daa0308a 100644 --- a/radio/src/dataconstants.h +++ b/radio/src/dataconstants.h @@ -260,7 +260,7 @@ enum UartModes { UART_MODE_TELEMETRY_MIRROR, UART_MODE_TELEMETRY, UART_MODE_SBUS_TRAINER, - // UART_MODE_CPPM_TRAINER, + UART_MODE_LUA, UART_MODE_COUNT, UART_MODE_MAX = UART_MODE_COUNT-1 }; diff --git a/radio/src/gui/480x272/radio_hardware.cpp b/radio/src/gui/480x272/radio_hardware.cpp index 98414244f..e6c204939 100644 --- a/radio/src/gui/480x272/radio_hardware.cpp +++ b/radio/src/gui/480x272/radio_hardware.cpp @@ -50,7 +50,7 @@ enum MenuRadioHardwareItems { ITEM_RADIO_HARDWARE_BLUETOOTH_MODE, ITEM_RADIO_HARDWARE_BLUETOOTH_PAIRING_CODE, ITEM_RADIO_HARDWARE_BLUETOOTH_NAME, -#if defined(SERIAL2) && defined(DEBUG) +#if defined(SERIAL2) ITEM_RADIO_HARDWARE_UART3_MODE, #endif ITEM_RADIO_HARDWARE_JITTER_FILTER, @@ -203,7 +203,7 @@ bool menuRadioHardware(event_t event) editName(HW_SETTINGS_COLUMN+50, y, g_eeGeneral.bluetoothName, LEN_BLUETOOTH_NAME, event, attr); break; -#if defined(SERIAL2) && defined(DEBUG) +#if defined(SERIAL2) case ITEM_RADIO_HARDWARE_UART3_MODE: lcdDrawText(MENUS_MARGIN_LEFT, y, STR_UART3MODE); g_eeGeneral.serial2Mode = editChoice(HW_SETTINGS_COLUMN+50, y, STR_UART3MODES, g_eeGeneral.serial2Mode, 0, UART_MODE_MAX, attr, event); diff --git a/radio/src/lua/api_general.cpp b/radio/src/lua/api_general.cpp index a04864e67..59039d65a 100644 --- a/radio/src/lua/api_general.cpp +++ b/radio/src/lua/api_general.cpp @@ -1280,6 +1280,44 @@ static int luaResetGlobalTimer(lua_State * L) return 0; } +/*luadoc +@function serialWrite(str) +@param str (string) String to be written to the serial port. + +Writes a string to the serial port. The string is allowed to contain any character, including 0. + +@status current Introducted in TODO +*/ +static int luaSerialWrite(lua_State * L) +{ + const char * str = luaL_checkstring(L, 1); + size_t len = lua_rawlen(L, 1); + + if (!str || len < 1) + return 0; + +#if !defined(SIMU) + #if defined(USB_SERIAL) + if (getSelectedUsbMode() == USB_SERIAL_MODE) { + size_t wr_len = len; + const char* p = str; + while(wr_len--) usbSerialPutc(*p++); + } + #endif + #if defined(SERIAL2) + if (serial2Mode == UART_MODE_LUA) { + size_t wr_len = len; + const char* p = str; + while(wr_len--) serial2Putc(*p++); + } + #endif +#else + debugPrintf("luaSerialWrite: %.*s",len,str); +#endif + + return 0; +} + const luaL_Reg opentxLib[] = { { "getTime", luaGetTime }, { "getDateTime", luaGetDateTime }, @@ -1318,6 +1356,7 @@ const luaL_Reg opentxLib[] = { { "crossfireTelemetryPop", luaCrossfireTelemetryPop }, { "crossfireTelemetryPush", luaCrossfireTelemetryPush }, #endif + { "serialWrite", luaSerialWrite }, { NULL, NULL } /* sentinel */ }; diff --git a/radio/src/targets/common/arm/stm32/CMakeLists.txt b/radio/src/targets/common/arm/stm32/CMakeLists.txt index 3c7c046bf..ad2dca3d2 100644 --- a/radio/src/targets/common/arm/stm32/CMakeLists.txt +++ b/radio/src/targets/common/arm/stm32/CMakeLists.txt @@ -4,6 +4,8 @@ set(LUA_SCRIPT_LOAD_MODE "" CACHE STRING "Script loading mode and compilation fl option(LUA_COMPILER "Pre-compile and save Lua scripts" OFF) option(LUA_ALLOCATOR_TRACER "Trace Lua memory (de)allocations to debug port (also needs DEBUG=YES NANO=NO)" OFF) +option(USB_SERIAL "Enable USB serial (CDC)" OFF) + set(ARCH ARM) set(STM32USB_DIR ${THIRDPARTY_DIR}/STM32_USB-Host-Device_Lib_V2.2.0/Libraries) add_definitions(-DSTM32 -DLUA_INPUTS -DVARIO -DCROSSFIRE) @@ -39,7 +41,7 @@ set(FIRMWARE_TARGET_SRC ../common/arm/stm32/flash_driver.cpp ) -if(DEBUG OR CLI) +if(DEBUG OR CLI OR USB_SERIAL) set(STM32USB_SRC ${STM32USB_SRC} STM32_USB_Device_Library/Class/cdc/src/usbd_cdc_core.c diff --git a/radio/src/targets/common/arm/stm32/serial2_driver.cpp b/radio/src/targets/common/arm/stm32/serial2_driver.cpp index a5c67b51e..049f09374 100644 --- a/radio/src/targets/common/arm/stm32/serial2_driver.cpp +++ b/radio/src/targets/common/arm/stm32/serial2_driver.cpp @@ -101,6 +101,8 @@ void serial2Init(unsigned int mode, unsigned int protocol) uart3Setup(FRSKY_D_BAUDRATE, true); } break; + case UART_MODE_LUA: + uart3Setup(DEBUG_BAUDRATE, false); } } diff --git a/radio/src/translations/cz.h.txt b/radio/src/translations/cz.h.txt index ee4646747..8ed1c9a2c 100644 --- a/radio/src/translations/cz.h.txt +++ b/radio/src/translations/cz.h.txt @@ -69,7 +69,11 @@ #define TR_TRNCHN "CH1CH2CH3CH4" #define LEN_UART3MODES "\015" -#define TR_UART3MODES "VYP\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Trenér\0 ""Debug\0" +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Trenér\0 ""LUA\0 " +#else +#define TR_UART3MODES "VYP\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Trenér\0 ""LUA\0 " +#endif #define LEN_SWTYPES "\013" #define TR_SWTYPES "Žádný\0 ""Bez aretace""2-polohový\0""3-polohový\0" diff --git a/radio/src/translations/de.h.txt b/radio/src/translations/de.h.txt index 9ef57b602..d56247c3b 100644 --- a/radio/src/translations/de.h.txt +++ b/radio/src/translations/de.h.txt @@ -72,9 +72,9 @@ #define LEN_UART3MODES "\015" #if defined(CLI) || defined(DEBUG) -#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Eingang\0""Debug\0" +#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Eingang\0""LUA\0 " #else -#define TR_UART3MODES "AUS\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Eingang\0""Debug\0" +#define TR_UART3MODES "AUS\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Eingang\0""LUA\0 " #endif #define LEN_SWTYPES "\006" diff --git a/radio/src/translations/en.h.txt b/radio/src/translations/en.h.txt index 2baca31a4..758877621 100644 --- a/radio/src/translations/en.h.txt +++ b/radio/src/translations/en.h.txt @@ -72,9 +72,9 @@ #define LEN_UART3MODES "\015" #if defined(CLI) || defined(DEBUG) -#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetry\0 ""SBUS Trainer\0""Debug\0" +#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetry\0 ""SBUS Trainer\0""LUA\0 " #else -#define TR_UART3MODES "OFF\0 ""S-Port Mirror""Telemetry\0 ""SBUS Trainer\0""Debug\0" +#define TR_UART3MODES "OFF\0 ""S-Port Mirror""Telemetry\0 ""SBUS Trainer\0""LUA\0 " #endif #define LEN_SWTYPES "\006" diff --git a/radio/src/translations/es.h.txt b/radio/src/translations/es.h.txt index 08d6529ce..04a40a2a5 100644 --- a/radio/src/translations/es.h.txt +++ b/radio/src/translations/es.h.txt @@ -71,7 +71,11 @@ #define TR_TRNCHN "CH1CH2CH3CH4" #define LEN_UART3MODES "\015" -#define TR_UART3MODES "OFF\0 ""S-Port Mirror""Telemetry\0 ""SBUS Trainer\0""Debug\0" +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetry\0 ""SBUS Trainer\0""LUA\0 " +#else +#define TR_UART3MODES "OFF\0 ""S-Port Mirror""Telemetry\0 ""SBUS Trainer\0""LUA\0 " +#endif #define LEN_SWTYPES "\006" #define TR_SWTYPES "None\0 ""Toggle""2POS\0 ""3POS\0" diff --git a/radio/src/translations/fi.h.txt b/radio/src/translations/fi.h.txt index 64ffa8f49..f03a09da1 100644 --- a/radio/src/translations/fi.h.txt +++ b/radio/src/translations/fi.h.txt @@ -71,7 +71,11 @@ #define TR_TRNCHN "CH1CH2CH3CH4" #define LEN_UART3MODES "\015" -#define TR_UART3MODES "POIS\0 ""S-Port Pelik\0""Telemetry\0 ""SBUS Trainer\0""Debug\0" +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""S-Port Pelik\0""Telemetry\0 ""SBUS Trainer\0""LUA\0 " +#else +#define TR_UART3MODES "POIS\0 ""S-Port Pelik\0""Telemetry\0 ""SBUS Trainer\0""LUA\0 " +#endif #define LEN_SWTYPES "\006" #define TR_SWTYPES "None\0 ""Toggle""2POS\0 ""3POS\0" diff --git a/radio/src/translations/fr.h.txt b/radio/src/translations/fr.h.txt index 030ba61ef..285bc5686 100644 --- a/radio/src/translations/fr.h.txt +++ b/radio/src/translations/fr.h.txt @@ -71,7 +71,11 @@ #define TR_TRNCHN "CH1CH2CH3CH4" #define LEN_UART3MODES "\016" -#define TR_UART3MODES "OFF\0 ""Recopie S-Port""Télémétrie\0 ""Ecolage SBUS\0 ""Debug\0" +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""Recopie S-Port""Télémétrie\0 ""Ecolage SBUS\0 ""LUA\0 " +#else +#define TR_UART3MODES "OFF\0 ""Recopie S-Port""Télémétrie\0 ""Ecolage SBUS\0 ""LUA\0 " +#endif #define LEN_SWTYPES "\006" #define TR_SWTYPES "Rien\0 ""Levier""2-POS\0""3-POS\0" diff --git a/radio/src/translations/it.h.txt b/radio/src/translations/it.h.txt index 5aa820393..cd649dd55 100644 --- a/radio/src/translations/it.h.txt +++ b/radio/src/translations/it.h.txt @@ -71,7 +71,11 @@ #define TR_TRNCHN "ch1ch2ch3ch4" #define LEN_UART3MODES "\016" -#define TR_UART3MODES "OFF\0 ""Replica S-Port""Telemetria\0 ""SBUS Trainer\0 ""Debug\0" +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""Replica S-Port""Telemetria\0 ""SBUS Trainer\0 ""LUA\0 " +#else +#define TR_UART3MODES "OFF\0 ""Replica S-Port""Telemetria\0 ""SBUS Trainer\0 ""LUA\0 " +#endif #define LEN_SWTYPES "\006" #define TR_SWTYPES "None\0 ""Toggle""2POS\0 ""3POS\0" diff --git a/radio/src/translations/nl.h.txt b/radio/src/translations/nl.h.txt index 1038251ed..4a8704c05 100644 --- a/radio/src/translations/nl.h.txt +++ b/radio/src/translations/nl.h.txt @@ -72,10 +72,11 @@ #define LEN_UART3MODES "\015" #if defined(CLI) || defined(DEBUG) -#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Leerling""Debug\0 " +#define TR_UART3MODES "Debug\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Leerling""LUA\0 " #else -#define TR_UART3MODES "UIT\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Leerling""Debug\0 " +#define TR_UART3MODES "UIT\0 ""S-Port Mirror""Telemetrie\0 ""SBUS Leerling""LUA\0 " #endif + #define LEN_SWTYPES "\006" #define TR_SWTYPES "Geen\0 ""Wissel""2POS\0 ""3POS\0" diff --git a/radio/src/translations/pl.h.txt b/radio/src/translations/pl.h.txt index 6543e927a..118e6b551 100644 --- a/radio/src/translations/pl.h.txt +++ b/radio/src/translations/pl.h.txt @@ -70,9 +70,12 @@ #define LEN_TRNCHN "\003" #define TR_TRNCHN "KN1KN2KN3KN4" - #define LEN_UART3MODES "\015" /*13 decimal*/ -#define TR_UART3MODES "Wyłącz\0 ""S-Port Kopia ""Telemetria\0 ""Trener SBUS\0 ""Debug\0 " +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""S-Port Kopia ""Telemetria\0 ""Trener SBUS\0 ""LUA\0 " +#else +#define TR_UART3MODES "Wyłącz\0 ""S-Port Kopia ""Telemetria\0 ""Trener SBUS\0 ""LUA\0 " +#endif #define LEN_SWTYPES "\006" #define TR_SWTYPES "Brak\0 ""Chwil.""2POZ\0 ""3POZ\0" diff --git a/radio/src/translations/pt.h.txt b/radio/src/translations/pt.h.txt index 00d28a5b5..6b98a00a9 100644 --- a/radio/src/translations/pt.h.txt +++ b/radio/src/translations/pt.h.txt @@ -71,7 +71,11 @@ #define TR_TRNCHN "CH1CH2CH3CH4" #define LEN_UART3MODES "\017" -#define TR_UART3MODES "OFF\0 ""S-Port Mirror\0 ""Telemetry\0 ""SBUS Trainer\0 ""Debug\0" +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""S-Port Mirror\0 ""Telemetry\0 ""SBUS Trainer\0 ""LUA\0 " +#else +#define TR_UART3MODES "OFF\0 ""S-Port Mirror\0 ""Telemetry\0 ""SBUS Trainer\0 ""LUA\0 " +#endif #define LEN_SWTYPES "\006" #define TR_SWTYPES "None\0 ""Toggle""2POS\0 ""3POS\0" diff --git a/radio/src/translations/se.h.txt b/radio/src/translations/se.h.txt index a11fd3c76..dc2bba56b 100644 --- a/radio/src/translations/se.h.txt +++ b/radio/src/translations/se.h.txt @@ -71,7 +71,11 @@ #define TR_TRNCHN "KN1KN2KN3KN4" #define LEN_UART3MODES "\022" -#define TR_UART3MODES "Av\0 ""Spegling av S-Port""Telemetri\0 ""SBUS Trainer\0 ""Debug\0" +#if defined(CLI) || defined(DEBUG) +#define TR_UART3MODES "Debug\0 ""Spegling av S-Port""Telemetri\0 ""SBUS Trainer\0 ""LUA\0 " +#else +#define TR_UART3MODES "Av\0 ""Spegling av S-Port""Telemetri\0 ""SBUS Trainer\0 ""LUA\0 " +#endif #define LEN_SWTYPES "\006" #define TR_SWTYPES "Ingen\0""Flipp\0""2Pos\0 ""3Pos\0"