Skip to content

Commit

Permalink
AP_Scripting: add readstring and writestring to serial device
Browse files Browse the repository at this point in the history
Enables more efficient scripts.
  • Loading branch information
tpwrules committed Jun 2, 2024
1 parent a021952 commit 2719533
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libraries/AP_Scripting/AP_Scripting_SerialDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ int16_t AP_Scripting_SerialDevice::Port::device_read(void)
return -1;
}

int32_t AP_Scripting_SerialDevice::Port::device_read(uint8_t *data, uint32_t len)
{
WITH_SEMAPHORE(sem);
if (writebuffer) {
return writebuffer->read(data, len);
}
return 0;
}

int32_t AP_Scripting_SerialDevice::Port::device_write(uint8_t c)
{
WITH_SEMAPHORE(sem);
Expand All @@ -78,6 +87,15 @@ int32_t AP_Scripting_SerialDevice::Port::device_write(uint8_t c)
return 0;
}

int32_t AP_Scripting_SerialDevice::Port::device_write(const uint8_t *data, uint32_t len)
{
WITH_SEMAPHORE(sem);
if (readbuffer) {
return readbuffer->write(data, len);
}
return 0;
}

int32_t AP_Scripting_SerialDevice::Port::device_available(void)
{
WITH_SEMAPHORE(sem);
Expand Down
2 changes: 2 additions & 0 deletions libraries/AP_Scripting/AP_Scripting_SerialDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class AP_Scripting_SerialDevice
uint8_t get_protocol_index(void) { return state.protocol; }

int16_t device_read(void);
int32_t device_read(uint8_t *data, uint32_t len);
int32_t device_write(uint8_t c);
int32_t device_write(const uint8_t *data, uint32_t len);
int32_t device_available(void);

private:
Expand Down
2 changes: 2 additions & 0 deletions libraries/AP_Scripting/generator/description/bindings.desc
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,10 @@ ap_object AP_Scripting_SerialDevice::Port depends AP_SCRIPTING_SERIALDEVICE_ENAB
ap_object AP_Scripting_SerialDevice::Port method get_protocol_index uint8_t
ap_object AP_Scripting_SerialDevice::Port method device_read int16_t
ap_object AP_Scripting_SerialDevice::Port method device_read rename read
ap_object AP_Scripting_SerialDevice::Port manual readstring AP_Scripting_SerialDevice__Port__readstring 1 1
ap_object AP_Scripting_SerialDevice::Port method device_write int32_t uint8_t'skip_check
ap_object AP_Scripting_SerialDevice::Port method device_write rename write
ap_object AP_Scripting_SerialDevice::Port manual writestring AP_Scripting_SerialDevice__Port__writestring 1 1
ap_object AP_Scripting_SerialDevice::Port method device_available int32_t
ap_object AP_Scripting_SerialDevice::Port method device_available rename available

Expand Down
40 changes: 40 additions & 0 deletions libraries/AP_Scripting/lua_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,46 @@ int lua_get_serialdevice_port(lua_State *L) {

return 1;
}

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

AP_Scripting_SerialDevice::Port * port = *check_AP_Scripting_SerialDevice__Port(L, 1);

// create a buffer sized to hold the number of bytes the user wants to read
luaL_Buffer b;
const uint16_t req_bytes = get_uint16_t(L, 2);
uint8_t *data = (uint8_t *)luaL_buffinitsize(L, &b, req_bytes);

// read up to that number of bytes
const uint32_t read_bytes = port->device_read(data, req_bytes);

// push the buffer as a string, truncated to the number of bytes that were actually read
luaL_pushresultsize(&b, read_bytes);

return 1;
}

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

AP_Scripting_SerialDevice::Port * port = *check_AP_Scripting_SerialDevice__Port(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->device_write((const uint8_t*)data, req_bytes);

// return the number of bytes that were actually written
lua_pushinteger(L, written_bytes);

return 1;
}

#endif // AP_SCRIPTING_SERIALDEVICE_ENABLED

/*
Expand Down
2 changes: 2 additions & 0 deletions libraries/AP_Scripting/lua_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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_get_serialdevice_port(lua_State *L);
int AP_Scripting_SerialDevice__Port__readstring(lua_State *L);
int AP_Scripting_SerialDevice__Port__writestring(lua_State *L);
int lua_dirlist(lua_State *L);
int lua_removefile(lua_State *L);
int SRV_Channels_get_safety_state(lua_State *L);
Expand Down

0 comments on commit 2719533

Please sign in to comment.