Repository navigation
Expand file tree
/
Copy pathitems.h
More file actions
539 lines (506 loc) · 23 KB
/
Copy pathitems.h
File metadata and controls
539 lines (506 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* -*- C++ -*- */
#ifndef RSITE_ARDUINO_MENU_SYSTEM_ITEMS
#define RSITE_ARDUINO_MENU_SYSTEM_ITEMS
#include "menuBase.h"
#include "shadows.h"
namespace Menu {
#ifdef MENU_FMT_WRAPS
enum classes {noClass=0,promptClass,textFieldClass,fieldClass,toggleClass,selectClass,chooseClass,valueClass,menuClass};
#endif
#ifdef MENU_ASYNC
template<typename T> inline const char* typeStr() {return "o";}
template<> inline const char* typeStr<uint8_t>() {return "u8";}
template<> inline const char* typeStr<uint16_t>() {return "u16";}
template<> inline const char* typeStr<uint32_t>() {return "u32";}
template<> inline const char* typeStr<int8_t>() {return "i8";}
template<> inline const char* typeStr<int16_t>() {return "i16";}
template<> inline const char* typeStr<int32_t>() {return "i32";}
// template<> inline const char* typeStr<long>() {return "long";}
// template<> inline const char* typeStr<unsigned long>() {return "unsigned long";}
// template<> inline const char* typeStr<long long>() {return "long long";}
// template<> inline const char* typeStr<unsigned long long>() {return "unsigned long long";}
template<> inline const char* typeStr<float>() {return "f";}
template<> inline const char* typeStr<double>() {return "d";}
#endif
// Menu objects and data
//////////////////////////////////////////////////////////////////////////
class prompt {
friend class navNode;
friend class menuOut;
protected:
public:
constMEM promptShadow* shadow;//constant read-only data (PROGMEM)
public:
status enabled=enabledStatus;//ignore enter if false
bool dirty=true;//needs to be redrawn
inline uint16_t hash() const {
int tmp=(int)this;
return ((uint16_t*)&tmp)[sizeof(this)/sizeof(uint16_t)-1];
}
inline prompt(constMEM promptShadow& shadow):shadow(&shadow) {}
inline prompt(constText* t,action a=doNothing,eventMask e=noEvent,styles s=noStyle,systemStyles ss=_noStyle)
:shadow(new promptShadow(t,a,e,s,ss)) {}
inline void enable() {enabled=enabledStatus;}
inline void disable() {enabled=disabledStatus;}
inline constText* getText() const {return shadow->getText();}
inline systemStyles sysStyles() const {return shadow->_sysStyles();}
inline styles style() const {return shadow->_style();}
inline eventMask events() const {return shadow->_events();}
inline bool is(systemStyles chk) const {return (sysStyles()&chk)==chk;}
inline bool has(systemStyles chk) const {return sysStyles()&chk;}
inline bool is(styles chk) const {return (style()&chk)==chk;}
inline bool has(styles chk) const {return style()&chk;}
inline bool is(eventMask chk) const {return (events()&chk)==chk;}
inline bool has(eventMask chk) const {return events()&chk;}
inline bool canWrap() const {return style()&wrapStyle;}
inline bool canNav() const {return sysStyles()&_canNav;}//can receive navigation focus and process keys
inline bool isMenu() const {return sysStyles()&_menuData;}//has menu data list and can be a navNode target
inline bool isVariant() const {return sysStyles()&_isVariant;}//a menu as an enumerated field, connected to a variable value
inline bool parentDraw() const {return sysStyles()&_parentDraw;}//a menu as an enumerated field, connected to a variable value
inline bool asPad() const {return sysStyles()&_asPad;}//a menu as an enumerated field, connected to a variable value
inline bool hasTitle(navNode& nav) const;
inline result operator()(FUNC_PARAMS) const {return (*shadow)(FUNC_VALUES);}
idx_t printRaw(menuOut& out,idx_t len) const;
virtual void clearChanged(const navNode &nav,const menuOut& out,bool sub) {dirty=false;}
virtual Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0);//raw print to output device
virtual bool changed(const navNode &nav,const menuOut& out,bool sub=true,bool test=false) {return dirty;}
//this is the system version of enter handler, its used by elements like toggle
virtual result sysHandler(SYS_FUNC_PARAMS) {return proceed;}
virtual result eventHandler(eventMask e,navNode& nav,idx_t i) {
return operator()(e,nav,*this);
}
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return promptClass;}
#endif
#ifdef MENU_ASYNC
// virtual prompt* seek(idx_t* uri,idx_t len) {return len?NULL:this;}
virtual bool async(const char*uri,navRoot& root,idx_t lvl);
//some functions to use on htmlFmt
// for enumerations:
virtual idx_t selected() const {return 0;}
virtual const char* typeName() const {return "prompt";}
#endif
#ifdef MENU_ASYNC
virtual void printValue(menuOut&) const {}
virtual void printHigh(menuOut&) const {}
virtual void printLow(menuOut&) const {}
virtual void printStep(menuOut& o) const {};
virtual void printTune(menuOut& o) const {};
#endif
};
class Exit:public prompt {
public:
Exit(constText* t):prompt(t,(callback)doExit,enterEvent) {}
};
//--------------------------------------------------------------------------
// can receive navigation focus and process keys
// this is the minimal candidate for navRoot::navFocus
class navTarget:public prompt {
public:
navTarget(constMEM promptShadow& shadow):prompt(shadow) {}
navTarget(constText* t,action a=doNothing,eventMask e=noEvent,styles s=noStyle,systemStyles ss=_noStyle)
:prompt(t,a,e,s,ss) {}
virtual void parseInput(navNode& nav,menuIn& in);
virtual void doNav(navNode& nav,navCmd cmd);
#ifdef MENU_ASYNC
const char* typeName() const override {return "navTarget";}
#endif
};
//--------------------------------------------------------------------------
class textField:public navTarget {
public:
// int hash=0;//not implemented yet
bool charEdit=false;
bool edited=false;
idx_t cursor=0;
textField(constMEM textFieldShadow& shadow):navTarget(shadow) {}
textField(
constText*label,
char* b,
idx_t sz,
char* const* v,
action a=doNothing,
eventMask e=noEvent,
styles style=noStyle,
systemStyles ss=(Menu::systemStyles)(_noStyle|_canNav|_parentDraw)
):navTarget(*new textFieldShadow(label,b,sz,v,a,e,style,ss)) {}
inline char* buffer() const {return ((textFieldShadow*)shadow)->_buffer();}
inline idx_t sz() const {return ((textFieldShadow*)shadow)->_sz();}
constText* validator(int i) {return ((textFieldShadow*)shadow)->operator[](i%sz());}
void parseInput(navNode& nav,menuIn& in) override;
void doNav(navNode& nav,navCmd cmd) override;
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0) override;
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return textFieldClass;}
#endif
#ifdef MENU_ASYNC
bool async(const char*uri,navRoot& root,idx_t lvl) override;
const char* typeName() const override {return "textField";}
#endif
};
//--------------------------------------------------------------------------
//using this fieldBase saves 418 flash bytes per field template instantiation
class fieldBase:public navTarget {
public:
bool tunning=false;
fieldBase(constMEM promptShadow& shadow):navTarget(shadow) {}
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return fieldClass;}
#endif
#ifdef MENU_ASYNC
bool async(const char*uri,navRoot& root,idx_t lvl) override;
#endif
inline constText* units() {return ((fieldBaseShadow*)shadow)->_units();}
void doNav(navNode& nav,navCmd cmd) override;
virtual bool canTune()=0;
virtual void constrainField()=0;
virtual void stepit(int increment)=0;
virtual idx_t printReflex(menuOut& o) const =0;
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0) override;
#ifdef MENU_ASYNC
const char* typeName() const override {return "fieldBase";}
#endif
};
//--------------------------------------------------------------------------
template<typename T>
class menuField:public fieldBase {
public:
T reflex;
menuField(constMEM menuFieldShadow<T> & shadow):fieldBase(shadow) {}
menuField(
T &value,
constText* text,
constText*units,
T low,
T high,
T step,
T tune,
action a=doNothing,
eventMask e=noEvent,
styles s=noStyle
):menuField(*new menuFieldShadow<T>(value,text,units,low,high,step,tune,a,e,s)) {}
bool canTune() override {return !!tune();}
void constrainField() override {target() = constrain(target(), low(), high());}
idx_t printReflex(menuOut& o) const override;
void parseInput(navNode& nav,menuIn& in) override;
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0) override;
inline T& target() const {return ((menuFieldShadow<T>*)shadow)->target();}
inline T getTypeValue(constMEM T* from) const {return ((menuFieldShadow<T>*)shadow)->getTypeValue(from);}
inline T low() const {return ((menuFieldShadow<T>*)shadow)->_low();}
inline T high() const {return ((menuFieldShadow<T>*)shadow)->_high();}
inline T step() const {return ((menuFieldShadow<T>*)shadow)->_step();}
inline T tune() const {return ((menuFieldShadow<T>*)shadow)->_tune();}
virtual void clearChanged(const navNode &nav,const menuOut& out,bool sub) {
fieldBase::clearChanged(nav,out,sub);
reflex=target();
}
bool changed(const navNode &nav,const menuOut& out,bool sub=true,bool test=false) override {
trace(if (test&&dirty) Serial<<"field dirty"<<endl);
trace(if (test&&(reflex!=target())) Serial<<"reflex!=target reflex:"<<reflex<<" target:"<<target()<<endl);
return dirty||(reflex!=target());
}
#ifdef MENU_ASYNC
void printValue(menuOut& o) const override;
void printHigh(menuOut& o) const override;
void printLow(menuOut& o) const override;
void printStep(menuOut& o) const override;
void printTune(menuOut& o) const override;
#endif
void stepit(int dir) override {
// dir*=options->invertFieldKeys?-1:1;
T thisstep = tunning?tune():step();
dirty=true;
//by default they are inverted.. now buttons and joystick have to flip them
if (dir > 0) {
if ((high()-target()) < thisstep)
target() = canWrap()?low():high();
else
target() += thisstep;
} else {
if ((target()-low()) < thisstep)
target() = canWrap()?high():low();
else
target() -= thisstep;
}
}
#ifdef MENU_ASYNC
const char* typeName() const override {return typeStr<T>();};
// const char* typeName() const override {return "menuField";}
#endif
};
//--------------------------------------------------------------------------
template<typename T>
class menuValue:public prompt {
public:
menuValue(constMEM menuValueShadow<T>& shadow):prompt(shadow) {}
menuValue(constText* text,T value,action a=doNothing,eventMask e=noEvent)
:menuValue(*new menuValueShadow<T>(text,value,a,e)) {}
// #ifdef MENU_DEBUG
// bool changed(const navNode &nav,const menuOut& out,bool sub=true) override {return false;}
// #endif
//inline T getTypeValue(const T* from) const {return &((menuValueShadow<T>*)shadow)->getTypeValue(from);}
inline T target() const {return ((menuValueShadow<T>*)shadow)->target();}
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return valueClass;}
#endif
#ifdef MENU_ASYNC
bool async(const char*uri,navRoot& root,idx_t lvl=0) override;
const char* typeName() const override {return "menuValue";}
#endif
};
//--------------------------------------------------------------------------
class menuNode:public navTarget {
public:
menuNode(constMEM menuNodeShadow& s):navTarget(s) {}
menuNode(constText* text,idx_t sz,prompt* constMEM data[],action a=noAction,eventMask e=noEvent,styles style=wrapStyle,systemStyles ss=(systemStyles)(_menuData|_canNav))
:navTarget(*new menuNodeShadow(text,sz,data,a,e,style,ss)) {}
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return menuClass;}
#endif
inline prompt& operator[](idx_t i) const {return ((menuNodeShadow*)shadow)->operator[](i);}
bool changed(const navNode &nav,const menuOut& out,bool sub=true,bool test=false) override;
void clearChanged(const navNode &nav,const menuOut& out,bool sub) override;
inline idx_t sz() const {return ((menuNodeShadow*)shadow)->_sz();}
inline prompt* constMEM* data() const {return ((menuNodeShadow*)shadow)->_data();}
// #ifdef MENU_ASYNC
// prompt* seek(idx_t* uri,idx_t len) override;
// #endif
#ifdef MENU_ASYNC
bool async(const char*uri,navRoot& root,idx_t lvl=0) override;
const char* typeName() const override {return "mn";}
//aux function, parse uri node (text to idx_t)
idx_t parseUriNode(const char*&uri);
#endif
};
//--------------------------------------------------------------------------
class menu:public menuNode {
public:
menu(constMEM menuNodeShadow& shadow):menuNode(shadow) {}
// Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0) override {
// }
#ifdef MENU_ASYNC
const char* typeName() const override {return "menu";}
#endif
};
//--------------------------------------------------------------------------
class menuVariantBase:public menuNode {
public:
menuVariantBase(constMEM menuNodeShadow& s):menuNode(s) {}
virtual idx_t sync()=0;
virtual idx_t sync(idx_t i)=0;
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0) override;
idx_t togglePrintTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr);
void doNav(navNode& nav,navCmd cmd) override;
#ifdef MENU_ASYNC
const char* typeName() const override {return "menuVariantBase";}
#endif
};
template<typename T>
class menuVariant:public menuVariantBase {
public:
idx_t reflex;
menuVariant(constMEM menuNodeShadow& s):menuVariantBase(s) {}
menuVariant(constText* text,T &target,idx_t sz,prompt* constMEM* data,action a,eventMask e,styles style)
:menuVariantBase(*new menuVariantShadow<T>(text,target,sz,data,a,e,style)) {}
idx_t sync() override {
for(idx_t i=0;i<sz();i++)
if (((menuValue<T>*)&operator[](i))->target()==target()) return i;
#ifdef MENU_DEBUG
Serial.print(F("value out of range "));
Serial.println(target());Serial.flush();
assert(false);
#endif
return -1;
}
idx_t sync(idx_t i) override {
#ifdef MENU_DEBUG
if (!(i>=0&&i<sz())){
print_P(Serial,getText());
Serial.print(F(" : value out of range "));
Serial.println(i);
}
assert(i>=0&&i<sz());
#endif
if (i!=reflex) dirty=true;
reflex=i;
target()=((menuValue<T>*)&operator[](i))->target();
return i;
}
inline T& target() const {return ((menuVariantShadow<T>*)shadow)->target();}
bool changed(const navNode &nav,const menuOut& out,bool sub=true,bool test=false) override;
#ifdef MENU_ASYNC
virtual idx_t selected() const {return reflex;}
const char* typeName() const override {return "menuVariant";}
#endif
};
template<typename T>//-------------------------------------------
class select:public menuVariant<T> {
public:
select(constMEM menuNodeShadow& s):menuVariant<T>(s) {}
select(
constText* text,
T &target,
idx_t sz,
prompt* constMEM* data,
action a=doNothing,
eventMask e=noEvent,
styles style=noStyle,
systemStyles ss=((systemStyles)(Menu::_menuData|Menu::_canNav|Menu::_isVariant|Menu::_parentDraw))
):menuVariant<T>(*new menuVariantShadow<T>(text,target,sz,data,a,e,style,ss)) {}
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return selectClass;}
#endif
#ifdef MENU_ASYNC
const char* typeName() const override {return "select";}
#endif
};
template<typename T>//-------------------------------------------
class toggle:public menuVariant<T> {
public:
toggle(constMEM menuNodeShadow& s):menuVariant<T>(s) {}
toggle(
constText* text,
T &target,
idx_t sz,
prompt* constMEM* data,
action a=doNothing,
eventMask e=noEvent,
styles style=noStyle,
systemStyles ss=((systemStyles)(Menu::_menuData|Menu::_isVariant))
):menuVariant<T>(*new menuVariantShadow<T>(text,target,sz,data,a,e,style,ss)) {}
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return toggleClass;}
#endif
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0) override;
result sysHandler(SYS_FUNC_PARAMS) override {
switch(event) {
case activateEvent: {
idx_t at=menuVariant<T>::sync();
assert(at!=-1);
at++;
if (at>=menuNode::sz()) at=0;
menuVariant<T>::sync(at);
prompt::dirty=true;
(menuNode::operator[](at))(FUNC_VALUES);
}
default:
return proceed;
}
}
#ifdef MENU_ASYNC
const char* typeName() const override {return "toggle";}
bool async(const char*uri,navRoot& root,idx_t lvl);
#endif
};
template<typename T>//-------------------------------------------
class choose:public menuVariant<T> {
public:
choose(constMEM menuNodeShadow& s):menuVariant<T>(s) {}
choose(
constText* text,
T &target,
idx_t sz,
prompt* constMEM* data,
action a=doNothing,
eventMask e=noEvent,
styles style=noStyle,
systemStyles ss=((systemStyles)(Menu::_menuData|Menu::_canNav|Menu::_isVariant))
):menuVariant<T>(*new menuVariantShadow<T>(text,target,sz,data,a,e,style,ss)) {}
#ifdef MENU_FMT_WRAPS
virtual classes type() const {return chooseClass;}
#endif
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr=0) override;
result sysHandler(SYS_FUNC_PARAMS) override;
bool changed(const navNode &nav,const menuOut& out,bool sub=true,bool test=false) override {
return menuVariant<T>::changed(nav,out)||menuNode::changed(nav,out);
}
#ifdef MENU_ASYNC
const char* typeName() const override {return "choose";}
#endif
};
}
////////////////////////////////////////////////////////////////////////
// template implementation
#include "io.h"
#include "nav.h"
namespace Menu {
#ifdef MENU_ASYNC
template<typename T>
bool menuValue<T>::async(const char*uri,navRoot& root,idx_t lvl) {
trace(Serial<<(*(prompt*)this)<<" menuValue::async! lvl:"<<lvl<<" navRoot.level:"<<root.level<<" navFocus:"<<(*(prompt*)root.navFocus)<<endl);
return prompt::async(uri,root,lvl);
}
#endif
template<typename T>
idx_t menuField<T>::printReflex(menuOut& o) const {return o.print(reflex);}
template<typename T>
Used menuField<T>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) {
trace(print_P(out,getText());Serial<<" menuField<T>::printTo "<<reflex<<endl);
reflex=target();
return fieldBase::printTo(root,sel,out,idx,len,panelNr);
}
template<typename T>
void menuField<T>::parseInput(navNode& nav,menuIn& in) {
navCmd cmd=in.peek();
if (cmd==numValue) {//a numeric value was entered
trace(Serial<<"numValue"<<endl);
// if (in.numValueInput) {
target()=(T)in.parseFloat();//TODO: use template specialization and proper convertion
tunning=true;
doNav(nav,enterCmd);
// } else doNav(nav,idxCmd);
} else doNav(nav,in.getCmd());
}
#ifdef MENU_ASYNC
template<typename T>
void menuField<T>::printValue(menuOut& o) const {o.print(reflex);}
template<typename T>
void menuField<T>::printHigh(menuOut& o) const {o.print(high());}
template<typename T>
void menuField<T>::printLow(menuOut& o) const {o.print(low());}
template<typename T>
void menuField<T>::printStep(menuOut& o) const {o.print(step());}
template<typename T>
void menuField<T>::printTune(menuOut& o) const {o.print(tune());}
#endif
template<typename T>
Used choose<T>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) {
bool ed=this==root.navFocus;
return ed?
prompt::printTo(root,sel,out,idx,len,panelNr)
:menuVariantBase::printTo(root,sel,out,idx,len,panelNr);
}
template<typename T>
Used toggle<T>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) {
return menuVariantBase::togglePrintTo(root,sel,out,idx,len,panelNr);
}
#ifdef MENU_ASYNC
template<typename T>
bool toggle<T>::async(const char*uri,navRoot& root,idx_t lvl) {
trace(Serial<<(*(prompt*)this)<<" toggle::async! uri:"<<uri<<endl);
if(uri[0]) {
trace("selecting value by index!");
idx_t n=menuNode::parseUriNode(uri);
trace(Serial<<"n:"<<n<<" sel:"<<root.path[lvl].sel<<endl);
menuVariant<T>::sync(n);
return true;
}
return prompt::async(uri,root,lvl);
}
#endif
template<typename T>
bool menuVariant<T>::changed(const navNode &nav,const menuOut& out,bool sub,bool test) {
return dirty||((menuValue<T>*)&operator[](reflex))->target()!=target();
}
template<typename T>
result choose<T>::sysHandler(SYS_FUNC_PARAMS) {
switch(event) {
case updateEvent:
case enterEvent:
nav.sel=menuVariant<T>::sync();
default:
return proceed;
}
}
}//namespace Menu
#endif