mirror of
https://github.com/opentx/opentx.git
synced 2025-07-15 04:15:26 +03:00
parent
51f841d2af
commit
682f0f1b05
4 changed files with 79 additions and 2 deletions
|
@ -66,6 +66,10 @@
|
||||||
{ "EVT_"#xxx"_LONG", EVT_KEY_LONG(yyy) }, \
|
{ "EVT_"#xxx"_LONG", EVT_KEY_LONG(yyy) }, \
|
||||||
{ "EVT_"#xxx"_REPT", EVT_KEY_REPT(yyy) }
|
{ "EVT_"#xxx"_REPT", EVT_KEY_REPT(yyy) }
|
||||||
|
|
||||||
|
#if defined(LUA) && !defined(CLI)
|
||||||
|
Fifo<uint8_t, LUA_FIFO_SIZE> * luaRxFifo = nullptr;
|
||||||
|
#endif
|
||||||
|
|
||||||
/*luadoc
|
/*luadoc
|
||||||
@function getVersion()
|
@function getVersion()
|
||||||
|
|
||||||
|
@ -1615,6 +1619,53 @@ static int luaSerialWrite(lua_State * L)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*luadoc
|
||||||
|
@function serialRead([num])
|
||||||
|
@param num (optional): maximum number of bytes to read.
|
||||||
|
If non-zero, serialRead will read up to num characters from the buffer.
|
||||||
|
If 0 or left out, serialRead will read up to and including the first newline character or the end of the buffer.
|
||||||
|
Note that the returned string may not end in a newline if this character is not present in the buffer.
|
||||||
|
|
||||||
|
@retval str string. Empty if no new characters were available.
|
||||||
|
|
||||||
|
Reads characters from the serial port. The string is allowed to contain any character, including 0.
|
||||||
|
|
||||||
|
@status current Introduced in TODO
|
||||||
|
*/
|
||||||
|
static int luaSerialRead(lua_State * L)
|
||||||
|
{
|
||||||
|
int num = luaL_optunsigned(L, 1, 0);
|
||||||
|
|
||||||
|
#if defined(LUA) && !defined(CLI)
|
||||||
|
if (!luaRxFifo) {
|
||||||
|
luaRxFifo = new Fifo<uint8_t, LUA_FIFO_SIZE>();
|
||||||
|
if (!luaRxFifo) {
|
||||||
|
lua_pushlstring(L, "", 0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint8_t str[LUA_FIFO_SIZE];
|
||||||
|
uint8_t *p = str;
|
||||||
|
while (luaRxFifo->pop(*p)) {
|
||||||
|
p++; // increment only when pop was successful
|
||||||
|
if (p - str >= LUA_FIFO_SIZE) {
|
||||||
|
break; // Buffer full
|
||||||
|
}
|
||||||
|
if (num == 0 && (*(p - 1) == '\n' || *(p - 1) == '\r')) {
|
||||||
|
break; // Found newline
|
||||||
|
}
|
||||||
|
if (num > 0 && p - str >= num) {
|
||||||
|
break; // Requested number of characters reached
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, (const char*)str, p - str);
|
||||||
|
#else
|
||||||
|
lua_pushlstring(L, "", 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg opentxLib[] = {
|
const luaL_Reg opentxLib[] = {
|
||||||
{ "getTime", luaGetTime },
|
{ "getTime", luaGetTime },
|
||||||
{ "getDateTime", luaGetDateTime },
|
{ "getDateTime", luaGetDateTime },
|
||||||
|
@ -1662,6 +1713,7 @@ const luaL_Reg opentxLib[] = {
|
||||||
{ "multiBuffer", luaMultiBuffer },
|
{ "multiBuffer", luaMultiBuffer },
|
||||||
#endif
|
#endif
|
||||||
{ "serialWrite", luaSerialWrite },
|
{ "serialWrite", luaSerialWrite },
|
||||||
|
{ "serialRead", luaSerialRead },
|
||||||
{ nullptr, nullptr } /* sentinel */
|
{ nullptr, nullptr } /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,11 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(CLI)
|
||||||
|
#define LUA_FIFO_SIZE 256
|
||||||
|
extern Fifo<uint8_t, LUA_FIFO_SIZE> * luaRxFifo;
|
||||||
|
#endif
|
||||||
|
|
||||||
extern lua_State * lsScripts;
|
extern lua_State * lsScripts;
|
||||||
extern lua_State * lsWidgets;
|
extern lua_State * lsWidgets;
|
||||||
extern bool luaLcdAllowed;
|
extern bool luaLcdAllowed;
|
||||||
|
|
|
@ -192,6 +192,19 @@ extern "C" void AUX_SERIAL_USART_IRQHandler(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(LUA) && !defined(CLI)
|
||||||
|
if (luaRxFifo && auxSerialMode == UART_MODE_LUA) {
|
||||||
|
// Receive
|
||||||
|
uint32_t status = AUX_SERIAL_USART->SR;
|
||||||
|
while (status & (USART_FLAG_RXNE | USART_FLAG_ERRORS)) {
|
||||||
|
uint8_t data = AUX_SERIAL_USART->DR;
|
||||||
|
if (!(status & USART_FLAG_ERRORS)) {
|
||||||
|
luaRxFifo->push(data);
|
||||||
|
}
|
||||||
|
status = AUX_SERIAL_USART->SR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -218,11 +218,18 @@ static uint16_t VCP_DataRx (uint8_t* Buf, uint32_t Len)
|
||||||
|
|
||||||
#if defined(CLI)
|
#if defined(CLI)
|
||||||
//copy data to the application FIFO
|
//copy data to the application FIFO
|
||||||
for (uint32_t i = 0; i < Len; i++)
|
for (uint32_t i = 0; i < Len; i++) {
|
||||||
{
|
|
||||||
cliRxFifo.push(Buf[i]);
|
cliRxFifo.push(Buf[i]);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(LUA) && !defined(CLI)
|
||||||
|
// copy data to the LUA FIFO
|
||||||
|
if (luaRxFifo) {
|
||||||
|
for (uint32_t i = 0; i < Len; i++) {
|
||||||
|
luaRxFifo->push(Buf[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return USBD_OK;
|
return USBD_OK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue