Replies: 1 comment 6 replies
-
Wouldn't you generally want persistent storage... to persist? Persistent storage can be useful to store things such as color sensor calibration values, precisely so you don't have to calibrate again after loading a program.
More generally, you could have your data and add some kind of checksum.
This is probably something we could/should fix. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to implement saving calibration data and reloading it afterwards. I can see from the docs that I can use
hub.system.storage
for that purpose and that there are various helpers for conversion of data to and from bytes, seems easy to use. However, I have a problem with detecting whether the storage has been initialized or not, and would like to ask for advice in it.In general, what I'd like to do is try to read calibration data in the beginning of the program, and if it's already there use it, otherwise do the calibration and save the data. However, as far as I can see the storage contains random data even after installing Pybricks, so I cannot rely on its contents. Ideally, the way I could imagine it to work is that the storage could be reset (for example cleared to zero) when certain actions happen (for example optionally when a new program is uploaded to the hub). That would allow me to at least detect that the storage is empty (assuming that the data I want to store is never all zeros).
Or is there another/better way of detecting whether the storage has already been written into since a certain event?
If not, I think it could be optional to clear the storage upon uploading the program, because sometimes I don't need the storage to be cleared if I am experimenting with other aspects of the program. However, a feature that would be good to have is that if I publish a program, and somebody else loads it on their hub (that maybe had already Pybricks installed), then it should still work correctly. This is why I think loading a new program would be a good place to clear the storage. This could be the default, and could be opted out from by those who understand what they are doing.
So far, the only solution I could come up with is writing a magic number into the first few bytes of the storage, and checking that upon reading.. This way on first startup of the program the storage will not have the magic number with high probability, which signals the need to calibrate and save the data. On next startup, the magic will be there and calibration will not be necessary. Works, but could be improved/simplified.
Beta Was this translation helpful? Give feedback.
All reactions