Unable to write to file on Monster M4SK QSPI flash.

Wearable electronics: boards, conductive materials, and projects from Adafruit!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ocram
 
Posts: 28
Joined: Mon Nov 19, 2012 10:06 am

Unable to write to file on Monster M4SK QSPI flash.

Post by ocram »

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

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

{
  "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 1135 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!

User avatar
ocram
 
Posts: 28
Joined: Mon Nov 19, 2012 10:06 am

Re: Unable to write to file on Monster M4SK QSPI flash.

Post by ocram »

Found the problem. The Arcada library prints to the serial monitor the "path" to the file passed to it on open(). The printout includes a forward slash (/) and was deceiving me because it implied that the library was adding it to the filename. Maybe it is, who knows, the point is that after adding a slash at the beginning of the file name when passed to open() the problem went away and the file was saved in the QSPI memory.

arcada.open("myFile.txt", O_RDWR | O_TRUNC); //<----- BAD!

arcada.open("/myFile.txt", O_RDWR | O_TRUNC); //<----- GOOD!

This bit of text from https://adafruit.github.io/Adafruit_Arc ... 462af08356 helped me out:
path A string with the filename path, must start with / e.g. "/roms"

Locked
Please be positive and constructive with your questions and comments.

Return to “Wearables”