From b9d7dc48cbea58950d11148cb05c8cde5d7f6800 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Sun, 30 Jun 2024 11:28:47 -0500 Subject: [PATCH] AP_Scripting: replace int return from SerialAccess:write() with bool Avoids the pitfall of 0 being truthy in Lua. Now, `if port:write() then` will work as expected. --- libraries/AP_Scripting/AP_Scripting_SerialAccess.cpp | 4 ++-- libraries/AP_Scripting/AP_Scripting_SerialAccess.h | 2 +- libraries/AP_Scripting/docs/docs.lua | 2 +- libraries/AP_Scripting/generator/description/bindings.desc | 2 +- libraries/AP_Scripting/tests/serial_loopback.lua | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/AP_Scripting/AP_Scripting_SerialAccess.cpp b/libraries/AP_Scripting/AP_Scripting_SerialAccess.cpp index 678fc87022aca..d8a47f731fd9f 100644 --- a/libraries/AP_Scripting/AP_Scripting_SerialAccess.cpp +++ b/libraries/AP_Scripting/AP_Scripting_SerialAccess.cpp @@ -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) diff --git a/libraries/AP_Scripting/AP_Scripting_SerialAccess.h b/libraries/AP_Scripting/AP_Scripting_SerialAccess.h index de6c1d6407373..c3dec6d18c72f 100644 --- a/libraries/AP_Scripting/AP_Scripting_SerialAccess.h +++ b/libraries/AP_Scripting/AP_Scripting_SerialAccess.h @@ -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); diff --git a/libraries/AP_Scripting/docs/docs.lua b/libraries/AP_Scripting/docs/docs.lua index ac4668ea7075f..035919c5a9f3e 100644 --- a/libraries/AP_Scripting/docs/docs.lua +++ b/libraries/AP_Scripting/docs/docs.lua @@ -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 diff --git a/libraries/AP_Scripting/generator/description/bindings.desc b/libraries/AP_Scripting/generator/description/bindings.desc index 4ea1147e51f3d..85b5fdd50765a 100644 --- a/libraries/AP_Scripting/generator/description/bindings.desc +++ b/libraries/AP_Scripting/generator/description/bindings.desc @@ -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 diff --git a/libraries/AP_Scripting/tests/serial_loopback.lua b/libraries/AP_Scripting/tests/serial_loopback.lua index 5c39656a4c4bd..b84622b6fdba9 100644 --- a/libraries/AP_Scripting/tests/serial_loopback.lua +++ b/libraries/AP_Scripting/tests/serial_loopback.lua @@ -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