Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By andrew melvin
#30659 I've done a bit of searching, but came up short. And this time I'm really clueless...

I have "potentially" large struct array occupying like 1K+. This is too big for EEPROM, so I'm thinking that SPIFFS will work... My initial thoughts are to take a pointer to the array and write it out byte wise to SPIFFS.. then to try and re-create it back in memory again?

Does anyone know of, have any, or willing to provide examples of doing such things with the new SPIFFS api?

thanks in advance?
User avatar
By andrew melvin
#31166 Thought I'd answer my own question... and post another

To write a struct array to SPIFFS...

Code: Select all        long start_time_spiffs = millis();

      File configFile = SPIFFS.open("/lights.conf", "w+");
        
        if (!configFile)
           {
             Serial.println(F("Failed to open test.conf"));            
           } else {
           
              Serial.println(F("Opened Hue_conf.txt for UPDATE...."));
              Serial.printf("Start Position =%u \n", configFile.position());

              unsigned char * data = reinterpret_cast<unsigned char*>(Lights); // use unsigned char, as uint8_t is not guarunteed to be same width as char...
              size_t bytes = configFile.write(data, sizeof(HueLight) * _LightCount ); // C++ way

               Serial.printf("END Position =%u \n", configFile.position());
              configFile.close();

           }


to copy data back from SPIFFS file into a struct array

Code: Select allFile LightsFile = SPIFFS.open("/lights.conf", "r");
        
        if (!LightsFile)
           {
             Serial.println(F("Failed to open lights.conf"));
           } else {
              Serial.println(F("Opened lights.conf"));
              Serial.print(F("CONFIG FILE CONTENTS----------------------"));
              Serial.println();

              uint8_t light[sizeof(HueLight)];
              size_t size = LightsFile.size();
            uint8_t counter = 0;

            for(int i=0; i<size; i+=sizeof(HueLight)){
              
                 for(int j=0;j<sizeof(HueLight);j++){
                   light[j] = LightsFile.read();
                 }
              
              HueLight *thelight = (HueLight *)light;
             HueLight *currentlight = &Lights[counter];
             memcpy (currentlight , thelight , sizeof(HueLight));

              Serial.printf( "Saved Light:%3u, %15s, State = %u, HSB(%5u,%3u,%3u) \n",
               counter, thelight->Name, thelight->State, thelight->Hue, thelight->Sat, thelight->Bri);
            
            counter++;
            }

              LightsFile.close();
           }
User avatar
By Espradio01
#79360
andrew melvin wrote:Thought I'd answer my own question... and post another

To write a struct array to SPIFFS...

Code: Select all        long start_time_spiffs = millis();

      File configFile = SPIFFS.open("/lights.conf", "w+");
        
        if (!configFile)
           {
             Serial.println(F("Failed to open test.conf"));            
           } else {
           
              Serial.println(F("Opened Hue_conf.txt for UPDATE...."));
              Serial.printf("Start Position =%u \n", configFile.position());

              unsigned char * data = reinterpret_cast<unsigned char*>(Lights); // use unsigned char, as uint8_t is not guarunteed to be same width as char...
              size_t bytes = configFile.write(data, sizeof(HueLight) * _LightCount ); // C++ way

               Serial.printf("END Position =%u \n", configFile.position());
              configFile.close();

           }


to copy data back from SPIFFS file into a struct array

Code: Select allFile LightsFile = SPIFFS.open("/lights.conf", "r");
        
        if (!LightsFile)
           {
             Serial.println(F("Failed to open lights.conf"));
           } else {
              Serial.println(F("Opened lights.conf"));
              Serial.print(F("CONFIG FILE CONTENTS----------------------"));
              Serial.println();

              uint8_t light[sizeof(HueLight)];
              size_t size = LightsFile.size();
            uint8_t counter = 0;

            for(int i=0; i<size; i+=sizeof(HueLight)){
              
                 for(int j=0;j<sizeof(HueLight);j++){
                   light[j] = LightsFile.read();
                 }
              
              HueLight *thelight = (HueLight *)light;
             HueLight *currentlight = &Lights[counter];
             memcpy (currentlight , thelight , sizeof(HueLight));

              Serial.printf( "Saved Light:%3u, %15s, State = %u, HSB(%5u,%3u,%3u) \n",
               counter, thelight->Name, thelight->State, thelight->Hue, thelight->Sat, thelight->Bri);
            
            counter++;
            }

              LightsFile.close();
           }



Hi there ..

Would you possibly have a little clearer example ?

Anyone else please ?

D