Skip to content

Commit f4ba635

Browse files
authored
Fix softuart module setup function parameters (#3348)
1 parent 432fe49 commit f4ba635

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

app/modules/softuart.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ static int softuart_init(softuart_t *s)
194194
}
195195
return platform_gpio_register_intr_hook(mask, softuart_intr_handler);
196196
}
197+
return 1;
197198
}
198199

199200

@@ -206,30 +207,27 @@ static int softuart_setup(lua_State *L)
206207
NODE_DBG("[SoftUART]: setup called\n");
207208
baudrate = (uint32_t)luaL_checkinteger(L, 1); // Get Baudrate from
208209
luaL_argcheck(L, (baudrate > 0 && baudrate < 230400), 1, "Invalid baud rate");
209-
lua_remove(L, 1); // Remove baudrate argument from stack
210-
if (lua_gettop(L) == 2) { // 2 arguments: 1st can be nil
211-
if (lua_isnil(L, 1)) {
212-
tx_gpio_id = 0xFF;
213-
} else {
214-
tx_gpio_id = (uint8_t)luaL_checkinteger(L, 1);
215-
luaL_argcheck(L, (platform_gpio_exists(tx_gpio_id) && tx_gpio_id != 0)
216-
, 2, "Invalid SoftUART tx GPIO");
217-
}
218-
rx_gpio_id = (uint8_t)luaL_checkinteger(L, 2);
210+
211+
if (lua_isnoneornil(L, 2)) {
212+
tx_gpio_id = 0xFF;
213+
} else {
214+
tx_gpio_id = (uint8_t)luaL_checkinteger(L, 2);
215+
luaL_argcheck(L, (platform_gpio_exists(tx_gpio_id) && tx_gpio_id != 0)
216+
, 2, "Invalid SoftUART tx GPIO");
217+
}
218+
219+
if (lua_isnoneornil(L, 3)) {
220+
rx_gpio_id = 0xFF;
221+
} else {
222+
rx_gpio_id = (uint8_t)luaL_checkinteger(L, 3);
219223
luaL_argcheck(L, (platform_gpio_exists(rx_gpio_id) && rx_gpio_id != 0)
220224
, 3, "Invalid SoftUART rx GPIO");
221225
luaL_argcheck(L, softuart_gpio_instances[rx_gpio_id] == NULL
222226
, 3, "SoftUART rx already configured on the pin");
227+
}
223228

224-
} else if (lua_gettop(L) == 1) { // 1 argument: transmit part only
225-
rx_gpio_id = 0xFF;
226-
tx_gpio_id = (uint8_t)luaL_checkinteger(L, 1);
227-
luaL_argcheck(L, (platform_gpio_exists(tx_gpio_id) && tx_gpio_id != 0)
228-
, 2, "Invalid SoftUART tx GPIO");
229-
} else {
230-
// SoftUART object without receive and transmit part would be useless
231-
return luaL_error(L, "Not enough arguments");
232-
}
229+
// SoftUART object without receive and transmit part would be useless
230+
if ((rx_gpio_id == 0xFF) && (tx_gpio_id == 0xFF)) {return luaL_error(L, "Not enough arguments");}
233231

234232
softuart = (softuart_t*)lua_newuserdata(L, sizeof(softuart_t));
235233
softuart->pin_rx = rx_gpio_id;

0 commit comments

Comments
 (0)