From f4ba635d3c3a4ed750fac32aeaff26cbee0b4d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Voborsk=C3=BD?= Date: Wed, 16 Dec 2020 12:56:55 +0100 Subject: [PATCH] Fix softuart module setup function parameters (#3348) --- app/modules/softuart.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/app/modules/softuart.c b/app/modules/softuart.c index 39559fec..0980636c 100644 --- a/app/modules/softuart.c +++ b/app/modules/softuart.c @@ -194,6 +194,7 @@ static int softuart_init(softuart_t *s) } return platform_gpio_register_intr_hook(mask, softuart_intr_handler); } + return 1; } @@ -206,30 +207,27 @@ static int softuart_setup(lua_State *L) NODE_DBG("[SoftUART]: setup called\n"); baudrate = (uint32_t)luaL_checkinteger(L, 1); // Get Baudrate from luaL_argcheck(L, (baudrate > 0 && baudrate < 230400), 1, "Invalid baud rate"); - lua_remove(L, 1); // Remove baudrate argument from stack - if (lua_gettop(L) == 2) { // 2 arguments: 1st can be nil - if (lua_isnil(L, 1)) { - tx_gpio_id = 0xFF; - } else { - tx_gpio_id = (uint8_t)luaL_checkinteger(L, 1); - luaL_argcheck(L, (platform_gpio_exists(tx_gpio_id) && tx_gpio_id != 0) - , 2, "Invalid SoftUART tx GPIO"); - } - rx_gpio_id = (uint8_t)luaL_checkinteger(L, 2); + + if (lua_isnoneornil(L, 2)) { + tx_gpio_id = 0xFF; + } else { + tx_gpio_id = (uint8_t)luaL_checkinteger(L, 2); + luaL_argcheck(L, (platform_gpio_exists(tx_gpio_id) && tx_gpio_id != 0) + , 2, "Invalid SoftUART tx GPIO"); + } + + if (lua_isnoneornil(L, 3)) { + rx_gpio_id = 0xFF; + } else { + rx_gpio_id = (uint8_t)luaL_checkinteger(L, 3); luaL_argcheck(L, (platform_gpio_exists(rx_gpio_id) && rx_gpio_id != 0) , 3, "Invalid SoftUART rx GPIO"); luaL_argcheck(L, softuart_gpio_instances[rx_gpio_id] == NULL , 3, "SoftUART rx already configured on the pin"); + } - } else if (lua_gettop(L) == 1) { // 1 argument: transmit part only - rx_gpio_id = 0xFF; - tx_gpio_id = (uint8_t)luaL_checkinteger(L, 1); - luaL_argcheck(L, (platform_gpio_exists(tx_gpio_id) && tx_gpio_id != 0) - , 2, "Invalid SoftUART tx GPIO"); - } else { - // SoftUART object without receive and transmit part would be useless - return luaL_error(L, "Not enough arguments"); - } + // SoftUART object without receive and transmit part would be useless + if ((rx_gpio_id == 0xFF) && (tx_gpio_id == 0xFF)) {return luaL_error(L, "Not enough arguments");} softuart = (softuart_t*)lua_newuserdata(L, sizeof(softuart_t)); softuart->pin_rx = rx_gpio_id;