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

pybricks.tools.read_input_byte: Allow chr conversion of last byte. #244

Merged
merged 1 commit into from
Apr 4, 2024

Conversation

laurensvalk
Copy link
Member

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.

Applying chr is useful for remote controlling something with the keyboard, and simplifies the block code in pybricks/support#1574

last=True can be useful for various things, including using the arrow keys, which when combined with chr will then simply map to A/B/C/D instead of having to parse three bytes and adding logic to find the last one.

@coveralls
Copy link

coveralls commented Mar 29, 2024

Coverage Status

coverage: 56.071% (-0.08%) from 56.155%
when pulling a0d063b on work
into 46f29c0 on master.

pybricks/tools/pb_module_tools.c Outdated Show resolved Hide resolved
pybricks/tools/pb_module_tools.c Outdated Show resolved Hide resolved
if (!mp_obj_is_true(last_in)) {
break;
}
}
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@dlech dlech Apr 3, 2024

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.

Copy link
Member Author

@laurensvalk laurensvalk Apr 4, 2024

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.

@laurensvalk
Copy link
Member Author

The block for this will have a dropdown (Pybricks style) instead of two bool options so we can keep it concise while still giving a more verbose description than we could on the block.

image

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.
@laurensvalk laurensvalk merged commit a0d063b into master Apr 4, 2024
34 checks passed
@dlech dlech deleted the work branch April 4, 2024 14:52
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.

3 participants