Your Pages‎ > ‎Users Pages‎ > ‎


My junk pile

TIPUsing 'Parameters' with bas.rtcmem$ non-volatile memory - Electroguard 28/12/20
Bas.rtcmem$ offers 512 bytes of non-volatile memory for storing useful info that will still be available after a reboot or power fail, ideal for storing settings and user variables, or logging error info.
Originally the contents of bas.rtcmem$ will be undefined, and possibly contain unprintable garbage characters, but we can easily clear it then check it is empty using...
bas.rtcmem$ = ""
wlog bas.rtcmem$
Basically, bas.rtcmem$ is effectively one big non-volatile string variable, and like any variable, writing new data will overwrite the old data unless the new is specifically added to the beginning or end of the old, requiring some sort of parsing to extract out anything specific. Fortunately, adding and extracting specific info is made much easier by using some useful word parameter instructions, allowing only targeted info to be affected, ie: item3=value3$ etc. This requires Annex 1.42.5C or later - and be aware that parameters can be read directly from bas.rtcmem$,   eg: filename$=word.getparam$(bas.rtcmem$, "Script") works ok, but parameters cannot be written directly to bas.rtcmem$,   eg: word.setparam bas.rtcmem$, "Script", bas.filename$) does not work. 
So instead of manipulating the non-volatile memory directly, bas.rtcmem$contents need to be read into a variable of choice for manipulation, then saved back to bas.rtcmem$
In the example below the user variable is called rtcmem$ ...  and note that the two lines could be included at the top of all scripts to display any previously saved info from the previous session.rtcmem$ = bas.rtcmem$
wlog rtcmem$

These next few lines are unnecessarily verbose example, but don't underestimate what they do, because they can prevent editing an old autorunning script after forgetting to update the Config autorun filename to your latest script and thereby prevent wasting time an irrelevent old file, and they can avoid wasting time on wrong or old scripts showing in stale cached browser pages, and they can help remember what you were doing on all those dusty devices that may have been laying around for ages. ... and generally they can save a lot of wasted time and effort.
rtcmem$ = bas.rtcmem$
filename$ = word.getparam$(rtcmem$, "Script")
if filename$ <> bas.filename$ then
  wlog "WARNING: this file=" + bas.filename$  + " is different from " + filename$ + " previously run on this device"
  wlog "if not aborted, filename will be updated"
  pause 5000     'long enough to notice and take action
  word.setparam rtcmem$, "Script", bas.filename$   
  bas.rtcmem$ = rtcmem$
  wlog "filename updated"
  wlog "Same filenames"

Manipulating parameters in a user variable is particularly convenient when needing to update and/or access individual entries in different parts of the script. 
Here's a convoluted example below (explained in the comments) which aims to demonstrate the points mentioned, and hopefully offer some inspiration for how it might come in handy.
I've made the example re-iterate in a loop for lazy convenience, because scrolling back through the log iterations makes it easier to spot changes and differences (eg: incrementing Restarts).
(It needs to be pointed out that for practical use you may wish to  'Stop log' in the Editor page to prevent displayed info being scrolled off screen by unwanted html logging)
It would be a good exercise to remove the loop count and run the script, then reboot the device and reconnect, then run the script again, checking that all the info survives the reboot(s), and that the Restarts count continues to increment.
Don't expect to see any Err info, because there will not have been any errors.
if you wish to trap script Errs you should create your own subroutine to branch from onerror goto myhandler and add in the relevent Err setparam lines (and any other variables you wish to save) then write it using bas.rtcmem$ = rtcmem$, then do a reboot to restart and autorun the device before it locks up and hangs.
That could allow the device to keep working indefinitely, and recording incrementing Restarts could show how frequently things have been going wrong.

'We'll save some parameters, any of which might need to be updated independently somewhere in the script, eg: if using a breadcrumb trail
'We'll iterate a few times to show that...
' 1: first time around the contents are not valid  (you may even need to do  bas.rtcmem$ = "" first to clear out garbage
' 2: successive iterations have incremented the Restarts variable, and updated the time
' 3: new content can subsequently be added to the variable without losing existing info
' 4: by adding an ONERROR GOTO errorhandler branch you could record details of fatal errors
' 5: by adding a 'breadcrumb trail', you could record where fatal errors occured

for iteration = 1 to 3                                                     'remove the iteration count if you prefer to run the script manually each time
 wlog "Iteration=" + str$(iteration)
 rtcmem$ = bas.rtcmem$                                           'read non-volatime memory into our variable
 wlog rtcmem$                                                            'show contents of non-volatile memory
 restarts$ = word.getparam$(rtcmem$, "Restarts")    'extract previous saved value of Restarts
 restarts$ = str$(val(restarts$) + 1)                              'increment value of Restarts by 1
 word.setparam rtcmem$, "Restarts", restarts$          'update new value of Restarts parameter into the rtcmem$ variable
 if val(restarts$) > 1 then word.setparam rtcmem$, "Woohoo", "We've got a rolling restarts count"   'add new variable if Restarts > 0
 word.setparam rtcmem$, "Script", bas.filename$      'update the following parameter pairs with their latest data values into rtcme$
 word.setparam rtcmem$, "Flashfree", str$(flashfree)
' word.setparam rtcmem$, "Breadcrumb", breadcrumb$    
 word.setparam rtcmem$, "Ramfree", str$(ramfree)
 word.setparam rtcmem$, "Time", time$
 word.setparam rtcmem$, "Date", date$
 word.setparam rtcmem$, "Err num", str$(bas.errnum)
 word.setparam rtcmem$, "Err msg", bas.errmsg$
 word.setparam rtcmem$, "Err line", str$(bas.errline)
 word.setparam rtcmem$, "Last ResetReason", str$(bas.resetreason)
 word.setparam rtcmem$, "Background colour", "lightyellow"
 bas.rtcmem$ = rtcmem$                                            'write the rtcmem$ser variable back into non-volatile memory
 pause 1000                                                                 'simply to allow the recorded time$ to advance by 1 second
next iteration