Storing settings in the memory of the Core



  • Storing settings in the memory of the Core

    Hello! If you are fortunate enough to hold the ESP32 microcontroller in your hands (I was more fortunate and have M5Stack in my hands) from the Chinese company ESPRESSIF, then this post may be useful.

    There is a situation when it is necessary to save some parameters in non-volatile memory (for example: count the number of times the device is turned on for the entire time or save the Wi-Fi settings). This can be done with ease using the Preferences library.

    We declare an instance of the Preferences class, and there we will see ...

    The first thing we need to do is create a keychain by calling the begin method with a pair of arguments (but only with the first one): the name of the keychain and the read-only flag.

    To save a string value in memory, you need to pass the key and the value itself to a method whose name consists of two parts: the first is put and the second is the type name, for example: String. Everything is clear and understandable. True, there are still "raw" bytes without roasting, to which no one wants to assign a type. In this case, the method also takes the third argument with the number of these bytes. With this procedure, everything seems to be.

    After the value has been written, you can read using the method (whose name is similar to the previous one), where the first part will be get. This method returns the value for the key of the corresponding type. Remember the byte case? If you don’t know (or don’t remember) how many bytes are on the key, then pull the getBytesLength method with a single argument - the key, it will calculate everything and return the amount to size_t.

    If you want to remove a certain key from the keychain, then give it the only argument to the remove method.

    Do you want to bring a real marafet and clear the whole bunch? Call the clear method without any arguments!

    When you wish to complete the work with the bundle, call the end method without any arguments.

    In general, the following types are supported: Char, UChar, Short, UShort, Int, UInt, Long, ULong, Long64, ULong64, Float, Double, Bool, String and Bytes.

    I understand that I want something code, so here is a sketch. A sketch counts the number of turns on the device and displays it on the display:

    #include <M5Stack.h>
    #include <Preferences.h>
    
    Preferences preferences;
    const char* key = "OnOff";
    uint32_t count;
    
    void setup() {
      m5.begin();
      preferences.begin("MyKeyChain");
      count = preferences.getUInt(key);
      preferences.putUInt(key, count + 1);
      M5.Lcd.setTextSize(3);
      M5.Lcd.setTextColor(TFT_WHITE);
      M5.Lcd.println("Hello, Habr!");
      M5.Lcd.setTextSize(2);
      M5.Lcd.println("M5Stack Turned On:");
      M5.Lcd.setTextSize(3);
      M5.Lcd.setTextColor(TFT_RED);
      M5.Lcd.println(count);
      M5.Lcd.setTextColor(TFT_WHITE);
      M5.Lcd.setTextSize(2);
      M5.Lcd.println("times");
    }
    
    void loop() { }
    

    Library link.



  • @dimi
    Congratulations ! Thank you very much.