Clear all

python bytes to integer and back

1 Posts
1 Users
Estimable Member
Joined: 2 years ago
Posts: 148
Topic starter  

I working on a program in circuitpython on a Adafruit Metro ESP32-S2 and trying to use the builtin NVM that is powered during a deep sleep. For now I'm just using it as a loop counter.  Since coming out of deep sleep is almost like a reset this Alarm sleep memory is all that is preserved.

I used 1 byte as the LSB of an integer and the next byte as the MSB. So far I have not observed it needed to use the upper byte so I don't know if my code is working.

Does the code below look okay?

loop_count = alarm.sleep_memory[5] | alarm.sleep_memory[6] << 8
loop_count += 1
alarm.sleep_memory[6] = loop_count >> 8
alarm.sleep_memory[5] = loop_count & 255

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.