Skip to content

Commit

Permalink
AP_Scripting: replace int return from SerialAccess:write() with bool
Browse files Browse the repository at this point in the history
Avoids the pitfall of 0 being truthy in Lua. Now, `if port:write() then`
will work as expected.
  • Loading branch information
tpwrules committed Jun 30, 2024
1 parent 353099a commit b9d7dc4
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions libraries/AP_Scripting/AP_Scripting_SerialAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ void AP_Scripting_SerialAccess::begin(uint32_t baud)
}
}

int32_t AP_Scripting_SerialAccess::write(uint8_t c)
bool AP_Scripting_SerialAccess::write(uint8_t c)
{
return (int32_t)write(&c, 1); // return value will be 0 or 1
return write(&c, 1) > 0;
}

size_t AP_Scripting_SerialAccess::write(const uint8_t *buffer, size_t size)
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Scripting/AP_Scripting_SerialAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AP_Scripting_SerialAccess {

void begin(uint32_t baud);

int32_t write(uint8_t c);
bool write(uint8_t c);
size_t write(const uint8_t *buffer, size_t size);

bool read(uint8_t &c);
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Scripting/docs/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ function AP_Scripting_SerialAccess_ud:begin(baud_rate) end

-- Writes a single byte
---@param value integer -- byte to write
---@return integer -- 1 if success else 0
---@return boolean -- true if successfully written
function AP_Scripting_SerialAccess_ud:write(value) end

-- Writes a string. The number of bytes actually written, i.e. the length of the
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Scripting/generator/description/bindings.desc
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ include AP_Scripting/AP_Scripting_SerialAccess.h
-- don't let user create access objects
userdata AP_Scripting_SerialAccess creation null -1
userdata AP_Scripting_SerialAccess method begin void int32_t 1 INT32_MAX
userdata AP_Scripting_SerialAccess method write int32_t uint8_t'skip_check
userdata AP_Scripting_SerialAccess method write boolean uint8_t'skip_check
userdata AP_Scripting_SerialAccess manual writestring lua_serial_writestring 1 1
userdata AP_Scripting_SerialAccess manual read lua_serial_read 0 1
userdata AP_Scripting_SerialAccess manual readstring lua_serial_readstring 1 1
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Scripting/tests/serial_loopback.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function test_driver_to_device()
local msg_send = "hello device"
local num_sent = 0
for ci = 1,#msg_send do
num_sent = num_sent + ser_driver:write(msg_send:byte(ci, ci))
num_sent = num_sent + (ser_driver:write(msg_send:byte(ci, ci)) and 1 or 0)
end
local msg_recv = ser_device:readstring(#msg_send)
if msg_send == msg_recv and num_sent == #msg_send then
Expand Down

0 comments on commit b9d7dc4

Please sign in to comment.