B4R Question Tables in Flash program memory.

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody,
I have written this module to store my table in flash and retrieve it.
Is this the correct mode to handle it without side effects?
Main code
B4X:
Sub Process_Globals
    Public Serial1 As Serial
End Sub
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Dim nr As UInt=3
    statica.GetString(nr)
    
    Log(statica.stringa)
End Sub

This is module(cut off for readability):
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public stringa() As Byte
End Sub

public Sub GetString(index As UInt) As Int 
      RunNative("getdata1", index)
      Log(stringa(1))
End Sub


#if C
#include <avr/pgmspace.h>
char strx[50];
const char string_1[] PROGMEM = "R1=SCHEDA DI TARATURA";
const char string_2[] PROGMEM = "R1A=Generata il:";
const char string_3[] PROGMEM = "R1B=Ore:";
const char string_4[] PROGMEM = "R2=Parametri generali:";
const char string_5[] PROGMEM = "R3=Prodotto in uso           :";
const char string_6[] PROGMEM = "R4=Fotocellula               :";
const char string_7[] PROGMEM = "R5=Reset manuale             :";
const char string_8[] PROGMEM = "R6=Tempo di oscuramento      :";
const char string_9[] PROGMEM = "R7=Stop su errore            :";
const char string_10[] PROGMEM = "R8=Tempo stabilizzazione     :";
const char string_11[] PROGMEM = "R9=Timeout fotocellula       :";
const char string_12[] PROGMEM = "R10=Errore mancata espulsione :";
const char string_13[] PROGMEM = "R11=Tempo sincronismo contatti:";
const char string_14[] PROGMEM = "R12=Inserita";
const char string_15[] PROGMEM = "R13=Disinserita";
const char string_16[] PROGMEM = "R14=Inserito";
const char string_17[] PROGMEM = "R15=Disinserito";
const char string_18[] PROGMEM = "PP1=Parametri prodotto : ";
const char string_19[] PROGMEM = "PP2=Nome:";
const char string_20[] PROGMEM = "PP3=Programma N.        :";
const char* const string_table[] PROGMEM  = {string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8, string_9, string_10, string_11, string_12, string_13, string_14, string_15, string_16, string_17, string_18, string_19, string_20}; 

B4R::Object* getdata1(B4R::Object* o) {
    //Serial.println(o->toLong());
    strcpy_P(strx, (char*)pgm_read_word(&(string_table[o->toLong()])));
    //Serial.println(strx);
    b4r_statica::_stringa->data = strx;
    int n=0;
    do
        n++;
    while (strx[n]!=0) ; 
    b4r_statica::_stringa->length = n;
    return 0;   
}
#end if
This code seems to work. I had to define stringa lenght to terminating null to correctly print it(like a string).
Regards
Mauro Zanin
 

Cableguy

Expert
Licensed User
Longtime User
Why not serialise it before saving it to memory? Strings should be avoid in b4R...
 
Upvote 0
Top