This commit is contained in:
Raphael Coeffic
2022-03-20 16:22:14 +01:00
parent da59fca1bc
commit 530d395204
66 changed files with 1267 additions and 1081 deletions
+84 -63
View File
@@ -91,9 +91,49 @@
{ "EVT_"#xxx"_LONG", EVT_KEY_LONG(yyy) }, \
{ "EVT_"#xxx"_REPT", EVT_KEY_REPT(yyy) }
#if defined(LUA) && !defined(CLI)
Fifo<uint8_t, LUA_FIFO_SIZE> * luaRxFifo = nullptr;
#endif
// Note:
// - luaRxFifo & luaReceiveData are used only for USB serial
// - otherwise, the AUX serial buffer is used directly
//
static Fifo<uint8_t, LUA_FIFO_SIZE>* luaRxFifo = nullptr;
static int luaRxFifoGetByte(void*, uint8_t* data)
{
if (!luaRxFifo) return -1;
return luaRxFifo->pop(*data);
}
void luaReceiveData(uint8_t* buf, uint32_t len)
{
if (!luaRxFifo) {
luaRxFifo = new Fifo<uint8_t, LUA_FIFO_SIZE>();
luaSetGetSerialByte(nullptr, luaRxFifoGetByte);
}
if (luaRxFifo) {
while(len--) luaRxFifo->push(*buf++);
}
}
static void (*luaSendDataCb)(void*, uint8_t) = nullptr;
static void* luaSendDataCtx = nullptr;
void luaSetSendCb(void* ctx, void (*cb)(void*, uint8_t))
{
luaSendDataCb = nullptr;
luaSendDataCtx = ctx;
luaSendDataCb = cb;
}
static int (*luaGetSerialByte)(void*, uint8_t*) = nullptr;
static void* luaGetSerialByteCtx = nullptr;
void luaSetGetSerialByte(void* ctx, int (*fct)(void*, uint8_t*))
{
luaGetSerialByte = nullptr;
luaGetSerialByteCtx = ctx;
luaGetSerialByte = fct;
}
/*luadoc
@function getVersion()
@@ -1882,22 +1922,23 @@ Set baudrate for serial port(s) affected to LUA
*/
static int luaSetSerialBaudrate(lua_State * L)
{
#if defined(AUX_SERIAL) || defined(AUX2_SERIAL)
unsigned int baudrate = luaL_checkunsigned(L, 1);
#endif
// #if defined(AUX_SERIAL) || defined(AUX2_SERIAL)
// unsigned int baudrate = luaL_checkunsigned(L, 1);
// #endif
#if defined(AUX_SERIAL)
if (auxSerialMode == UART_MODE_LUA) {
auxSerialStop();
auxSerialSetup(baudrate, false);
}
#endif
#if defined(AUX2_SERIAL)
if (aux2SerialMode == UART_MODE_LUA) {
aux2SerialStop();
aux2SerialSetup(baudrate, false);
}
#endif
// TODO: add some callbacks for serial settings
// #if defined(AUX_SERIAL)
// if (auxSerialMode == UART_MODE_LUA) {
// auxSerialStop();
// auxSerialSetup(baudrate, false);
// }
// #endif
// #if defined(AUX2_SERIAL)
// if (aux2SerialMode == UART_MODE_LUA) {
// aux2SerialStop();
// aux2SerialSetup(baudrate, false);
// }
// #endif
return 1;
}
@@ -1917,30 +1958,15 @@ static int luaSerialWrite(lua_State * L)
if (!str || len < 1)
return 0;
#if defined(USB_SERIAL)
if (getSelectedUsbMode() == USB_SERIAL_MODE) {
auto _sendCb = luaSendDataCb;
auto _ctx = luaSendDataCtx;
if (_sendCb) {
size_t wr_len = len;
const char* p = str;
while(wr_len--) usbSerialPutc(*p++);
while(wr_len--) _sendCb(_ctx, *p++);
}
#endif
#if defined(AUX_SERIAL)
if (auxSerialMode == UART_MODE_LUA) {
size_t wr_len = len;
const char* p = str;
while(wr_len--) auxSerialPutc(*p++);
}
#endif
#if defined(AUX2_SERIAL)
if (aux2SerialMode == UART_MODE_LUA) {
size_t wr_len = len;
const char* p = str;
while(wr_len--) aux2SerialPutc(*p++);
}
#endif
return 0;
}
@@ -1962,34 +1988,29 @@ static int luaSerialRead(lua_State * L)
#if defined(LUA) && !defined(CLI)
int num = luaL_optunsigned(L, 1, 0);
if (!luaRxFifo) {
// TODO:
// - check which serial port has been assigned this function
// - set the receive callback on that serial port
//
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) {
// buffer full
break;
}
if (num == 0) {
if (*(p - 1) == '\n' || *(p - 1) == '\r') {
// found newline
auto _getByte = luaGetSerialByte;
auto _ctx = luaGetSerialByteCtx;
if (_getByte) {
while (_getByte(_ctx, p) > 0) {
p++; // increment only when pop was successful
if (p - str >= LUA_FIFO_SIZE) {
// buffer full
break;
}
if (num == 0) {
if (*(p - 1) == '\n' || *(p - 1) == '\r') {
// found newline
break;
}
}
else if (p - str >= num) {
// requested number of characters reached
break;
}
}
else if (p - str >= num) {
// requested number of characters reached
break;
}
}
lua_pushlstring(L, (const char*)str, p - str);