ספריית NVS / Preferences מאפשרת לך לאחסן הגדרות וקטעי מידע במחיצת NVS בזיכרון ה-flash. זה אומר שהגדרות אלה יכולות להיות מוגדרות ונקראות ללא אובדן בעת הפעלה מחדש או במקרה של אובדן חשמל. אתה יכול לאחסן רוב סוגי המשתנים גם בגרסאות חתומות וגם לא חתומות באמצעות ספריה זו.
בדוגמה זו אנו גורמים לכפתור ההגדרה לפעול כלחצן הפעלה/כיבוי עבור ה-LED. אתה יכול גם להוסיף שינה עמוקה אם היא במצב כיבוי כדי לחסוך בחשמל אבל יש עוד על כך מאוחר יותר. בכל פעם שאתה לוחץ על כפתור החשמל, ה-Neopixel יהיה או דלוק או כבוי בהתאם למצב הטוגל.
ספריית Preferences תומכת בכל סוגי המשתנים הנפוצים. למספרים שלמים השתמש ב-prefs.putInt("key", 42) וב-prefs.getInt("key", 0) כאשר הפרמטר השני הוא ברירת המחדל אם המפתח לא קיים. עבור מספרים שלמים לא חתומים יש putUInt / getUInt, שימושי לעדכונים כמו prefs.putUInt("boots", bootCount). מחרוזות מאוחסנות עם prefs.putString("wifi_ssid", "MyNetwork") והן מוחזרות עם prefs.getString("wifi_ssid", ""). לערכי נקודה צפה השתמש ב-prefs.putFloat("calibration", 1.0523) וב-prefs.getFloat("calibration", 1.0) — שימושי לקיזוזי חיישנים. ערכים בוליאניים משתמשים ב-prefs.putBool("enabled", true) וב-prefs.getBool("enabled", false). כדי להסיר מפתח יחיד קרא ל-prefs.remove("key"), או כדי למחוק מרחב שמות שלם השתמש ב-prefs.clear(). כל מרחב שמות מוגבל ל-15 תווים ומפתחות ל-15 תווים. תמיד קרא ל-prefs.end() כאשר סיימת כדי לשחרר משאבים.
/* * Mercury NVS Preferences Example * Toggles a value (0/1) in flash memory each time the board starts. * If 1: NeoPixel LEDs turn green. If 0: LEDs stay off. * * The value survives power cycles and reprogramming. * Reset by uploading new code that clears the namespace. * * No external libraries needed — Preferences and NeoPixel * are built into the ESP32 Arduino framework. */ #include "Preferences.h" #include "Adafruit_NeoPixel.h" #include "Mercury_Pins.h" Preferences prefs; Adafruit_NeoPixel pixels(4, LED, NEO_GRB + NEO_KHZ800); void setup() { Serial.begin(115200); delay(500); // Power on NeoPixel pinMode(LEDPOWER, OUTPUT); digitalWrite(LEDPOWER, HIGH); delay(10); pixels.begin(); pixels.clear(); pixels.show(); // Open NVS namespace ("demo" can be any name, up to 15 chars) prefs.begin("demo", false); // false = read/write mode // Read the stored value (defaults to 0 if not yet saved) int state = prefs.getInt("toggle", 0); // Flip it int newState = (state == 0) ? 1 : 0; // Save the new value to flash prefs.putInt("toggle", newState); prefs.end(); Serial.println("Previous state: " + String(state)); Serial.println("New state: " + String(newState)); if (newState == 1) { // All 4 pixels green for (int i = 0; i < 4; i++) { pixels.setPixelColor(i, pixels.Color(0, 30, 0)); } pixels.show(); Serial.println("LEDs: GREEN (reset to toggle off)"); } else { Serial.println("LEDs: OFF (reset to toggle on)"); } } void loop() { // Nothing to do — just demonstrating NVS storage delay(1000); }#pragma once /* * Mercury (ESP32-C6) Pin Definitions * Board-specific GPIO assignments */ // ── Status LED (NeoPixel) ── #define LEDPOWER 3 // NeoPixel power (drive HIGH to enable) #define LED 2 // NeoPixel data signal // ── I2C Bus ── #define SDA 21 // I2C data #define SCL 22 // I2C clock // ── Sensor Power ── #define VACC 20 // Sensor power rail (drive HIGH to enable) // ── General Purpose Ports ── #define GP06 6 // GP06 port #define GP07 7 // GP07 port // ── High Current Output ── #define OUT1 5 // High current output (e.g. pyro / relay) // ── Battery Bar LEDs ── #define BL1 4 // Battery LED 1 (lowest) #define BL2 14 // Battery LED 2 #define BL3 15 // Battery LED 3 #define BL4 18 // Battery LED 4 #define BL5 19 // Battery LED 5 (highest) // ── Indicators ── #define DISK 8 // Disk activity LED // ── Analogue / Detection ── #define BATIN 0 // Battery voltage (1:1 divider) #define USBDETECT 1 // USB power detect (HIGH = USB present) #define BUTTON 9 // BUTTON on the board, boot button but can be used