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

pbio/sys/program_load: Allow reading user program. #243

Merged
merged 1 commit into from
Apr 4, 2024
Merged

Conversation

laurensvalk
Copy link
Member

@laurensvalk laurensvalk commented Mar 28, 2024

This is an experimental pull request to facilitate the use case in https://github.com/orgs/pybricks/discussions/1567.

This extends the readable region of user data to include the user code. This seems like a reasonable case for advanced users, so we might even choose to merge this.

The write-able region is not extended.

NOTE
If your purpose is to recover an existing program, naturally you mustn't override it with a new program to use this feature. Instead, use the REPL!


The hub.system.storage method is normally used to read and store user data. Such as user light sensor calibration values. This is 512 bytes on SPIKE Prime. The user program size (4 bytes) and program follow immediately thereafter.

So, you can theoretically extract your user code using the REPL as follows:

>>> PROGRAM_START = 512 # For SPIKE Prime. Everything before this is user data.
>>> size = hub.system.storage(PROGRAM_START, read=1)[0]
>>> print(size)
61
>>> hub.system.storage(PROGRAM_START + 4, read=61) # For large programs, read it chunk by chunk.
b'0\x00\x00\x00__main__\x00M\x06\x00\x1f\x03\x01\x10hello.py\x00\x0f\x81w\x05\rHello, world!\x00`\x08\x02\x01\x11\x02#\x004\x01YQc'

@coveralls
Copy link

coveralls commented Mar 28, 2024

Coverage Status

coverage: 56.572%. remained the same
when pulling a289bb1 on read-data
into 1d0f987 on master.

@laurensvalk laurensvalk merged commit a289bb1 into master Apr 4, 2024
33 checks passed
@dlech dlech deleted the read-data branch April 4, 2024 18:43
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.

2 participants