Skip to content

Commit

Permalink
AP_Scripting: added readstring for uarts
Browse files Browse the repository at this point in the history
this is much more efficient than reading a byte at a time
  • Loading branch information
tridge committed Dec 10, 2023
1 parent 83559c4 commit d1893f0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libraries/AP_Scripting/docs/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,14 @@ function AP_HAL__UARTDriver_ud:read() end
---@param baud_rate uint32_t_ud
function AP_HAL__UARTDriver_ud:begin(baud_rate) end

--[[
read count bytes from a uart and return as a lua string. Note
that the returned string can be shorter than the requested length
--]]
---@param count integer
---@return string|nil
function AP_HAL__UARTDriver_ud:readstring(count) end


-- desc
---@class RC_Channel_ud
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Scripting/generator/description/bindings.desc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ include AP_SerialManager/AP_SerialManager.h
ap_object AP_HAL::UARTDriver depends HAL_GCS_ENABLED
ap_object AP_HAL::UARTDriver method begin void uint32_t 1U UINT32_MAX
ap_object AP_HAL::UARTDriver method read int16_t
ap_object AP_HAL::UARTDriver manual readstring AP_HAL__UARTDriver_readstring 1
ap_object AP_HAL::UARTDriver method write uint32_t uint8_t'skip_check
ap_object AP_HAL::UARTDriver method available uint32_t
ap_object AP_HAL::UARTDriver method set_flow_control void AP_HAL::UARTDriver::flow_control'enum AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE AP_HAL::UARTDriver::FLOW_CONTROL_AUTO
Expand Down
24 changes: 24 additions & 0 deletions libraries/AP_Scripting/lua_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,30 @@ int AP_HAL__I2CDevice_read_registers(lua_State *L) {
return success;
}

int AP_HAL__UARTDriver_readstring(lua_State *L) {
binding_argcheck(L, 2);

AP_HAL::UARTDriver * ud = *check_AP_HAL__UARTDriver(L, 1);

const uint16_t count = get_uint16_t(L, 2);
uint8_t *data = (uint8_t*)malloc(count);
if (data == nullptr) {
return 0;
}

const auto ret = ud->read(data, count);
if (ret < 0) {
free(data);
return 0;
}

// push to lua string
lua_pushlstring(L, (const char *)data, ret);
free(data);

return 1;
}

#if AP_SCRIPTING_CAN_SENSOR_ENABLED
int lua_get_CAN_device(lua_State *L) {

Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Scripting/lua_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ int lua_mission_receive(lua_State *L);
int AP_Logger_Write(lua_State *L);
int lua_get_i2c_device(lua_State *L);
int AP_HAL__I2CDevice_read_registers(lua_State *L);
int AP_HAL__UARTDriver_readstring(lua_State *L);
int lua_get_CAN_device(lua_State *L);
int lua_get_CAN_device2(lua_State *L);
int lua_dirlist(lua_State *L);
Expand Down

0 comments on commit d1893f0

Please sign in to comment.