1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-15 12:25:12 +03:00

add LUA access to serial port (serial2 and USB)

This commit is contained in:
Raphael Coeffic 2018-06-25 10:10:01 +02:00
parent 5dd365238e
commit 25c02b96f3
16 changed files with 94 additions and 19 deletions

View file

@ -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 */
};