Reading/Writing internal flash


#1

Particle Photons here. Porting all my projects over to Zerynth from particle ide, well, because of python.

Stuck on a small part of just writing a couple values to internal flash as constants options for my script to use for that particular device. (ie number of leds in a strip, different with each device but using the same code).

I just need a couple int values and a boolean or two (which i could just save as int 1 or 0)

I have been playing with the internal flash library, and all the functions operate, but i cannot get consistent results. (or im not writing to the flash correctly)

after a:
data = flash.FlashFileStream(0x80E0000,32)
i can see byte data:
print(data[0])
print(data[1]) etc…

i have also tried:
data.read_int() with some results

but how can i write to those bytes so i can read them back how i wrote them?
i do a:
data.write(buf)
but im not sure how it wants that buf data
data.write(‘99’) doesn’t seem to do what you would expect
its not like i can read back data[0] and get 99

or how would i write an int to flash so that data.read_int() works?

the flash library example gets into json and writes back the length before the data, but i dont need anything fancy, just simple int value stored i can read back (even if it was a string i could just parse my values out of)

can someone toss me an example maybe of how i could just write and read simple int values saved to internal flash?


#2

Hi Rykker,

If you want to write an integer you can simply try with:

my_int = 99
data.write(my_int)
data.flush()

unfortunately the documentation of this feature is missing, we will try to fix it as soon as possible.
Anyway this instruction will convert the integer 99 to four bytes (little endian format) and write them starting from current position: you will be able to retrieve it later with a read_int call.

Otherwise you could have tried, following the documentation, to write it manually converting it to a bytearray and passing it to the write function:

my_int = 99
buf = bytearray(4)
buf[0] = my_int & 0xff
buf[1]= (my_int >> 8) & 0xff
buf[2]= (my_int >> 16) & 0xff
buf[3]= (my_int >> 24) & 0xff
data.write(buf)
data.flush()

Let me know if this helps :slight_smile: