2014-12-22 12:35:05 +01:00
|
|
|
// Module for interfacing with serial
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <string.h>
|
2016-01-20 09:37:03 +01:00
|
|
|
#include "rom.h"
|
2019-07-18 18:02:02 +02:00
|
|
|
#include "driver/input.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
static int uart_receive_rf = LUA_NOREF;
|
2019-07-18 18:02:02 +02:00
|
|
|
|
|
|
|
void uart_on_data_cb(const char *buf, size_t len){
|
2016-08-02 22:33:05 +02:00
|
|
|
lua_State *L = lua_getstate();
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, uart_receive_rf);
|
|
|
|
lua_pushlstring(L, buf, len);
|
2020-04-27 02:13:38 +02:00
|
|
|
luaL_pcallx(L, 1, 0);
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2014-12-30 12:33:54 +01:00
|
|
|
// Lua: uart.on("method", [number/char], function, [run_input])
|
2016-12-11 20:35:04 +01:00
|
|
|
static int l_uart_on( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2019-07-18 18:02:02 +02:00
|
|
|
size_t el;
|
|
|
|
int stack = 2, data_len = -1;
|
|
|
|
char end_char = 0;
|
|
|
|
const char *method = lua_tostring( L, 1);
|
|
|
|
bool run_input = true;
|
|
|
|
luaL_argcheck(L, method && !strcmp(method, "data"), 1, "method not supported");
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_type( L, stack ) == LUA_TNUMBER) {
|
2019-07-18 18:02:02 +02:00
|
|
|
data_len = luaL_checkinteger( L, stack );
|
2019-07-20 01:45:08 +02:00
|
|
|
luaL_argcheck(L, data_len >= 0 && data_len < LUA_MAXINPUT, stack, "wrong arg range");
|
2014-12-30 08:42:24 +01:00
|
|
|
stack++;
|
2020-04-27 02:13:38 +02:00
|
|
|
} else if (lua_isstring(L, stack)) {
|
2014-12-30 08:42:24 +01:00
|
|
|
const char *end = luaL_checklstring( L, stack, &el );
|
2020-04-27 02:13:38 +02:00
|
|
|
end_char = end[0];
|
2014-12-30 08:42:24 +01:00
|
|
|
stack++;
|
2019-07-18 18:02:02 +02:00
|
|
|
if(el!=1) {
|
2014-12-30 08:42:24 +01:00
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, stack)) {
|
2019-07-18 18:02:02 +02:00
|
|
|
if (lua_isnumber(L, stack+1) && lua_tointeger(L, stack+1) == 0) {
|
|
|
|
run_input = false;
|
2014-12-30 08:42:24 +01:00
|
|
|
}
|
2019-07-18 18:02:02 +02:00
|
|
|
lua_pushvalue(L, stack);
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, uart_receive_rf);
|
|
|
|
uart_receive_rf = luaL_ref(L, LUA_REGISTRYINDEX);
|
2014-12-22 12:35:05 +01:00
|
|
|
} else {
|
2019-07-18 18:02:02 +02:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, uart_receive_rf);
|
|
|
|
uart_receive_rf = LUA_NOREF;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
if (uart_receive_rf == LUA_NOREF) {
|
|
|
|
input_setup_receive(NULL, 0, 0, 1);
|
|
|
|
} else
|
|
|
|
input_setup_receive(uart_on_data_cb, data_len, end_char, run_input);
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool uart0_echo = true;
|
|
|
|
// Lua: actualbaud = setup( id, baud, databits, parity, stopbits, echo )
|
2016-12-11 20:35:04 +01:00
|
|
|
static int l_uart_setup( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2019-07-18 18:02:02 +02:00
|
|
|
uint32_t id, databits, parity, stopbits;
|
2016-12-11 20:35:04 +01:00
|
|
|
uint32_t baud, res;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( uart, id );
|
2016-12-11 20:35:04 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
baud = luaL_checkinteger( L, 2 );
|
|
|
|
databits = luaL_checkinteger( L, 3 );
|
|
|
|
parity = luaL_checkinteger( L, 4 );
|
|
|
|
stopbits = luaL_checkinteger( L, 5 );
|
2019-07-18 18:02:02 +02:00
|
|
|
if (lua_isnumber(L,6)) {
|
|
|
|
input_setecho(lua_tointeger(L,6) ? true : false);
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res = platform_uart_setup( id, baud, databits, parity, stopbits );
|
|
|
|
lua_pushinteger( L, res );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:35:04 +01:00
|
|
|
// uart.getconfig(id)
|
|
|
|
static int l_uart_getconfig( lua_State* L )
|
|
|
|
{
|
|
|
|
uint32_t id, databits, parity, stopbits;
|
|
|
|
uint32_t baud;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-12-11 20:35:04 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( uart, id );
|
|
|
|
|
|
|
|
platform_uart_get_config(id, &baud, &databits, &parity, &stopbits);
|
|
|
|
|
|
|
|
lua_pushinteger(L, baud);
|
|
|
|
lua_pushinteger(L, databits);
|
|
|
|
lua_pushinteger(L, parity);
|
|
|
|
lua_pushinteger(L, stopbits);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2015-11-28 23:51:01 +01:00
|
|
|
// Lua: alt( set )
|
2016-12-11 20:35:04 +01:00
|
|
|
static int l_uart_alt( lua_State* L )
|
2015-11-28 23:51:01 +01:00
|
|
|
{
|
|
|
|
unsigned set;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2015-11-28 23:51:01 +01:00
|
|
|
set = luaL_checkinteger( L, 1 );
|
|
|
|
|
|
|
|
platform_uart_alt( set );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Lua: write( id, string1, [string2], ..., [stringn] )
|
2016-12-11 20:35:04 +01:00
|
|
|
static int l_uart_write( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
int id;
|
|
|
|
const char* buf;
|
|
|
|
size_t len, i;
|
|
|
|
int total = lua_gettop( L ), s;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( uart, id );
|
|
|
|
for( s = 2; s <= total; s ++ )
|
|
|
|
{
|
|
|
|
if( lua_type( L, s ) == LUA_TNUMBER )
|
|
|
|
{
|
|
|
|
len = lua_tointeger( L, s );
|
|
|
|
if( len > 255 )
|
|
|
|
return luaL_error( L, "invalid number" );
|
|
|
|
platform_uart_send( id, ( u8 )len );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
luaL_checktype( L, s, LUA_TSTRING );
|
|
|
|
buf = lua_tolstring( L, s, &len );
|
|
|
|
for( i = 0; i < len; i ++ )
|
|
|
|
platform_uart_send( id, buf[ i ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(uart, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( setup, l_uart_setup )
|
|
|
|
LROT_FUNCENTRY( getconfig, l_uart_getconfig )
|
|
|
|
LROT_FUNCENTRY( write, l_uart_write )
|
|
|
|
LROT_FUNCENTRY( on, l_uart_on )
|
|
|
|
LROT_FUNCENTRY( alt, l_uart_alt )
|
|
|
|
LROT_NUMENTRY( STOPBITS_1, PLATFORM_UART_STOPBITS_1 )
|
|
|
|
LROT_NUMENTRY( STOPBITS_1_5, PLATFORM_UART_STOPBITS_1_5 )
|
|
|
|
LROT_NUMENTRY( STOPBITS_2, PLATFORM_UART_STOPBITS_2 )
|
|
|
|
LROT_NUMENTRY( PARITY_NONE, PLATFORM_UART_PARITY_NONE )
|
|
|
|
LROT_NUMENTRY( PARITY_EVEN, PLATFORM_UART_PARITY_EVEN )
|
|
|
|
LROT_NUMENTRY( PARITY_ODD, PLATFORM_UART_PARITY_ODD )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(uart, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
NODEMCU_MODULE(UART, "uart", uart, NULL);
|