Dirk- / NanoBLEFlashPrefs

Substitute for the missing EEPROM storage on Arduino Nano 33 BLE and BLE Sense.
GNU General Public License v2.0
16 stars 1 forks source link

Minimum number of fields/bytes in struct? #3

Closed alexpopof closed 2 years ago

alexpopof commented 2 years ago

The example code works great and this library is cool. I was trying to reduce the sample to the smallest possible example. To do this, I commented out all fields other than the first one (and updated the read/write code accordingly, of course):

typedef struct flashStruct {
    char someString[64];
//    bool aSetting;
//    int  someNumber;
//    float anotherNumber;
} flashPrefs;

Interestingly, in the serial output, it will just hang on "Write preferences...". I added back the int field and things worked as expected. Are there certain requirements for the struct used in your library?

Dirk- commented 2 years ago

There should not be a minimum number of fields. Can you try deletePrefs() before writePrefs() after changing the struct?

alexpopof commented 2 years ago

Thanks for the quick response! I did try this, including garbage collection, by uncommenting that section of your code. For clarity, the code and output are below. Perhaps I am missing something obvious, but it definitely seems strange. run2.txt run2

Dirk- commented 2 years ago

You found a bug in the logic of the library. I will try to fix it ASAP.

Dirk- commented 2 years ago

I think I fixed it. Can you try the current library code? Just replace NanoBLEFlashPrefs.h and NanoBLEFlashPrefs.cpp in /Users/<you>/Documents/Arduino/libraries/NanoBLEFlashPrefs/src/ with the new files from the repository.

Also, remove all the waiting code

    // Wait until completion
    while (!myFlashPrefs.operationCompleted()) {
    }

from your Arduino sketch. The functions are all synchronous now.

Please let me know if it works, I will then update the rest of the repository to reflect the changes.

alexpopof commented 2 years ago

Functionally, the fix seems good for the four-item struct and the single-item struct (i.e. only char someString[64];).

Other minor comments you may be aware of already but figured I would mention:

In any case, thanks again for the quick response and fix. The synchronous change is a nice bonus too!

Dirk- commented 2 years ago
  • I think your new sample .ino is missing the lines to actually write the prefs
  • Upon compilation, there are a few warnings -- most looked benign but a clean compile is always nice
  • writePrefs still has some debugging code Serial.println(record.data.length_words);

Yes, I was in a hurry. This should all be fixed now and version 1.1 of the library should appear in the Arduino library manager soon.

Thanks for your bug report!

alexpopof commented 2 years ago

Sounds great, and happy to help.