Keeping state when awaking from sleep
When sleeping other micro controllers (e.g. ATMEL) pick up where they left off, with state mostly the same (ie pin states, memory structures, but usually not the clock - depends on mode). This makes sleeping and saving power easy.
As the pycom boards seem to restart (ie (re)run
main.py) when re-awaking from sleep what are peoples thoughts on maintaining state?
For example you can use
pin.hold([hold])to fix pin state which is good.
What about variables or state? Use
pycom.nvs_set(key, value)? However this can only hold 32 bit ints'.
@jcaron thanks, I will keep improving the library.
@mdk22 A few comments:
- You should probably store the length separately
- You should add a (documented) separator between the topic and the index. Otherwise you may end up with collisions when you store
- Not sure I figured the reason for the
0x200, but that means you can end up with an overflow
# Written by Mayunk Kulkarni import binascii from pycom import nvs_set, nvs_get class NvsStore(): # this class basically stores the string and topic provided in the nvram # given the fact that the string sizes can be variable. Using a variety of # functions to convert string in to int and int back to string we use this # class to store a topic and string in a series of nvram cells def __init__(self, topic, stringToStore): self.string = stringToStore self.topic = topic self.__Init(self.string, self.topic) def __Init(self, string, topic): # step 1: split string into a list of strings each of maximum 4 chars # step 2: store the topic and string in the nvram cell and use str2int # to shift each list componenent into nvram self.strSplit(string) self.store(topic) def str2int(self, string): x = binascii.hexlify(string) x = int(x, 16) x = x + 0x200 return x def store(self, topic): # store list values into nvram topic spaces for i in range(len(self.strlist)): nvs_set(topic+str(i), self.str2int(self.strlist[i])) def strSplit(self, string): # split string into 4 chars each and put it in a list self.strlist =  temp = string while temp != "": if len(temp) < 4 or len(temp) == 4: self.strlist.append(temp) break self.strlist.append(temp[len(temp)-4:len(temp)]) temp = temp[:len(temp)-4] def intcount(self, val): # old function used for debugging y = val i = 0 while int(y/10) != 0: y = y / 10 i+=1 return i class NvsExtract(): # This class will be used to extract and return string values back to # pycom for their use in lora, wifi, LTE services. The topic keys are # constant hence we can find the strings yet we need to use try/except # to catch error for finding limit of a certain topic string def __init__(self, topic): self.topic = topic self.__Init(self.topic) def __Init(self, topic): self.extractInt(topic) def extractInt(self, topic): # extract integer from nvram topic and put string in self.string cnt = 0 self.string = "" x =  while True: if nvs_get(topic+str(cnt)) == None: break else: x.append(self.int2str(nvs_get(topic+str(cnt))).decode("utf-8")) cnt += 1 while len(x) > 0: self.string = self.string + x[len(x) -1] x = x[:len(x)-1] temp = self.string def retval(self): return self.string def int2str(self, integer): #convert integer obtained into character string integer = integer - 0x200 integer = hex(integer) return binascii.unhexlify(integer[2:]) test = False if test == True: NvsStore("ssid", "one") NvsExtract("ssid")
Here it is, let me know any suggestions or issues, I am new to this developer community.
@mdk22 can you share your lib?
I had to create a library myself to save a string of streams in the nvram, along with this, the soft reset option can keep hardware states saved.
@jcaron Good point.
@gregcope Depending on what you need to save exactly, you can also use the flash file system to save/restore data. Probably not a good idea if you save on each sleep and you wake up often, but for data that does not change too much, it should be a good option.