Skip to content

Instantly share code, notes, and snippets.

@lorol
Created March 3, 2019 00:59
Show Gist options
  • Save lorol/d107055c209d9eeae71729667d73981d to your computer and use it in GitHub Desktop.
Save lorol/d107055c209d9eeae71729667d73981d to your computer and use it in GitHub Desktop.
common
//-----Custom floatField----------------
/*
Usage:
altFIELD(decimalslField,ee.herz,"Fine"," Hz",0,999.9,10,0.1,setFreq,enterEvent,noStyle)
OR dFIELD(ee.herz,"Fine"," Hz",0,999.9,10,0.1,setFreq,enterEvent,noStyle)
((decimalslField<typeof(ee.herz)>*)&mainMenu[4])->setDecimals(2); // if needed - change dynamiclaly.
OR setDecimals<float>(mainMenu[4],2);
*/
#define dFIELD(...) altFIELD(decimalslField,__VA_ARGS__)
#define DECIMALSFLIED_DEFAULT 1
template<typename T>
class decimalslField :public menuField<T> { //https://github.com/neu-rah/ArduinoMenu/blob/master/examples/customField/customField/customField.ino
private:
idx_t decimals;
public:
decimalslField(constMEM menuFieldShadow<T>& shadow) :menuField<T>(shadow) { decimals = DECIMALSFLIED_DEFAULT; }
decimalslField(
T &value,
constText* text,
constText*units,
T low,
T high,
T step,
T tune,
action a = doNothing,
eventMask e = noEvent,
styles s = noStyle
) :decimalslField(*new menuFieldShadow<T>(value, text, units, low, high, step, tune, a, e, s)) {}
Used printTo(navRoot &root, bool sel, menuOut& out, idx_t idx, idx_t len, idx_t panelNr = 0) override {// https://github.com/neu-rah/ArduinoMenu/issues/94#issuecomment-290936646
//menuFieldShadow<T>& s=*(menuFieldShadow<T>*)shadow;
menuField<T>::reflex = menuField<T>::target();
idx_t l = prompt::printTo(root, sel, out, idx, len);
bool ed = this == root.navFocus;
//bool sel=nav.sel==i;
if (l < len) {
out.print((root.navFocus == this&&sel) ? (menuField<T>::tunning ? '>' : ':') : ' ');
l++;
if (l < len) {
l += out.print(menuField<T>::reflex, decimals);//NOTE: this can exceed the limits!
if (l < len) {
l += print_P(out, fieldBase::units(), len);
}
}
}
return l;
}
void setDecimals(idx_t d) { decimals = d; }
idx_t getDecimals(void) { return(decimals); }
};
template<typename T>
inline void setDecimals(prompt& o,int n) {
((decimalslField<T>*)&o)->setDecimals(n);
}
//-----Custom floatField----------------END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment