Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Keeping state when awaking from sleep

    Scheduled Pinned Locked Moved Discussion
    sleepstate
    9 Posts 3 Posters 1.6k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G Offline
      gregcope
      last edited by

      Hi,

      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 boot.py and 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'.

      Thoughts?

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @gregcope
        last edited by

        @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.

        G 1 Reply Last reply Reply Quote 0
        • G Offline
          gregcope @jcaron
          last edited by

          @jcaron Good point.

          1 Reply Last reply Reply Quote 0
          • M Offline
            mdk22
            last edited by

            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.

            G 1 Reply Last reply Reply Quote 0
            • G Offline
              gregcope @mdk22
              last edited by

              @mdk22 can you share your lib?

              M 1 Reply Last reply Reply Quote 0
              • M Offline
                mdk22 @gregcope
                last edited by

                @gregcope

                # 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.

                J 1 Reply Last reply Reply Quote 0
                • J Offline
                  jcaron @mdk22
                  last edited by

                  @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 thing and thing1 for instance
                  • Not sure I figured the reason for the 0x200, but that means you can end up with an overflow
                  G M 2 Replies Last reply Reply Quote 0
                  • G Offline
                    gregcope @jcaron
                    last edited by

                    @jcaron thanks

                    1 Reply Last reply Reply Quote 0
                    • M Offline
                      mdk22 @jcaron
                      last edited by

                      @jcaron thanks, I will keep improving the library.

                      1 Reply Last reply Reply Quote 0

                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                      With your input, this post could be even better 💗

                      Register Login
                      • 1 / 1
                      • First post
                        Last post

                      Pycom on Twitter

                      Pycom