neu-rah / ArduinoMenu

Arduino generic menu/interactivity system
GNU Lesser General Public License v2.1
929 stars 189 forks source link

using altFIELD to save updated values #395

Open crivaronicolini opened 1 year ago

crivaronicolini commented 1 year ago

Hi! I'm trying to write an altFIELD that takes a string as a parameter and uses the updated value of the variable, to save it using the Preferences library with an exitEvent. I'd like to write something like:

#include <Preferences.h>
Preferences prefs;
prefs.begin("test");

int timeOn=10;
int timeOff=90;

MENU(mainMenu, "Blink menu", doNothing,noEvent, wrapStyle
  ,altFIELD(saveField, "timeOn", timeOn,"On","ms",0,100,10,1, doNothing, exitEvent, noStyle)
  ,altFIELD(saveField, "timeOff". timeOff,"Off","ms",0,100,10,1,doNothing, exitEvent, noStyle)
  ,EXIT("<Back")
);

with an altFIELD that executes

template<typename T>
class saveField:public menuField<T> {
public:
  using menuField<T>::menuField;
  // code
  prefs.putInt(key, value); //would be prefs.putInt("timeOn", timeOn);  
};

when the exitEvent is called.

Thanks!