Hi all!
Trying to save a set of integer values to non-volatile memory in the EPS32, using the preferences library.
Here's my code. When I look at the output, I'm not seeing that the preferences entries are getting stored. The output only seems to include the first and last items. There should be 120 of them, right?
Also, I am seeing the strange line in the serial monitor:
tag = Pg = P9_S8_TUNE
I don't know where this 'Pg' can be coming from!
It seems like either a stupid syntax error on my part, or a strange threading problem?
Thanks in advance!
-eric
#include "Preferences.h"
int currentString = 0; // tells us which servo to move
int currentPedal = 0;
int currentTune = 500; // value from 1 to 1000, where 500 is no change for the control.
Preferences copedent;
int pedalCount = 10;
int stringCount = 10;
////////////////////////////////////////// PERSISTENcE ////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
const char* getTuneTag(int ped, int str) {
String tag = "P";
tag = tag + ped + "_S" + str + "_TUNE";
Serial.println("tag = " + tag);
const char* result = tag.c_str();
//if (!copedent.isKey(result)) Serial.println("ERROR: key not found");
return result;
}
const char* getTag(int IDX, char* type) {
String tag = "P";
tag += IDX;
tag += "_";
tag += type;
Serial.println("tag = " + tag);
const char* result = tag.c_str();
return result;
}
// Storing tuning settings
void writeTuning(int ped, int str, int tune) { //0-1000
Serial.print(getTuneTag(ped, str));
Serial.println(tune);
copedent.putInt(getTuneTag(ped, str), tune);
}
const char* readTuning(int ped, int str) {
//Serial.println("reading tuning ");
String val = "";
val = val + copedent.getInt(getTuneTag(ped, str));
Serial.println("val = " + val);
const char* result = val.c_str();
return result;
}
// Storing the effective range of each control for mapping
void writeMax(int ped, int max) {
copedent.putInt(getTag(ped, "MAX"), max);
//Serial.println(getTag(ped, "MAX"));
}
void writeMin(int ped, int min) {
copedent.putInt(getTag(ped, "MIN"), min);
//Serial.println(getTag(ped, "MIN"));
}
int readMax(int ped) {
copedent.getInt(getTag(ped, "MAX"));
}
int readMin(int ped) {
copedent.getInt(getTag(ped, "MIN"));
}
void createDefaultPrefs() {
Serial.println("Creating Prefs");
for (int ped = 0; ped < pedalCount; ped++) {
writeMax(ped, 14400);
writeMin(ped, 2000);
for (int str = 0; str < stringCount; str++) {
writeTuning(ped, str, 500);
}
}
}
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
copedent.begin("tuning", false);
bool notNew = copedent.isKey("P0_S0_TUNE");
if (notNew == false) {
createDefaultPrefs();
}
}
void loop() {
// put your main code here, to run repeatedly:
}