AP_Scripting: add writestring for serial

Enables more efficient scripting.
This commit is contained in:
Thomas Watson 2024-06-16 16:55:34 -05:00 committed by Peter Barker
parent d93aa15f2a
commit c18f9f733e
4 changed files with 28 additions and 0 deletions

View File

@ -1242,6 +1242,13 @@ function AP_Scripting_SerialAccess_ud:begin(baud_rate) end
---@return uint32_t_ud -- 1 if success else 0
function AP_Scripting_SerialAccess_ud:write(value) end
-- Writes a string. The number of bytes actually written, i.e. the length of the
-- written prefix of the string, is returned. It may be 0 up to the length of
-- the string.
---@param data string -- string of bytes to write
---@return integer -- number of bytes actually written, which may be 0
function AP_Scripting_SerialAccess_ud:writestring(data) end
-- Reads a single byte from the serial port
---@return integer -- byte, -1 if error or none available
function AP_Scripting_SerialAccess_ud:read() end

View File

@ -397,6 +397,7 @@ include AP_Scripting/AP_Scripting_SerialAccess.h
userdata AP_Scripting_SerialAccess creation null -1
userdata AP_Scripting_SerialAccess method begin void uint32_t 1U UINT32_MAX
userdata AP_Scripting_SerialAccess method write uint32_t uint8_t'skip_check
userdata AP_Scripting_SerialAccess manual writestring lua_serial_writestring 1 1
userdata AP_Scripting_SerialAccess method read int16_t
userdata AP_Scripting_SerialAccess manual readstring lua_serial_readstring 1 1
userdata AP_Scripting_SerialAccess method available uint32_t

View File

@ -743,6 +743,25 @@ int lua_serial_find_serial(lua_State *L) {
}
#endif // HAL_GCS_ENABLED
int lua_serial_writestring(lua_State *L)
{
binding_argcheck(L, 2);
AP_Scripting_SerialAccess * port = check_AP_Scripting_SerialAccess(L, 1);
// get the bytes the user wants to write, along with their length
size_t req_bytes;
const char *data = lua_tolstring(L, 2, &req_bytes);
// write up to that number of bytes
const uint32_t written_bytes = port->write((const uint8_t*)data, req_bytes);
// return the number of bytes that were actually written
lua_pushinteger(L, written_bytes);
return 1;
}
int lua_serial_readstring(lua_State *L) {
binding_argcheck(L, 2);

View File

@ -11,6 +11,7 @@ int AP_HAL__I2CDevice_read_registers(lua_State *L);
int lua_get_CAN_device(lua_State *L);
int lua_get_CAN_device2(lua_State *L);
int lua_serial_find_serial(lua_State *L);
int lua_serial_writestring(lua_State *L);
int lua_serial_readstring(lua_State *L);
int lua_dirlist(lua_State *L);
int lua_removefile(lua_State *L);