Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

tpwrules
Copy link
Contributor

Removes some annoyances with unnecessary uint32_t_ud and pitfalls with -1 return values. This does technically break compatibility, not sure if there is any policy on that. I tried to edit the scripts in the repo to fix them but user scripts will need to be adjusted in minor ways.

Tested in SITL and on Cube Orange with my REPL script.

tpwrules added 2 commits June 27, 2024 22:58
Avoids the hassle of having to convert results to regular numbers in
scripts. We trust the user will not be processing 2GiB of data in one
call or attempting to go past 2 gigabaud.
Reduces likelihood of programming errors caused by forgetting to check
the return value.

Use a manual binding to ensure we return nil instead of nothing.
@tpwrules tpwrules requested review from IamPete1 and peterbarker June 28, 2024 04:29
// 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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@tpwrules tpwrules Jun 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically return 0 does not actually result in returning nil. It's not a big deal here but is more of a problem for port:read().

if (port->read(c)) {
lua_pushinteger(L, c);
} else {
lua_pushnil(L); // error, return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return 0

Copy link
Contributor Author

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).

libraries/AP_Scripting/AP_Scripting_SerialAccess.cpp Outdated Show resolved Hide resolved
Avoids the pitfall of 0 being truthy in Lua. Now, `if port:write() then`
will work as expected.
@IamPete1
Copy link
Member

IamPete1 commented Jul 1, 2024

I do think this is a improvement, the key thing we have to decide is if were OK with the breaking change.

Copy link
Contributor

@tridge tridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't break all the existing scripts that use serial ports

@tpwrules
Copy link
Contributor Author

tpwrules commented Jul 5, 2024

Yeah, the breakage would be unfortunate.

We could allow ourselves to remove uint32_t_ud return values by adding a metatable to numbers with the same methods. We also could deprecate the current serial read and add a new one with a new name.

It's questionable whether these are worth the flash space. They are not a high priority for me right now in any case.

@tpwrules tpwrules closed this Jul 5, 2024
@tpwrules tpwrules deleted the pr/lua-serial-refresh branch July 5, 2024 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants