// - NVS-NVS- NVS-NVS- NVS-NVS- NVS-NVS- NVS-NVS----------------------
#include <Preferences.h>
#include "nvs_flash.h"
#include "common.h"
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#define READWRITEnvs false // Read Write NVS
#define READONLYnvs true // Read Only NVS
#define NVS_VERSION 105 // my NVS Version
/* Namespace name is limited to 15 characters. */
#define NVS_NAMESPACE_VERSION "nvsVer" // namespace for Version
#define NVS_NAMESPACE_PREAMP "nvs_Preamp"
#define NVS_NAMESPACE_VER "nvs_version"
#define NVS_NAMESPACE_SEL_IN "Select_in"
#define NVS_NAMESPACE_PREAP_VOL "Preap_Volume"
#define NVS_NAMESPACE_BUZ_VOL "Buzzer_Volume"
#define NVS_NAMESPACE_BL_BRIGHT "BL_Brightness"
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
uint8_t nvsVersion;
Preferences NVSprefs;
/////////////////////////////////////////////////////////////////////////
//=======================================================================
// nvsLoadVersion
void nvsLoadVersion() {
nvsVersion = NVSprefs.getUChar(NVS_NAMESPACE_VERSION);
NVSprefs.end(); // Close the Preferences nvsLoadVersion
#if DEBUGLEVEL > 2 /* Serial Debugging */
printf("\r------ Load NVS Version -------------------------------\n");
printf("[info] namespace : %s \r\n", NVS_NAMESPACE_VERSION);
printf("\t%-13s: %d \r\n", "nvs version", nvsVersion);
PRINT_DASHED_LINES;
#endif //DEBUGLEVEL > 2
if (nvsVersion != NVS_VERSION) {
#if DEBUGLEVEL > 0 /* Serial Debugging */
printf("-⚠️ Different version of NVS found-\n\r");
printf("\tExisting Version : %d\n", nvsVersion);
printf("\tFound this Version : %d\n", NVS_VERSION);
printf("--⚠️ Erases NVS flash and\n\tinitialize the NVS partition\n>>>> Now Reboot ESP <<<<\n\r");
PRINT_DASHED_LINES;
#endif // DEBUGLEVEL > 0
// To completely erase and reformat the NVS memory used by Preferences
nvs_flash_erase(); // erase the NVS partition and...
nvs_flash_init(); // initialize the NVS partition.
delay(100);
ESP.restart();
delay(1000);
}
}
//=======================================================================
void nvsSaveNewValues() {
if (millis() - DelayTimeStoreToNVS > 10000) {
NVSprefs.begin(NVS_NAMESPACE_PREAMP, READWRITEnvs);
FlagStoreToNVS = false;
DelayTimeStoreToNVS = millis();
if (oldSelect_in != Select_in) {
oldSelect_in = Select_in;
NVSprefs.putUChar(NVS_NAMESPACE_SEL_IN, Select_in);
#if DEBUGLEVEL > 4 // Serial Debugging
DEBUG_F("New Store in nvs Select in: %s\n", inputName[Select_in]);
#endif //DEBUGLEVEL > 4
}
if (oldVolume != Volume) {
oldVolume = Volume;
NVSprefs.putUChar(NVS_NAMESPACE_PREAP_VOL, Volume);
#if DEBUGLEVEL > 4 // Serial Debugging
DEBUG_F("New Store in nvs Volume : %d\n", Volume);
#endif //DEBUGLEVEL > 4
}
if (oldBuzzerVolume != BuzzerVolume) {
oldBuzzerVolume != BuzzerVolume;
NVSprefs.putUChar(NVS_NAMESPACE_BUZ_VOL, BuzzerVolume);
#if DEBUGLEVEL > 4 // Serial Debugging
DEBUG_F("New Store in nvs BuzzerVolume : %d\n", BuzzerVolume);
#endif //DEBUGLEVEL > 4
}
if (oldBL_brightness != BL_brightness) {
oldBL_brightness = BL_brightness;
NVSprefs.putUChar(NVS_NAMESPACE_BL_BRIGHT, BL_brightness);
#if DEBUGLEVEL > 4 // Serial Debugging
DEBUG_F("New Store in nvs BuzzerVolume : %d\n", BL_brightness);
#endif //DEBUGLEVEL > 4
}
NVSprefs.end();
} // if (millis() - DelayTimeStoreToNVS > 30000)
}
//=======================================================================
void StartWithDefaulPreampValues() {
Select_in = 1;
Volume = 26;
BuzzerVolume = 100;
BL_brightness = 250;
NVSprefs.begin(NVS_NAMESPACE_PREAMP, READWRITEnvs); //
NVSprefs.putUChar(NVS_NAMESPACE_SEL_IN, Select_in);
NVSprefs.putUChar(NVS_NAMESPACE_PREAP_VOL, Volume);
NVSprefs.putUChar(NVS_NAMESPACE_BUZ_VOL, BuzzerVolume);
NVSprefs.putUChar(NVS_NAMESPACE_BL_BRIGHT, BL_brightness);
NVSprefs.end(); //
#if DEBUGLEVEL > 0 // Serial Debugging
DEBUG_F("------ Start with Default Preamp Values -------------------\n\r");
DEBUG_F("\t[Default] %-15s : %s\n", NVS_NAMESPACE_SEL_IN, inputName[Select_in]);
DEBUG_F("\t[Default] %-15s : %d\n", NVS_NAMESPACE_PREAP_VOL, Volume);
DEBUG_F("\t[Default] %-15s : %d\n", NVS_NAMESPACE_BUZ_VOL, BuzzerVolume);
DEBUG_F("\t[Default] %-15s : %d\n", NVS_NAMESPACE_BL_BRIGHT, BL_brightness);
PRINT_DASHED_LINES;
#endif //DEBUGLEVEL > 0
}
//=======================================================================
void nvsLoadPreampValues() {
NVSprefs.begin(NVS_NAMESPACE_PREAMP, READONLYnvs); //
Select_in = NVSprefs.getUChar(NVS_NAMESPACE_SEL_IN);
Volume = NVSprefs.getUChar(NVS_NAMESPACE_PREAP_VOL);
BuzzerVolume = NVSprefs.getUChar(NVS_NAMESPACE_BUZ_VOL);
BL_brightness = NVSprefs.getUChar(NVS_NAMESPACE_BL_BRIGHT);
oldSelect_in = Select_in;
oldVolume = Volume;
oldBuzzerVolume = BuzzerVolume;
oldBL_brightness = BL_brightness;
NVSprefs.end();
#if DEBUGLEVEL > 2 // Serial Debugging
printf("------ Load Preamplifier Values from NVS --------------------\n");
printf("[info] namespace : %s\n", NVS_NAMESPACE_PREAMP);
DEBUG_F("\t%-13s : %s\n", NVS_NAMESPACE_SEL_IN, inputName[Select_in]);
DEBUG_F("\t%-13s : %d\n", NVS_NAMESPACE_PREAP_VOL, Volume);
DEBUG_F("\t%-13s : %d\n", NVS_NAMESPACE_BUZ_VOL, BuzzerVolume);
DEBUG_F("\t%-13s : %d\n", NVS_NAMESPACE_BL_BRIGHT, BL_brightness);
PRINT_DASHED_LINES;
#endif //DEBUGLEVEL > 2
}
#define RW_MODE false
#define RO_MODE true
// #define READWRITEnvs false // Read Write NVS
// #define READONLYnvs true // Read Only NVS
//=======================================================================
void NVS_init() {
DEBUG_F("\r------ Preferences NVS init -------------------------\n");
NVSprefs.begin(NVS_NAMESPACE_VERSION, READONLYnvs); // true -> Read Only mode
bool tpInit = NVSprefs.isKey("nvsInit"); // Test for the existence
if (tpInit == false) {
DEBUG_F("❌➡ The key [ nvsInit ] does not yet exist\n must be first-time run, or NVS new Version FOUND\n");
NVSprefs.end(); // close the namespace in READ ONLY mode
nvsVersion = NVS_VERSION;
NVSprefs.begin(NVS_NAMESPACE_VERSION, READWRITEnvs); // false -> reopen it in READ WRITE mode.
NVSprefs.putUChar(NVS_NAMESPACE_VERSION, nvsVersion); // Save NVS version
NVSprefs.putBool("nvsInit", true); // Create the key and store a value.
NVSprefs.end(); // Close the namespace in RW mode.
#if DEBUGLEVEL > 0 // Serial Debugging
DEBUG_F("\t[Default] %-15s : %d\n", NVS_NAMESPACE_VERSION, nvsVersion);
#endif //DEBUGLEVEL > 0
StartWithDefaulPreampValues(); // Load Default parameters and Save to NVS
} else {
nvsLoadVersion();
DEBUG_F("[info] Load Settings from nvs flash\n");
nvsLoadPreampValues();
}
DEBUG_F("------ Close Preferences NVS init --------------------\n\r");
} //--NVS_init()--
// - NVS-NVS- NVS-NVS- NVS-NVS- NVS-NVS- NVS-NVS----------------------
// -LED-LED-LED-LED-LED-LED-LED-LED------------------
#define LED_S 1 // S
#define LED_P 2 // P
#define LED_O 3 // O
#define LED_R 4 // R
#define LED_T 5 // T
#define LED_F 7 // F
#define LED_M 8 // M
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
int led = 0;
int count = 1;
// create an array of pins for outputs
const uint8_t LEDS[7] = { LED_S, LED_P, LED_O, LED_R, LED_T, LED_F, LED_M}; // Leds
const size_t NUM_Leds = sizeof(LEDS);
// -LED-LED-LED-LED-LED-LED-LED-LED------------------
//=======================================================================
void setup() {
Serial.begin(115200);
delay(10);
// - NVS-NVS- NVS----------------------
DEBUG_F("\r\n\n ------❗ESP Booting Firmware ➡️ [ %s ] ------\r\n", SKETCH_NAME.c_str());
NVS_init();
// -LED-LED----------------------------
for (uint8_t i = 0; i < NUM_Leds; i++) {
pinMode(LEDS[i], OUTPUT);
digitalWrite (LEDS[i], LOW);
}
StartLeds();
}
//=======================================================================
void loop() {
/* Continue();
delay ( 400 );
FadeAllLeds();
delay ( 400 ); */
RadomFadeLeds();
if (FlagStoreToNVS) {
nvsSaveNewValues();
}
}
void StartLeds() {
static bool State[NUM_Leds];
for (uint8_t i = 0; i < NUM_Leds; i++) {
State[i] = !State[i];
digitalWrite (LEDS[i], State[i]);
delay ( 400 );
}
}
void Continue() {
static bool State[NUM_Leds];
State[led] = !State[led];
digitalWrite (LEDS[led], State[led]);
delay ( 400 );
// led = (led + 1) % NUM_Leds;
led = led + count;
if (led < 0 || led >= NUM_Leds) {
count = -count;
}
}
void FadeAllLeds() {
brightness = brightness + fadeAmount;
if (brightness < 0 || brightness >= 150) {
fadeAmount = -fadeAmount;
}
analogWrite(LEDS[led], brightness);
delay(2);
led = led + count;
if (led < 0 || led >= NUM_Leds) {
count = -count;
}
}
void RadomFadeLeds() {
brightness = random (0, 50);
led = random(0, NUM_Leds);
analogWrite(LEDS[led], brightness);
delay(20);
}