bas.rtcmem$

If doesn't fit into any other category ....
Post Reply
barneyb
Posts: 10
Joined: Sat Jun 12, 2021 7:00 am
Has thanked: 5 times
Been thanked: 1 time

bas.rtcmem$

Post by barneyb »

hi is it possible to assign bas.rtcmem$ to variables or sectors, i see it uses rtcmem.read32(idx [, num]) and wondered if either accessing idx directly or assigning it to a var could be possible. i use sleep and wish a couple of variables to be reset after power loss, rtcmem seems ideal for this. I understand corruption of data only happens if idx <28.
thanks
cicciocb
Site Admin
Posts: 309
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 85 times
Been thanked: 197 times
Contact:

Re: bas.rtcmem$

Post by cicciocb »

Hi barneyb,
you can use the functions WORD.SETPARAM and WORD.GETPARAM to set your variables into a string buffer and then set it in the rtcmem.
The following is a code taken from the old forum that shows how manage variables with these functions and how store them into a file.
In your case, you can simply replace the file save / file read with bas.rtcmem$ i.e.
replace

Code: [Local Link Removed for Guests]

file.save "/myconf.txt", a$
with

Code: [Local Link Removed for Guests]

bas.rtcmem$ = a$ 
and replace

Code: [Local Link Removed for Guests]

a$ = file.read$( "/myconf.txt")
'with

Code: [Local Link Removed for Guests]

a$ = bas.rtcmem$

Code: [Local Link Removed for Guests]

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' how save and restore variables into a "configuration" file
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' variables to be stored
color$ = "green"
size$ = "big"
orientation$ = "left"
line$ = "first"

''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  SNIPPET 1 
'store all the variables in the file "myconf.txt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''
a$ = ""
'set the variables in a$
WORD.SETPARAM a$, "color$", color$
WORD.SETPARAM a$, "size$", size$
WORD.SETPARAM a$, "orientation$", orientation$
WORD.SETPARAM a$, "line$", line$
wlog a$ ' shows the content just for debug purposes
'save a$ in the config file
file.save "/myconf.txt", a$ 

'end

''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  SNIPPET 2
'read variable from the config file into a$
''''''''''''''''''''''''''''''''''''''''''''''''''''''
a$ = file.read$( "/myconf.txt")
wlog a$ ' shows the content just for debug purposes
'extract the variables
color$ = WORD.GETPARAM$(a$, "color$")
size$ = WORD.GETPARAM$(a$, "size$")
orientation$ = WORD.GETPARAM$(a$, "orientation$")
line$ = WORD.GETPARAM$(a$, "line$")
wlog color$, size$, orientation$, line$

'end
'''''''''''''''''''''''''''
'  SNIPPET 3
'modify only one variable
'''''''''''''''''''''''''''
'read variable from the config file into a$
a$ = file.read$( "/myconf.txt")
wlog a$ ' shows the content just for debug purposes
WORD.SETPARAM a$, "color$", "blue" ' modify the content of the variable
wlog a$ ' shows the content just for debug purposes
file.save "/myconf.txt", a$

'end

''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  SNIPPET 4
'another "dynamic" approach for reading variables
''''''''''''''''''''''''''''''''''''''''''''''''''''''
' clear the variables 
color$ = ""
size$ = ""
orientation$ = ""
line$ = ""
a$ = file.read$( "/myconf.txt")
for z = 1 to 10 ' max 10 parameters
  b$ = WORD$(a$, z, chr$(10)) 'split all the lines separated by new the line character (\n)
  if b$ = "" then exit for 'if the line is empty, stops
  b$ = replace$(b$, |=|, |="|) + |"| ' put the " around the argument (i.e. from lilo$=abc -> lilo$="abc" )
  command b$ 'execute dynamically the line setting the variable
next z
wlog color$, size$, orientation$, line$
barneyb
Posts: 10
Joined: Sat Jun 12, 2021 7:00 am
Has thanked: 5 times
Been thanked: 1 time

Re: bas.rtcmem$

Post by barneyb »

Thank you very much
its1000
Posts: 55
Joined: Thu May 20, 2021 6:57 pm
Has thanked: 1 time
Been thanked: 1 time

Re: bas.rtcmem$

Post by its1000 »

i made a test using this code, works perfect to store some text data.
even if the file is not here the file is created and the variables are correctly created in the file.

i tried to use the same code in order to store a numerical data but it does not work...

i mean :

the following code works perfect, i find the value "BLUE" store in the txt file :

color$ = "BLUE"
file.save "/myconf.txt", color$


but the following code does not work, i have an error type mismatch :

duration = 1000
file.save "/myconf.txt", duration

thank you in advance for your help.
bugs
Posts: 46
Joined: Mon Feb 08, 2021 10:10 pm
Has thanked: 7 times
Been thanked: 20 times

Re: bas.rtcmem$

Post by bugs »

HI,

Just change the numeric value to a string for storage e.g. file.save "/myconf.txt", str$(duration)
And change it back to a numeric value when you read it with duration=val(a$) [where a$ is what you retrieved with the file.read command.]

.
its1000
Posts: 55
Joined: Thu May 20, 2021 6:57 pm
Has thanked: 1 time
Been thanked: 1 time

Re: bas.rtcmem$

Post by its1000 »

very loud thank you !!!!!
i manage to do exactly what i need, with those code examples and the tips you gave me to convert...
now i just need to write the code to convert hours or minutes that are stored in decimal format to xxx hours xxx minutes of running time, as they always count lamps life in hours.

just for my personal knowledge :
it is not possible to store directly a numerical value in the eeprom of the esp? like an eeprom.write command of the arduino?
cicciocb
Site Admin
Posts: 309
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 85 times
Been thanked: 197 times
Contact:

Re: bas.rtcmem$

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Mon Jul 12, 2021 4:06 pm very loud thank you !!!!!
i manage to do exactly what i need, with those code examples and the tips you gave me to convert...
now i just need to write the code to convert hours or minutes that are stored in decimal format to xxx hours xxx minutes of running time, as they always count lamps life in hours.

just for my personal knowledge :
it is not possible to store directly a numerical value in the eeprom of the esp? like an eeprom.write command of the arduino?
In the ESP8266 and the ESP32 the eeprom is simply a block of flash memory allocated for this purpose.
So, practically speaking, writing to the eeprom or the to "disk" is the same thing.
The best approach is to write into a file as the file system allocates dynamically the new data in a different block (wear levelling) to increase the life of the flash that has a limited number of write cycles.
its1000
Posts: 55
Joined: Thu May 20, 2021 6:57 pm
Has thanked: 1 time
Been thanked: 1 time

Re: bas.rtcmem$

Post by its1000 »

thank you for the precision.
with the help of everyone on this topic, I perfectly manage to do it with file. so it will be ok.
is there any integrated function that convert minutes in decimal in hours and minutes base 60? or do i need to make the calculation ?
Post Reply