-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
pybricks.tools.read_input_byte: Allow chr conversion of last byte. #244
Conversation
if (!mp_obj_is_true(last_in)) { | ||
break; | ||
} | ||
} |
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.
Should we be skipping non-printable ASCII characters in the loop when chr=True
?
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.
Good point. I've changed the argument order to make it clearer we first care about the first/last byte, and then look at converting to a single character string, if chr
is True
.
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.
My question is if last=False, chr=True
, and the buffer contains 3 non-printable characters and one printable character, is the desired behavoir to return None
3 times and then return the printable character? Because it seems like that's what would happen now. But the way I read the docs, it seems like the expectation would be that it skips non-printable characters and returns the first printable character under these conditions.
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.
The existing docs (below) do seem to match your description of the implementation, but I'll add some more wording to clarify.
* @param [in] last Choose @c True to read until the last byte is read
* or @c False to get the first available byte.
* @param [in] chr Choose @c True to convert the resulting byte to a
* single character string if possible, or @c False to
* return the integer value of the byte.
The default behavior remains the same, but you can now optionally do: read_input_byte(chr=True, last=True) to apply the chr() function and discard all up to the last byte.
The default behavior remains the same, but you can now optionally do:
to apply the chr() function and discard all up to the last byte.
Applying
chr
is useful for remote controlling something with the keyboard, and simplifies the block code in pybricks/support#1574last=True
can be useful for various things, including using the arrow keys, which when combined withchr
will then simply map to A/B/C/D instead of having to parse three bytes and adding logic to find the last one.