5
0
mirror of https://github.com/ArduPilot/ardupilot synced 2025-01-03 14:38:30 -04:00

AP_Scripting: added readstring for uarts

this is much more efficient than reading a byte at a time
This commit is contained in:
Andrew Tridgell 2023-12-03 11:32:28 +11:00
parent 4a48dc2dde
commit 75217ec1e6
4 changed files with 34 additions and 0 deletions
libraries/AP_Scripting

View File

@ -1140,6 +1140,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

View File

@ -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

View File

@ -638,6 +638,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) {

View File

@ -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);