micro:bit v2 micropython: method to instantiate/reuse persistent 64-byte buffer #13174
-
Environment: micro:bit v2 micropython (current December 2023 version) Is there a method in this environment to declare and obtain a pointer to For example: _global_ba = bytearray(64) ... _global_ba[:] = data ... no go, as item assignment is not supported def msgDst(): # return a token from the message based on the message protocol while True: Based on my research, I am not entirely clear why the bytearray class Obviouly, (micro)python is not my forte, but I am trying to establish control If this is not doable, is there another method to achieve the objective that Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@burksfamly you need to see if your getRadioMsgBytes() function has an alternative, which reads into a pre-allocated buffer. SPI, for example, has a read_into() method which uses a pre-allocated buffer. micropython will never really extend a bytearray; instead, it has to create a new one and copy. getRadioMsgBytes is just returning an entirely new buffer and micropython then deallocates the old one since you trampled its name. note that your line |
Beta Was this translation helpful? Give feedback.
@burksfamly you need to see if your getRadioMsgBytes() function has an alternative, which reads into a pre-allocated buffer. SPI, for example, has a read_into() method which uses a pre-allocated buffer. micropython will never really extend a bytearray; instead, it has to create a new one and copy. getRadioMsgBytes is just returning an entirely new buffer and micropython then deallocates the old one since you trampled its name.
note that your line
_global_ba[:] = data #... no go, as item assignment is not supported
would work fine if you assigned to the memoryview instead, and as long as the RHS was an array, i.e._global_mv_ba[:] = data
.