Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Unable to write to file on Monster M4SK QSPI flash.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Unable to write to file on Monster M4SK QSPI flash.

by ocram on Sun Nov 21, 2021 3:17 am

I am modifying the source code for the M4SK and I need to save a configuration file on the flash (CIRCUITPY) filesystem. I was able to create the file initially on the computer and copy it over to the CIRCUITPY drive. Once there I can read it from within the code without problems. However, I want to update the file programmatically but I cannot succeed writing it out. To update the file, I am reading it into SRAM by parsing it into a StaticJsonDocument. Then I add a key/value pair, close the file on the filesystem, open it in write mode and call serializeJson(doc, file). I get no errors as far as I can tell but when I go to open the file in a text editor from the CIRCUITPY drive on the computer I get the original file, without the update and the date and time have not changed.

Here is the code I am using to perform the save:

Code: Select all | TOGGLE FULL SIZE
void saveActiveSequence(uint8_t as, char *filename)
{
   File file;
   if(file = arcada.open(filename, FILE_READ))
   {
      StaticJsonDocument<2048> doc;
      yield();
      DeserializationError error = deserializeJson(doc, file);
      yield();
        serializeJson(doc, Serial);
      if(error)
      {
        Serial.println("Sequence save error.");
        Serial.println(error.c_str());
      }
      else
      {
        file.close();
        file = arcada.open(filename, FILE_WRITE);
        Serial.print("Storing: ");Serial.println(as);
        doc["activeSequence"] = as;

        size_t serror = serializeJson(doc, Serial);
        serror = serializeJson(doc, file);
        if(serror == 0)
        {
           Serial.println("Failed to write to file.");
        }
        else
        {
           Serial.print("Saved: ");Serial.println(serror);
        }
      }
      file.close();
   }
}


Here is the corresponding output on the serial monitor:

ArcadaFileSys : open cwd '//sequence.eye'
{"hazel":10,"fish_eyes":10,"demon":10,"doom-red":10}
ArcadaFileSys : open cwd '//sequence.eye'
Storing: 3
{"hazel":10,"fish_eyes":10,"demon":10,"doom-red":10,"activeSequence":3}Saved: 71


Here is the contents of the file:

Code: Select all | TOGGLE FULL SIZE
{
  "hazel" : 10,
  "fish_eyes":10,
  "demon":10,
  "doom-red":10
}


This is what the file looks like in the File Manager, not the date and time are old, I am writing this on November 21, 2021:
Capture.PNG
Capture.PNG (1.85 KiB) Viewed 633 times


Is there a way to open a file as read/write with the Arcada::open() method? I tried both using only the FILE_WRITE flag and also oring the FILE_WRITE | FILE_READ flags but it caused the deserializeJson() call to error with an EmptyInput error. If I open the file with FILE_READ first it reads ok, but then I have to close it and re-open it in write mode. Anyway, this is not too big a deal, the real issue for me right now is figuring out why the file does not seem to be written to the filesystem.

Thanks!

ocram
 
Posts: 27
Joined: Mon Nov 19, 2012 10:06 am

Please be positive and constructive with your questions and comments.