Advertisement
pleasedontcode

Preferences Storage rev_04

Jun 18th, 2025
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Preferences Storage
  13.     - Source Code NOT compiled for: Arduino Nano 33 BLE
  14.     - Source Code created on: 2025-06-18 23:30:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop a BLE-based system for storing and */
  21.     /* retrieving user preferences using */
  22.     /* NanoBLEFlashPrefs library on Arduino Nano 33 BLE. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <NanoBLEFlashPrefs.h> // https://github.com/Dirk-/NanoBLEFlashPrefs
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void printPreferences(NanoBLEFlashPrefs::flashPrefs thePrefs);
  34. void printReturnCode(int rc);
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. NanoBLEFlashPrefs myFlashPrefs; // Instantiate the preferences storage object
  38.  
  39. // Structure of preferences. You determine the fields.
  40. // Must not exceed 1019 words (4076 bytes).
  41. typedef struct flashStruct
  42. {
  43.   char someString[64];
  44.   bool aSetting;
  45.   int someNumber;
  46.   float anotherNumber;
  47. } flashPrefs;
  48.  
  49. // Our preferences. All functions here can read and modify these values, but to make
  50. // them permanent, the struct must be written to flash explicitly (see below).
  51. flashPrefs globalPrefs;
  52.  
  53. void setup()
  54. {
  55.   Serial.begin(9600);
  56.   // Give user a chance to open the terminal
  57.   delay(5000);
  58.   Serial.println("----- NanoBLEFlashPrefs Test -----");
  59.  
  60.   // See if we already have a preference record
  61.   Serial.println("Read preference record...");
  62.   int rc = myFlashPrefs.readPrefs(&globalPrefs, sizeof(globalPrefs));
  63.   if (rc == FDS_SUCCESS)
  64.   {
  65.     printPreferences(globalPrefs);
  66.   }
  67.   else
  68.   {
  69.     Serial.println("No preferences found."); // This should be the case when running for the first time on that particular board
  70.     printReturnCode(rc);
  71.   }
  72.   Serial.println("");
  73.  
  74.   // Prepare preference record for writing
  75.   strcpy(globalPrefs.someString, "NanoBLEFlashPrefs Test");
  76.   globalPrefs.aSetting = true;
  77.   globalPrefs.someNumber = 42;
  78.   globalPrefs.anotherNumber = 3.14;
  79.  
  80.   // Write preference record
  81.   Serial.println("Write preferences...");
  82.   printReturnCode(myFlashPrefs.writePrefs(&globalPrefs, sizeof(globalPrefs)));
  83.   Serial.println("");
  84.  
  85.   // Read preference record
  86.   Serial.println("Read preferences...");
  87.   rc = myFlashPrefs.readPrefs(&globalPrefs, sizeof(globalPrefs));
  88.   if (rc == FDS_SUCCESS)
  89.   {
  90.     printPreferences(globalPrefs);
  91.   }
  92.   else
  93.   {
  94.     printReturnCode(rc);
  95.   }
  96.   Serial.println("");
  97.  
  98.   delay(1000);
  99.  
  100.   // Change preference record
  101.   strcpy(globalPrefs.someString, "NanoBLEFlashPrefs Test 2");
  102.   globalPrefs.aSetting = false;
  103.   globalPrefs.someNumber = 5050;
  104.   globalPrefs.anotherNumber = 2.72;
  105.  
  106.   // Write preference record
  107.   Serial.println("Write another preference record...");
  108.   printReturnCode(myFlashPrefs.writePrefs(&globalPrefs, sizeof(globalPrefs)));
  109.   Serial.println("");
  110.  
  111.   // Read preference record
  112.   Serial.println("Read preferences...");
  113.   rc = myFlashPrefs.readPrefs(&globalPrefs, sizeof(globalPrefs));
  114.   if (rc == FDS_SUCCESS)
  115.   {
  116.     printPreferences(globalPrefs);
  117.   }
  118.   else
  119.   {
  120.     printReturnCode(rc);
  121.   }
  122.  
  123.   Serial.println("");
  124.   Serial.println("Done. Press reset button to see that again or take look at");
  125.   Serial.println("the NanoBLEFlashPrefsUtils example for more info.");
  126.   Serial.println("");
  127. }
  128.  
  129. void loop()
  130. {
  131.   // In a real application, you might update preferences based on user input or events.
  132.   // For demonstration, we do nothing here.
  133. }
  134.  
  135. // Print preference record to Serial.
  136. void printPreferences(flashPrefs thePrefs)
  137. {
  138.   Serial.println("Preferences: ");
  139.   Serial.print("String: ");
  140.   Serial.println(thePrefs.someString);
  141.   Serial.print("A Setting: ");
  142.   Serial.println(thePrefs.aSetting);
  143.   Serial.print("Number: ");
  144.   Serial.println(thePrefs.someNumber);
  145.   Serial.print("Another Number: ");
  146.   Serial.println(thePrefs.anotherNumber);
  147. }
  148.  
  149. // Print return code infos to Serial.
  150. void printReturnCode(int rc)
  151. {
  152.   Serial.print("Return code: ");
  153.   Serial.print(rc);
  154.   Serial.print(", ");
  155.   Serial.println(myFlashPrefs.errorString(rc));
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement