store a single var in flash and load at start

Posted on
  • Hello,

    I'm looking for to store a var in a permanent way (power off) and load at start (power on)
    i saw that @Gordon implement a way to store data since 2v05, but the example code is for log datas (implementative way) and it seems a beat complex just for store a counter var.

    have you a simple example to implement a counter (with a long type) storage over power-down?

    I've tried

    var memoire = require("Storage");
    [...]
    let NumPoemeEncours = memoire.read("num"); // numero encours
      if (NumPoemeEncours == null || NumPoemeEncours == undefined  || NumPoemeEncours == "") {
          NumPoemeEncours = 1;
          console.log("numero du poeme inconnu", memoire.read("num"));
          memoire.write("num",NumPoemeEncours);
          console.log("numero du poeme connu", memoire.read("num"));
      }
    console.log("NumPoemeEncours:",NumPoemeE­ncours);
    

    but it does not works!

    what i'm doing wrong?

    thanks for your time.

  • Sat 2021.09.11

    https://www.espruino.com/Reference#Stora­ge

    Simple sample from above link:

    var f = require("Storage");
    f.write("a","Hello",0,14);
    f.write("a"," ",5);
    f.write("a","World!!!",6);
    print(f.read("a")); // "Hello World!!!"
    



    Add a counter:

    var nCount = 0;
    
    nCount++;
    
    f.write("n",nCount.toString());
    console.log("count is: ", f.read("n"));
    
    nCount++;
    nCount++;
    nCount++;
    
    f.write("n",nCount.toString());
    console.log("count is: ", f.read("n"));
    
    console.log(f.read("a"));
    


    Now, type save() archiving that code block to permanent storage.

    reset();
    

    Reset device or power off / power on, then type in L-Hand console of WebIDE

    console.log("count is: ", f.read("n"));
    
    console.log(f.read("a"));
    

    Output:

    count is:  4
    Hello World!!!
    >
    
  • check Combining options

    https://www.espruino.com/Saving

  • Thanks @Robin,
    I started with the same example, but i missed the ‘toString()’ to convert my counter in String in my attempt.

    It works now...

    thanks again.

    Regards

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

store a single var in flash and load at start

Posted by Avatar for Mrbbp @Mrbbp

Actions