-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AP_Scripting: refresh serial bindings #27411
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -792,6 +792,7 @@ int lua_serial_writestring(lua_State *L) | |
// 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); | ||
req_bytes = MIN(req_bytes, (size_t)INT32_MAX); // ensure result fits in a Lua integer | ||
|
||
// write up to that number of bytes | ||
const uint32_t written_bytes = port->write((const uint8_t*)data, req_bytes); | ||
|
@@ -802,6 +803,21 @@ int lua_serial_writestring(lua_State *L) | |
return 1; | ||
} | ||
|
||
int lua_serial_read(lua_State *L) { | ||
binding_argcheck(L, 1); | ||
|
||
AP_Scripting_SerialAccess * port = check_AP_Scripting_SerialAccess(L, 1); | ||
|
||
uint8_t c; | ||
if (port->read(c)) { | ||
lua_pushinteger(L, c); | ||
} else { | ||
lua_pushnil(L); // error, return nil | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int lua_serial_readstring(lua_State *L) { | ||
binding_argcheck(L, 2); | ||
|
||
|
@@ -814,13 +830,13 @@ int lua_serial_readstring(lua_State *L) { | |
|
||
// read up to that number of bytes | ||
const ssize_t read_bytes = port->read(data, req_bytes); | ||
if (read_bytes < 0) { | ||
return 0; // error, return nil | ||
if (read_bytes >= 0) { | ||
// push the buffer as a string, truncated to the number of bytes actually read | ||
luaL_pushresultsize(&b, read_bytes); | ||
} else { | ||
lua_pushnil(L); // error, return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just return 0 in this case. No need to push nil. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically |
||
} | ||
|
||
// push the buffer as a string, truncated to the number of bytes actually read | ||
luaL_pushresultsize(&b, read_bytes); | ||
|
||
return 1; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to push nil specifically so that we return a value. Otherwise e.g.
string.char(port:read())
will return an empty string if there is no data available instead of raising an error that it tried to do something with nil. (Previously, it would raise an error that -1 is out of range).