Arrays in preferences file

FinnHansen
Posts: 1
Joined: Sat Sep 14, 2024 9:39 am

Arrays in preferences file

Postby FinnHansen » Sun Sep 15, 2024 9:13 pm

Hi.
I do need help with preferences.

I have a number of midi data, i need to store in preferences, or something like that.
My problem is how to store arrays within an array ?
I'm completly lost now.

Thanks for any help.
  1.  
  2. #include <Preferences.h>
  3.  
  4. #define RO_MODE true
  5. #define RW_MODE false
  6.  
  7. void setup() {
  8.  
  9.     Preferences midiPrefs;
  10.  
  11.     Serial.begin(115200);
  12.     delay(250);
  13.  
  14.     midiPrefs.begin("myPrefs", RW_MODE);  
  15.     midiPrefs.clear();                    
  16.  
  17.  
  18.     int16_t myArray1[3][4] = { {192, 3, 45, 0} , {176, 15, 95, 5} , {176, 3, 65, 3}};
  19.     //int16_t myArray2[3][4] = { {192, 3, 45, 0} , {176, 15, 95, 5} , {176, 3, 65, 3}};
  20.     //int16_t myArray3[3][4] = { {192, 3, 45, 0} , {176, 15, 95, 5} , {176, 3, 65, 3}};
  21.  
  22.     Serial.println("Printing myArray1...");
  23.     for (int i = 0; i < 3; i++) {
  24.         Serial.print(myArray1[i][0]); Serial.print(", ");
  25.     }
  26.     Serial.println("\r\n");
  27.  
  28.    
  29.  
  30. }
  31.  
  32. void loop() {
  33.  
  34. }

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Arrays in preferences file

Postby lbernstone » Mon Sep 16, 2024 3:00 am

If it is a fixed structure (ie, the elements of the main array are all identical), you can cast a byte blob into an array of the right size.
If it is variable length strings, or an indeterminate complex structure, you can add them as sequenced elements, and keep the total count (or a list of handles) as a separate piece of state data.

Who is online

Users browsing this forum: No registered users and 50 guests