|
| 1 | +/* -*- C++ -*- */ |
| 2 | +/******************** |
| 3 | +May 2017 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com |
| 4 | +www.r-site.net |
| 5 | +unlike native objects, pluggins can be device specific, so some pluggins might |
| 6 | +not work on all devices. |
| 7 | +all sorts of plugins may be available in the future and if you customize |
| 8 | +a component and think it of interest of others please do pull request. |
| 9 | +contrubutions and contrubutors are welcome. |
| 10 | +
|
| 11 | +decimalslField - To select the number of decimal places that are printed in float and double variables types. Feb 2019 ferchinas |
| 12 | + Tested on ESP32, Oled i2c display, u8g2 driver. It could work on other devices. |
| 13 | +***/ |
| 14 | + |
| 15 | +#include <menu.h> |
| 16 | +#include <menuIO/serialOut.h> |
| 17 | +#include <menuIO/chainStream.h> |
| 18 | +#include <menuIO/serialIn.h> |
| 19 | + |
| 20 | +using namespace Menu; |
| 21 | + |
| 22 | +#define MAX_DEPTH 1 |
| 23 | + |
| 24 | +unsigned char cantDecimals = 1; |
| 25 | +float floatVar = 0.123456789123456789; |
| 26 | +double doubletVar = 0.123456789123456789; |
| 27 | + |
| 28 | +void updateEvent_cb(eventMask e); |
| 29 | + |
| 30 | +//-----Custom floatField---------------- |
| 31 | +#define DECIMALSFLIED_DEFAULT 1 |
| 32 | + |
| 33 | +template<typename T> |
| 34 | +class decimalslField :public menuField<T> { //https://github.com/neu-rah/ArduinoMenu/blob/master/examples/customField/customField/customField.ino |
| 35 | +private: |
| 36 | + idx_t decimals; |
| 37 | +public: |
| 38 | + decimalslField(constMEM menuFieldShadow<T>& shadow) :menuField<T>(shadow) { decimals = DECIMALSFLIED_DEFAULT; } |
| 39 | + decimalslField( |
| 40 | + T &value, |
| 41 | + constText* text, |
| 42 | + constText*units, |
| 43 | + T low, |
| 44 | + T high, |
| 45 | + T step, |
| 46 | + T tune, |
| 47 | + action a = doNothing, |
| 48 | + eventMask e = noEvent, |
| 49 | + styles s = noStyle |
| 50 | + ) :decimalslField(*new menuFieldShadow<T>(value, text, units, low, high, step, tune, a, e, s)) {} |
| 51 | + |
| 52 | + 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 |
| 53 | + //menuFieldShadow<T>& s=*(menuFieldShadow<T>*)shadow; |
| 54 | + menuField<T>::reflex = menuField<T>::target(); |
| 55 | + idx_t l = prompt::printTo(root, sel, out, idx, len); |
| 56 | + bool ed = this == root.navFocus; |
| 57 | + //bool sel=nav.sel==i; |
| 58 | + if (l < len) { |
| 59 | + out.print((root.navFocus == this&&sel) ? (menuField<T>::tunning ? '>' : ':') : ' '); |
| 60 | + l++; |
| 61 | + if (l < len) { |
| 62 | + l += out.print(menuField<T>::reflex, decimals);//NOTE: this can exceed the limits! |
| 63 | + if (l < len) { |
| 64 | + l += print_P(out, fieldBase::units(), len); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return l; |
| 69 | + } |
| 70 | + |
| 71 | + void setDecimals(idx_t d) { decimals = d; } |
| 72 | + idx_t getDecimals(void) { return(decimals); } |
| 73 | +}; |
| 74 | +//-----Custom floatField----------------END |
| 75 | + |
| 76 | + |
| 77 | +decimalslField <typeof(floatVar)>& floatFlied_Object = *new decimalslField<typeof(floatVar)>(floatVar, "f", "", -100.0, 100.0, 0.1, 1); |
| 78 | +decimalslField <typeof(doubletVar)>& doubleFlied_Object = *new decimalslField<typeof(doubletVar)>(doubletVar, "d", "", -100.0, 100.0, 0.1, 1); |
| 79 | +decimalslField <typeof(cantDecimals)>& DecimalsFlied_Object = *new decimalslField<typeof(cantDecimals)>(cantDecimals, "Dec", "", 0, 18, 1, 1, (Menu::callback)updateEvent_cb, updateEvent); |
| 80 | + |
| 81 | +void updateEvent_cb(eventMask e){ |
| 82 | + floatFlied_Object.setDecimals(cantDecimals); |
| 83 | + doubleFlied_Object.setDecimals(cantDecimals); |
| 84 | + Serial.println("");Serial.print("now using '");Serial.print(cantDecimals);Serial.println("' decimals."); |
| 85 | + } |
| 86 | + |
| 87 | +//---Main menu --------------------------------- |
| 88 | +prompt* mainData[] = { |
| 89 | + &floatFlied_Object, |
| 90 | + &doubleFlied_Object, |
| 91 | + &DecimalsFlied_Object, |
| 92 | + new Exit("<Back") |
| 93 | +}; |
| 94 | + |
| 95 | +menuNode& mainMenu = *new menuNode("Main menu", sizeof(mainData) / sizeof(prompt*), mainData); |
| 96 | + |
| 97 | +serialIn serial(Serial); |
| 98 | +MENU_INPUTS(in,&serial); |
| 99 | + |
| 100 | +MENU_OUTPUTS(out,MAX_DEPTH |
| 101 | + ,SERIAL_OUT(Serial) |
| 102 | + ,NONE//must have 2 items at least |
| 103 | +); |
| 104 | + |
| 105 | +NAVROOT(nav,mainMenu,MAX_DEPTH,in,out); |
| 106 | + |
| 107 | +void setup() { |
| 108 | + Serial.begin(115200); |
| 109 | + while(!Serial); |
| 110 | + Serial.println("Menu 4.x"); |
| 111 | + Serial.println("Use keys + - * /"); |
| 112 | + Serial.println("to control the menu navigation"); |
| 113 | + nav.useUpdateEvent = true; |
| 114 | +} |
| 115 | + |
| 116 | +void loop() { |
| 117 | + nav.poll(); |
| 118 | +} |
0 commit comments