forked from brunialti/MENWIZ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MENWIZ.cpp
1195 lines (1083 loc) · 37 KB
/
MENWIZ.cpp
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------------
// Created by Roberto Brunialti on 20/04/12.
// Copyright 2012 - Under creative commons license 3.0:
// Attribution-ShareAlike CC BY-SA
//
// This software is furnished "as is", without technical support, and with no
// warranty, express or implied, as to its usefulness for any purpose.
//
// Thread Safe: No
// Extendable: Yes
//
// @file MENWIZ.cpp
// This file implements a basic menu management library in the Arduino SDK
//
// @brief
// This is a menu management library. The library allows user to create
// an intire menu tree with relatively few lines of code.
// The library allows the users to define callbacks able to overload internal
// functions (i.e. navigation device management) or to be executed inside a menu.
// It is possible to define also splash screens and a default user screen to be
// activated after a time interval since the last interaction
//
// @author R. Brunialti - roberto_brunialti@tiscali.it
// ---------------------------------------------------------------------------
#include "MENWIZ.h"
#define SCREATE(p,s) p=(char *)malloc(strlen((char *)s)+1); strcpy((char *)p,(char *)s)
#define SFORM(b,s,l) memset(b,32,l); memcpy(b,s,strlen(s)); b[l]=0; lcd->print(b)
#define TSFORM(b,s,l,c) memset(b,32,l);strncpy_P(b,(const char PROGMEM*)s, min(col,strlen_P((char PROGMEM*) s))); b[strlen(b)]=' ';itoa(cur_menu->cur_item+1,tmp,10);strcat(tmp,"/");itoa(cur_menu->idx_o,tmp+strlen(tmp),10);b[col-strlen(tmp)-1]=c;memcpy(b+(col-strlen(tmp)),tmp,strlen(tmp));b[l]=0;lcd->print(b)
#define FSFORM(b,s,l) memset(b,32,l);memcpy_P(b,(const char PROGMEM*)s,min(l,strlen_P((const char PROGMEM*)s)));buf[l]=0;lcd->print(b);
#define BLANKLINE(b,r,c) memset(b,32,c);b[c]=0; lcd->setCursor(0,r);lcd->print(b)
// GLOBAL VARIABLES
// ---------------------------------------------------------------------------
const char MW_ver[]={"1.3.2 beta"};
byte MW_navbtn=0;
bool MW_invar=false;
static char *buf;
char tmp[6];
const uint8_t c0[8]={B00000, B00000, B00001, B00010, B10100, B01000, B00000, B00000};
const uint8_t c1[8]={B00000, B11100, B00100, B00100, B10101, B01110, B00100, B00000};
const uint8_t c2[8]={B00000, B01010, B11111, B01110, B11111, B01010, B00000, B00000};
//const uint8_t c3[8]={B00100, B01110, B11111, B00000, B00000, B00000, B11111, B00000};
menwiz::menwiz(){
setError(0);
bitWrite(flags,FL_SPLASH,false);
bitWrite(flags,FL_SPLASH_DRAW,false);
bitWrite(flags,FL_USRSCREEN_DRAW,false);
bitWrite(flags,MW_MENU_INDEX,true);
usrConfirmMessage = F("[Confirm] to run.");
cur_user=MW_GRANT_USER1;
usrScreen.fl=false;
usrNav.fl=false;
last_button=MW_BTU;
idx_m=0;
row=0;
col=0;
tm_start=0;
root=NULL;
#ifdef EEPROM_SUPPORT
eeprom_offset=0;
eeprom_version=1;
eeprom_write_on_confirm=false;
#endif
}
_menu::_menu(){
menwiz::setError(0);
/*
initialized in addMenu
*/
}
_option::_option(){
menwiz::setError(0);
label=NULL;
}
_menu * menwiz::addMenu(MW_TYPE t,_menu * p, MW_LABEL lab){
static _option *op;
menwiz::setError(0);
if ((idx_m==0)&&(t!=MW_ROOT)){
menwiz::setError(200);
return NULL;
}
//INITIALIZE NEW MENU VARIABLES
if (idx_m<MAX_MENU){
m[idx_m].type=(MW_TYPE)t; // ROOT| SUBMENU| VAR
m[idx_m].label=lab;
m[idx_m].cod=idx_m; // unique menu id
m[idx_m].idx_o=0; // reset option index
m[idx_m].cur_item=0; // reset cur item
bitWrite(m[idx_m].flags,MW_GRANT_USER1,true);
bitWrite(m[idx_m].flags,MW_GRANT_USER2,true);
bitWrite(m[idx_m].flags,MW_GRANT_USER3,true);
if (t==MW_ROOT){
//IF ROOT, PARENT=ITSELF, SET ROOT POINTER, SET ROOT AS START MENU
m[idx_m].parent=idx_m;
root=&m[idx_m];
cur_menu=&m[idx_m];
}
else{
//IF NOT ROOT, ADD MENU TO THE PARENTS OPLIST
m[idx_m].parent=p->cod;
if (m[p->cod].idx_o<MAX_OPTXMENU){
op=(_option *) malloc(sizeof(_option));
if(op==NULL){
menwiz::setError(900);
}
else{
op->sbm=idx_m;
op->type=(MW_TYPE)t;
m[p->cod].o[m[p->cod].idx_o]=(_option*)op;
m[p->cod].idx_o++;
}
}
else{menwiz::setError(105);}
}
idx_m++;
return &m[idx_m-1];
}
else{
// ERROR
menwiz::setError(100);
return NULL;}
}
_option *_menu::addItem(int t,MW_LABEL lab){
static _option *op=NULL;
menwiz::setError(0);
if (idx_o<MAX_OPTXMENU){
op=(_option *) malloc(sizeof(_option));
if(op==NULL){
menwiz::setError(900);
}
else {
op->type=(MW_TYPE)t;
op->label=lab;
o[idx_o]=(_option*)op;
idx_o++;
}
}
else{
// ERROR
menwiz::setError(105);
return NULL;
}
return op;
}
void _menu::addVar(MW_TYPE t, int* v){
menwiz::setError(0);
if(type==MW_ROOT){ //patch to be verified
type=(MW_TYPE)MW_VAR; //patch to be verified
}
if (t!=MW_LIST) {menwiz::setError(120);}
else if(type==MW_VAR){
var=(_var*)malloc(sizeof(_var));
if(var==NULL){menwiz::setError(900); return;}
bitWrite(flags,MW_SCROLL_HORIZONTAL,false);
bitWrite(flags,MW_LIST_3COLUMNS,false);
bitWrite(flags,MW_LIST_2COLUMNS,false);
((_var*)var)->type=MW_LIST;
((_var*)var)->val=v;
((_var*)var)->old=v;
cur_item=VBYTE(v);
}
// ERROR
else{menwiz::setError(110);}
}
void _menu::addVar(MW_TYPE t, int* v, int low, int up, int incr){
menwiz::setError(0);
if (t!=MW_AUTO_INT)
menwiz::setError(120);
else if(type==MW_VAR){
var=(_var*)malloc(sizeof(_var));if(var==NULL){menwiz::setError(900); return;}
((_var*)var)->type=MW_AUTO_INT;
((_var*)var)->val=v;
((_var*)var)->lower=malloc(sizeof(int)); if(((_var*)var)->lower!=NULL) VINT(((_var*)var)->lower)=low; else {menwiz::setError(900); return;}
((_var*)var)->upper=malloc(sizeof(int)); if(((_var*)var)->upper!=NULL) VINT(((_var*)var)->upper)=up; else {menwiz::setError(900); return;}
((_var*)var)->incr=malloc(sizeof(int)); if(((_var*)var)->incr!=NULL) VINT(((_var*)var)->incr)=incr; else {menwiz::setError(900); return;}
((_var*)var)->old=malloc(sizeof(int)); if(((_var*)var)->old!=NULL) VINT(((_var*)var)->old)=VINT(((_var*)var)->val); else {menwiz::setError(900); return;}
}
// ERROR
else{menwiz::setError(110);}
}
void _menu::addVar(MW_TYPE t, float* v, float low, float up, float incr){
menwiz::setError(0);
if (t!=MW_AUTO_FLOAT)
menwiz::setError(120);
else if(type==MW_VAR){
var=(_var*)malloc(sizeof(_var));if(var==NULL){menwiz::setError(900); return;}
((_var*)var)->type=MW_AUTO_FLOAT;
((_var*)var)->val=v;
((_var*)var)->lower=malloc(sizeof(float)); if(((_var*)var)->lower!=NULL) VFLOAT(((_var*)var)->lower)=low; else {menwiz::setError(900); return;}
((_var*)var)->upper=malloc(sizeof(float)); if(((_var*)var)->upper!=NULL) VFLOAT(((_var*)var)->upper)=up; else {menwiz::setError(900); return;}
((_var*)var)->incr=malloc(sizeof(float)); if(((_var*)var)->incr!=NULL) VFLOAT(((_var*)var)->incr)=incr; else {menwiz::setError(900); return;}
((_var*)var)->old=malloc(sizeof(float)); if(((_var*)var)->old!=NULL) VFLOAT(((_var*)var)->old)=VFLOAT(((_var*)var)->val); else {menwiz::setError(900); return;}
}
// ERROR
else{menwiz::setError(110);}
}
void _menu::addVar(MW_TYPE t, byte* v, byte low, byte up, byte incr){
menwiz::setError(0);
if (t!=MW_AUTO_BYTE)
menwiz::setError(120);
else if(type==MW_VAR){
var=(_var*)malloc(sizeof(_var));if(var==NULL){menwiz::setError(900); return;}
((_var*)var)->type=MW_AUTO_BYTE;
((_var*)var)->val=v;
((_var*)var)->lower=malloc(sizeof(byte)); if(((_var*)var)->lower!=NULL) VBYTE(((_var*)var)->lower)=low; else {menwiz::setError(900); return;}
((_var*)var)->upper=malloc(sizeof(byte)); if(((_var*)var)->upper!=NULL) VBYTE(((_var*)var)->upper)=up; else {menwiz::setError(900); return;}
((_var*)var)->incr=malloc(sizeof(byte)); if(((_var*)var)->incr!=NULL) VBYTE(((_var*)var)->incr)=incr; else {menwiz::setError(900); return;}
((_var*)var)->old=malloc(sizeof(byte)); if(((_var*)var)->old!=NULL) VBYTE(((_var*)var)->old)=VBYTE(((_var*)var)->val); else {menwiz::setError(900); return;}
}
// ERROR
else{menwiz::setError(110);}
}
void _menu::addVar(MW_TYPE t, bool* v){
menwiz::setError(0);
if (t!=MW_BOOL)
menwiz::setError(120);
else if(type==MW_VAR){
var=(_var*)malloc(sizeof(_var));if(var==NULL){menwiz::setError(900); return;}
((_var*)var)->type=MW_BOOL;
((_var*)var)->val=v;
((_var*)var)->old=malloc(sizeof(bool)); if(((_var*)var)->old!=NULL) VBOOL(((_var*)var)->old)=VBOOL(((_var*)var)->val); else {menwiz::setError(900); return;}
}
// menwiz::setError
else{menwiz::setError(110);}
}
void _menu::addVar(MW_TYPE t,char *s){
menwiz::setError(0);
if (t!=MW_EDIT_TEXT)
menwiz::setError(120);
else if(type==MW_VAR){
var=(_var*)malloc(sizeof(_var)); if(var==NULL){menwiz::setError(320); return;}
((_var*)var)->type=MW_EDIT_TEXT;
((_var*)var)->val=s;
((_var*)var)->old=s;
}
// menwiz::setError
else{menwiz::setError(110);}
}
void _menu::addVar(MW_TYPE t,void (*f)()){
menwiz::setError(0);
if (t!=MW_ACTION)
menwiz::setError(120);
else if(type==MW_VAR){
bitWrite(flags,MW_ACTION_CONFIRM,true);
var=(_act*)malloc(sizeof(_act));if(var==NULL){menwiz::setError(900); return;}
((_act*)var)->type=MW_ACTION;
((_act*)var)->action=f;
}
// ERROR
else{menwiz::setError(110);}
}
void menwiz::setCurrentUser(int u){
setError(0);
if((u>=MW_GRANT_USER1)&&(u<=MW_GRANT_USER3)) cur_user=u;
else setError(500);
}
void menwiz::addUsrNav(int (*f)(), int nb){
setError(0);
usrNav.fl=true;
usrNav.fi=f;
if ((nb==4) or (nb==6)) MW_navbtn=nb;
else setError(130);
}
void menwiz::setUsrConfirmMessage(MW_LABEL msg){
usrConfirmMessage = msg;
}
void menwiz::addUsrConfirmAction(void (*f)()){
setError(0);
usrConfirmAction.fl=true;
usrConfirmAction.fv=f;
}
void menwiz::begin(void *l,int c, int r){
setError(0);
tm_start=millis();
row=r;
col=c;
lcd=(MW_LCD*)l;
lcd->begin(c,r); // LCD size
// lcd->setBacklight(HIGH);
// lcd->noCursor();
lcd->noBlink();
lcd->createChar(0,(uint8_t*)c0);
lcd->createChar(1,(uint8_t*)c1);
lcd->createChar(2,(uint8_t*)c2);
sbuf=(char*)malloc(r*c+r+1); if(sbuf==NULL) setError(900);
memset(sbuf,' ',r*c+r);sbuf[r*c+r]=0;
buf =(char*)malloc(c+1); if(buf==NULL) setError(900);
memset(buf,' ',c);buf[c]=0;
}
void menwiz::drawUsrScreen(char *scr){
setError(0);
int start=0, cur=0;
lcd->noBlink();
for(int i=0;i<row;i++){
while((scr[cur]!=MW_EOL_CHAR)&&(scr[cur]!=0)){
cur++;
}
memset(buf,' ',col); //clear line buffer
memcpy(buf,&scr[start],cur-start);
buf[col]=0;
lcd->setCursor(0,i);
lcd->print(buf);
if (scr[cur]==0){
return;
}
cur++;
start=cur;
}
}
void menwiz::showUsrScreen(){
tm_push=millis()-tm_usrScreen-1; //we just set tm_push, so that the userscreen comes up
}
void menwiz::showMenuScreen(){
tm_push=millis(); //we just set tm_push, so that the userscreen comes up
}
void menwiz::draw(){
int ret;
unsigned int long lap1,lap2;
// setError(0);
// get nav choice
#ifdef BUTTON_SUPPORT
ret=usrNav.fl?usrNav.fi():scanNavButtons(); //internal method or user defined callback?
#else
ret=usrNav.fi(); //user defined callback
#endif
// ------ SPECIAL CASES START --------
//ENTER MENU SCREEN IF ANY BUTTON IS PUSHED
// if a button was pushed while running usrscreen or splash screen , skip last button, switch to MENU mode, draw menu and exit
if( (ret!=MW_BTNULL) && ((cur_mode==MW_MODE_USRSCREEN)||(cur_mode==MW_MODE_SPLASH))){
bitWrite(flags,FL_SPLASH_DRAW,1); //mark as drawn once
bitWrite(flags,FL_USRSCREEN_DRAW,1); //disable userscreen draw
cur_mode=MW_MODE_MENU;
tm_push=millis();
drawMenu(cur_menu);
return;
}
//EXIT MENU SCREEN IF CANCEL BUTTON IS PUSHED
// if the cancel button is pushed while in menu root, let the userscreen come up
if ( (ret==MW_BTE) && ((cur_mode==MW_MODE_MENU)) && (cur_menu->type==MW_ROOT)){
showUsrScreen();
ret = MW_BTNULL; //skip cancel button
}
// ------ SPECIAL CASES END --------
actNavButtons(ret);
lap1=(millis()-tm_start);
lap2=(millis()-tm_push);
//SPLASHSCREEN
// if defined splashscreen & not yet drawn & time window is ok, draw it
if(bitRead(flags,FL_SPLASH)&& (!bitRead(flags,FL_SPLASH_DRAW)) && (lap1<tm_splash)){
cur_mode=MW_MODE_SPLASH;
drawUsrScreen(sbuf);
if(lap1>=tm_splash) bitWrite(flags,FL_SPLASH_DRAW,1);//mark as drawn once }
}
//USERSCREEN
// if defined usrscreen & time since last button push > user defined time, draw it
else if((usrScreen.fl) && (lap2>tm_usrScreen)){
cur_mode=MW_MODE_USRSCREEN;
bitWrite(flags,FL_USRSCREEN_DRAW,0);
usrScreen.fv();
}
//MENU
else{
// if a button was pushed since last call, draw menu
cur_mode=MW_MODE_MENU;
if((last_button!=MW_BTNULL) || (!bitRead(flags,FL_USRSCREEN_DRAW)) ){
drawMenu(cur_menu);
}
}
}
void menwiz::drawMenu(_menu *mc){
int rstart,rstop,i,j;
_option *op;
_menu *mn;
byte top,fl=0;
// setError(0);
lcd->setCursor(0,0);
// DRAW THE FIRST LINE
if((mc->type==MW_ROOT)||(mc->type==MW_SUBMENU)||(((_act*)mc->var)->type==MW_LIST)){
if(bitRead(flags,MW_MENU_INDEX)){
if(((_act*)mc->var)->type==MW_LIST){
top=126;}
else if(bitRead( m[((_option*)mc->o[mc->cur_item])->sbm].flags,cur_user)==false){
top=2;}
else
top=126;
TSFORM(buf,mc->label,(int) col,top);
}
else{
FSFORM(buf,mc->label,(int) col);
}
}
else{
FSFORM(buf,mc->label,(int) col);
}
if (mc->type==MW_VAR){
drawVar(mc);
}
else{
rstart=max(0,mc->cur_item-(row-2));
rstop=min((mc->idx_o),(rstart+row));
//DRAW ALL REMAINNING LINES
for (i=1,j=rstart;i<row;i++,j++){
if(j<rstop){
lcd->setCursor(0,i);
if(bitRead(mc->flags,MW_MENU_COLLAPSED)){
op=(_option*)mc->o[j];
mn=&m[op->sbm];
if(j==mc->cur_item){
if(bitRead( m[((_option*)mc->o[mc->cur_item])->sbm].flags,cur_user)==false)
buf[0]=2;
else
buf[0]=126;
fl=true;
}
else{
if(bitRead( m[((_option*)mc->o[mc->cur_item])->sbm].flags,cur_user)==false)
buf[0]=2;
else
buf[0]=165;
fl=false;
}
strncpy_P(&buf[1],(const char PROGMEM*)mn->label,min(col,strlen_P((const char PROGMEM*)mn->label)));
strcpy(sbuf,buf);
if(mn->type==MW_VAR){
switch( ((_act*)mn->var)->type ){
case MW_AUTO_INT:
if (fl){strcat(sbuf,":[");strcat(sbuf,itoa( VINT(((_var*)mn->var)->val),buf,10));strcat(sbuf,"]");}
else {strcat(sbuf,":");strcat(sbuf,itoa(VINT(((_var*)mn->var)->val),buf,10));}
break;
case MW_LIST:
strcat(sbuf,": ");sbuf[strlen(sbuf)]=1;sbuf[strlen(sbuf)+1]=0;
break;
case MW_BOOL:
if (fl) {strcat(sbuf,":[");strcat(sbuf,itoa(VBOOL(((_var*)mn->var)->val),buf,10));strcat(sbuf,"]");}
else {strcat(sbuf,":");strcat(sbuf,itoa(VBOOL(((_var*)mn->var)->val),buf,10));}
break;
case MW_AUTO_FLOAT:
if (fl) {strcat(sbuf,":[");dtostrf(VFLOAT( ((_var*)mn->var)->val),0,MW_FLOAT_DEC,buf);strcat(sbuf,buf);strcat(sbuf,"]");}
else {strcat(sbuf,":");dtostrf(VFLOAT( ((_var*)mn->var)->val),0,MW_FLOAT_DEC,buf);strcat(sbuf,buf);}
break;
case MW_AUTO_BYTE:
if (fl) {strcat(sbuf,":[");strcat(sbuf,itoa(VBYTE( ((_var*)mn->var)->val),buf,10));strcat(sbuf,"]");}
else {strcat(sbuf,":");strcat(sbuf,itoa(VBYTE( ((_var*)mn->var)->val),buf,10));}
break;
case MW_ACTION:
strcat(sbuf,": ");sbuf[strlen(sbuf)]=1;sbuf[strlen(sbuf)+1]=0;
break;
}
memset(&sbuf[strlen(sbuf)],32,col-strlen(sbuf));
sbuf[col]=0;
// lcd->setCursor(0,i);
lcd->print(sbuf);
}
else{ // NOT VARS (ROOT OR SUBMENU)
op=(_option*)mc->o[j];
// lcd->setCursor(0,i);
lcd->write((j==mc->cur_item)?126:(bitRead(m[op->sbm].flags,cur_user)?165:2));
FSFORM(buf,m[op->sbm].label,(unsigned int) col-1);
}
}
else{// NOT MENU COLLAPSED
op=(_option*)mc->o[j];
// lcd->setCursor(0,i);
if(bitRead( m[((_option*)mc->o[mc->cur_item])->sbm].flags,cur_user)==false)
lcd->write((j==mc->cur_item)?2:165);
else
lcd->write((j==mc->cur_item)?126:165);
FSFORM(buf,m[op->sbm].label,(unsigned int) col-1);
}
}
else{// EMPTY LINE
BLANKLINE(buf,i,col);
}
}// NEXT
}
}
void menwiz::drawVar(_menu *mc){
int rstart,rstop,i,j;
_option *op;
// setError(0);
switch (((_var*)mc->var)->type){
case MW_LIST:
if(bitRead(mc->flags,MW_SCROLL_HORIZONTAL)){
lcd->setCursor(0,1);
op=(_option*)mc->o[mc->cur_item];
strncpy_P(buf,(const char PROGMEM*)pgm_read_word(op->label),min(col,strlen_P((const char PROGMEM*)op->label)));
strcpy(sbuf,"[");strcat(sbuf,buf);strcat(sbuf,"]");//@
SFORM(buf,sbuf,col);
for(i=2;i<row;i++){
BLANKLINE(buf,i,col);
}
}
else{
if(bitRead(mc->flags,MW_LIST_2COLUMNS)){
drawList(mc,2);
}
else if(bitRead(mc->flags,MW_LIST_3COLUMNS)){
drawList(mc,3);
}
else{
rstart=max(0,mc->cur_item-(row-2));
rstop=min((mc->idx_o),rstart+(row-1));
for (i=1,j=rstart;i<row;i++,j++){
if(j<rstop){
op=(_option*)mc->o[j];
lcd->setCursor(0,i);
lcd->write((j==mc->cur_item)?0:165);
FSFORM(buf,op->label,(unsigned int)col-1);
}
else{
BLANKLINE(buf,i,col);
}
}
}
}
break;
case MW_AUTO_INT:
case MW_AUTO_BYTE:
for(i=2;i<row;i++){
BLANKLINE(buf,i,col);
}
lcd->setCursor(0,1);
if(((_var*)mc->var)->type==MW_AUTO_INT){
strcpy(sbuf,itoa(VINT(((_var*)mc->var)->lower),buf,10));
strcat(sbuf," [");
strcat(sbuf,itoa(VINT(((_var*)mc->var)->val),buf,10));
strcat(sbuf,"] ");
strcat(sbuf,itoa(VINT(((_var*)mc->var)->upper),buf,10));
}
else{
strcpy(sbuf,itoa(VBYTE(((_var*)mc->var)->lower),buf,10));
strcat(sbuf," [");
strcat(sbuf,itoa(VBYTE(((_var*)mc->var)->val),buf,10));
strcat(sbuf,"] ");
strcat(sbuf,itoa(VBYTE(((_var*)mc->var)->upper),buf,10));
}
SFORM(buf,sbuf,col);
break;
case MW_AUTO_FLOAT:
for(i=2;i<row;i++){
BLANKLINE(buf,i,col);
}
lcd->setCursor(0,1);
strcpy(sbuf,dtostrf(VFLOAT(((_var*)mc->var)->lower),0,MW_FLOAT_DEC,buf));
strcat(sbuf," [");
strcat(sbuf,dtostrf(VFLOAT(((_var*)mc->var)->val),0,MW_FLOAT_DEC,buf));
strcat(sbuf,"] ");
strcat(sbuf,dtostrf(VFLOAT(((_var*)mc->var)->upper),0,MW_FLOAT_DEC,buf));
/*
lcd->setCursor(0,1);
lcd->print(dtostrf(VFLOAT(((_var*)mc->var)->lower),0,MW_FLOAT_DEC,buf));
lcd->print(F(" ["));
lcd->print(dtostrf(VFLOAT(((_var*)mc->var)->val),0,MW_FLOAT_DEC,buf));
lcd->print(F("] "));
lcd->print(dtostrf(VFLOAT(((_var*)mc->var)->upper),0,MW_FLOAT_DEC,buf));
*/
SFORM(buf,sbuf,col);
break;
case MW_BOOL:
for(i=2;i<row;i++){
BLANKLINE(buf,i,col);
}
lcd->setCursor(0,1);
SFORM(buf,VBOOL(((_var*)mc->var)->val)?MW_BOOL_ON:MW_BOOL_OFF,(int) col);
break;
case MW_ACTION:
for(i=2;i<row;i++){
BLANKLINE(buf,i,col);
}
lcd->setCursor(0,1);
FSFORM(buf,usrConfirmMessage,(int) col);
break;
case MW_EDIT_TEXT:
for(i=2;i<row;i++){
BLANKLINE(buf,i,col);
}
lcd->setCursor(0,1);
sbuf[0]='[';
strcpy(sbuf+1,(char*)((_var*)mc->var)->val);
strcat(sbuf,"]");
SFORM(buf,sbuf,col);
lcd->blink();
lcd->setCursor(mc->cur_item+1,1);
break;
default:
setError(300);
}
}
void menwiz::drawList(_menu *mc, int nc){
int nr=row-1; int cw=col/nc;
// setError(0);
for(int i=0;i<(row*nc-nc);i++){
int pc=i/nr; int pr=i%nr+1;
lcd->setCursor(pc*cw,pr);
if(i<mc->idx_o){
lcd->write((i==mc->cur_item)?0:165);
FSFORM(buf,((_option*)mc->o[i])->label,(unsigned int)cw-1);
}
else{
memset(buf,32,cw);
buf[cw]=0;
lcd->print(buf);
}
}
}
void menwiz::addSplash(char *s, int lap){
int l;
setError(0);
l=min((unsigned)row*col+row,strlen(s));
strncpy(sbuf,s,l);
sbuf[l]=0;
tm_splash=lap;
bitWrite(flags,FL_SPLASH,1);
bitWrite(flags,FL_SPLASH_DRAW,0);
}
void menwiz::addUsrScreen(void f(), unsigned long t){
setError(0);
usrScreen.fv=f;
usrScreen.fl=true;
tm_usrScreen=t;
}
#ifdef BUTTON_SUPPORT
void menwiz::navButtons(int btu,int btd,int bte,int btc){
setError(0);
btx=(_nav*)malloc(sizeof(_nav));if(btx==NULL){setError(900); return;}
if(btu!=255){btx->BTU.assign(btu); btx->BTU.setMode(OneShot); btx->BTU.turnOnPullUp();}
if(btd!=255){btx->BTD.assign(btd); btx->BTD.setMode(OneShot); btx->BTD.turnOnPullUp();}
if(bte!=255){btx->BTE.assign(bte); btx->BTE.setMode(OneShot); btx->BTE.turnOnPullUp();}
if(btc!=255){btx->BTC.assign(btc); btx->BTC.setMode(OneShot); btx->BTC.turnOnPullUp();}
// bouncing disarm
btx->BTU.check();
btx->BTD.check();
btx->BTE.check();
btx->BTC.check();
MW_navbtn=4;
// check 6-buttonsmode uncompatibility
for(int i=0;i<idx_m;i++){
if (bitRead(m[i].flags,MW_MENU_COLLAPSED)){
bitWrite(m[i].flags,MW_MENU_COLLAPSED,false);
setError(410);
}
}
}
void menwiz::navButtons(int btu,int btd,int btl,int btr,int bte,int btc){
setError(0);
btx=(_nav*)malloc(sizeof(_nav));if(btx==NULL){setError(900); return;}
if(btu!=255){btx->BTU.assign(btu); btx->BTU.setMode(OneShot); btx->BTU.turnOnPullUp();}
if(btd!=255){btx->BTD.assign(btd); btx->BTD.setMode(OneShot); btx->BTD.turnOnPullUp();}
if(btl!=255){btx->BTL.assign(btl); btx->BTL.setMode(OneShot); btx->BTL.turnOnPullUp();}
if(btr!=255){btx->BTR.assign(btr); btx->BTR.setMode(OneShot); btx->BTR.turnOnPullUp();}
if(bte!=255){btx->BTE.assign(bte); btx->BTE.setMode(OneShot); btx->BTE.turnOnPullUp();}
if(btc!=255){btx->BTC.assign(btc); btx->BTC.setMode(OneShot); btx->BTC.turnOnPullUp();}
// bouncing disarm
btx->BTU.check();
btx->BTD.check();
btx->BTL.check();
btx->BTR.check();
btx->BTE.check();
btx->BTC.check();
MW_navbtn=6;
// check 6-buttonsmode uncompatibility
}
int menwiz::scanNavButtons(){
//Serial.print("menwiz::scanNavButtons()");
if (btx->BTU.check()==ON) return MW_BTU;
if (btx->BTD.check()==ON) return MW_BTD;
if (btx->BTE.check()==ON) return MW_BTE;
if (btx->BTC.check()==ON) return MW_BTC;
if (MW_navbtn==6 && btx->BTL.check()==ON) return MW_BTL;
if (MW_navbtn==6 && btx->BTR.check()==ON) return MW_BTR;
return MW_BTNULL;
}
#endif
void menwiz::actNavButtons(int button){
setError(0);
if (button==MW_BTNULL){
last_button=button;
return;
}
else{
switch(button){
case MW_BTU:
if (MW_navbtn==6){
actBTU();
}
else{
if(MW_invar)
actBTR();
else
actBTU();
}
break;
case MW_BTD:
if (MW_navbtn==6){
actBTD();
}
else{
if(MW_invar)
actBTL();
else
actBTD();
}
break;
case MW_BTL:
actBTL();
break;
case MW_BTR:
actBTR();
break;
case MW_BTE:
actBTE();
break;
case MW_BTC:
actBTC();
break;
}
}
tm_push=millis();
}
void menwiz::actBTU(){
last_button=MW_BTU;
char* s;
s=(char*)((_var*)cur_menu->var)->val;
if((cur_menu->type!=MW_VAR)||(((_var*)cur_menu->var)->type==MW_LIST)){
cur_menu->cur_item=(cur_menu->cur_item-1)<0?(cur_menu->idx_o-1):cur_menu->cur_item-1;
}
else if((((_var*)cur_menu->var)->type==MW_EDIT_TEXT)&&(MW_navbtn==6)){
if(s[cur_menu->cur_item]<MW_MAX_CHARLIST)
s[cur_menu->cur_item]++;
else
s[cur_menu->cur_item]=MW_MIN_CHARLIST;
}
}
void menwiz::actBTD(){
last_button=MW_BTD;
char* s;
s=(char*)((_var*)cur_menu->var)->val;
if((cur_menu->type!=MW_VAR)||(((_var*)cur_menu->var)->type==MW_LIST)){
cur_menu->cur_item=(cur_menu->cur_item+1)%(cur_menu->idx_o);
}
else if((((_var*)cur_menu->var)->type==MW_EDIT_TEXT)&&(MW_navbtn==6)){
if(s[cur_menu->cur_item]>MW_MIN_CHARLIST)
s[cur_menu->cur_item]--;
else
s[cur_menu->cur_item]=MW_MAX_CHARLIST;
}
}
void menwiz::actBTL(){
last_button=MW_BTL;
_menu* cm;
_option* oc;
oc=(_option*)cur_menu->o[cur_menu->cur_item];
cm=bitRead(cur_menu->flags,MW_MENU_COLLAPSED)? &m[oc->sbm]:cur_menu;
if(((_var*)cm->var)->type==MW_AUTO_BYTE){
VBYTE(((_var*)cm->var)->val)=max((VBYTE(((_var*)cm->var)->val)-VBYTE(((_var*)cm->var)->incr)),VBYTE(((_var*)cm->var)->lower));}
else if(((_var*)cm->var)->type==MW_AUTO_INT){
VINT(((_var*)cm->var)->val)=max((VINT(((_var*)cm->var)->val)-VINT(((_var*)cm->var)->incr)),VINT(((_var*)cm->var)->lower));}
else if(((_var*)cm->var)->type==MW_AUTO_FLOAT){
VFLOAT(((_var*)cm->var)->val)=max((VFLOAT(((_var*)cm->var)->val)-VFLOAT(((_var*)cm->var)->incr)),VFLOAT(((_var*)cm->var)->lower));}
else if(((_var*)cm->var)->type==MW_BOOL){
VBOOL(((_var*)cm->var)->val)=!VBOOL(((_var*)cm->var)->val);}
else if((((_var*)cm->var)->type==MW_LIST)&& MW_invar)
cm->cur_item=(cm->cur_item+1)%(cm->idx_o);
else if((((_var*)cur_menu->var)->type==MW_EDIT_TEXT)&&(MW_navbtn==6)){
cur_menu->cur_item=(cur_menu->cur_item-1<0)?(strlen((char*)((_var*)cur_menu->var)->val)-1):(cur_menu->cur_item-1);
//@
}
}
void menwiz::actBTR(){
_menu *cm;
_option* oc;
last_button=MW_BTR;
oc=(_option*)cur_menu->o[cur_menu->cur_item];
cm=bitRead(cur_menu->flags,MW_MENU_COLLAPSED)? &m[oc->sbm]:cur_menu;
if(((_var*)cm->var)->type==MW_AUTO_INT){
VINT(((_var*)cm->var)->val)=min((VINT(((_var*)cm->var)->val)+VINT(((_var*)cm->var)->incr)),VINT(((_var*)cm->var)->upper));}
else if(((_var*)cm->var)->type==MW_AUTO_BYTE){
VBYTE(((_var*)cm->var)->val)=min((VBYTE(((_var*)cm->var)->val)+VBYTE(((_var*)cm->var)->incr)),VBYTE(((_var*)cm->var)->upper));}
else if(((_var*)cm->var)->type==MW_AUTO_FLOAT){
VFLOAT(((_var*)cm->var)->val)=min((VFLOAT(((_var*)cm->var)->val)+VFLOAT(((_var*)cm->var)->incr)),VFLOAT(((_var*)cm->var)->upper));}
else if(((_var*)cm->var)->type==MW_BOOL){
VBOOL(((_var*)cm->var)->val)=!VBOOL(((_var*)cm->var)->val);}
else if((((_var*)cm->var)->type==MW_LIST)&&MW_invar)
cm->cur_item=(cm->cur_item-1)<0?(cm->idx_o-1):cm->cur_item-1;
else if((((_var*)cur_menu->var)->type==MW_EDIT_TEXT)&&(MW_navbtn==6)){
cur_menu->cur_item=(cur_menu->cur_item+1) % (strlen((char*)((_var*)cur_menu->var)->val));
//@
}
}
void menwiz::actBTE(){
_menu *cm;
last_button=MW_BTE;
cm=cur_menu;
if(cm->type==MW_VAR){
if(((_var*)cm->var)->type==MW_AUTO_INT){
VINT(((_var*)cm->var)->val)=VINT(((_var*)cm->var)->old);}
else if(((_var*)cm->var)->type==MW_AUTO_FLOAT){
VFLOAT(((_var*)cm->var)->val)=VFLOAT(((_var*)cm->var)->old);}
else if(((_var*)cm->var)->type==MW_AUTO_BYTE){
VBYTE(((_var*)cm->var)->val)=VBYTE(((_var*)cm->var)->old);}
else if(((_var*)cm->var)->type==MW_BOOL){
VBOOL(((_var*)cm->var)->val)=VBOOL(((_var*)cm->var)->old);}
else if((((_var*)cm->var)->type==MW_EDIT_TEXT)){
lcd->noBlink();
}
}
cur_menu=&m[cur_menu->parent];
MW_invar=false;
}
void menwiz::actBTC(){
_menu *mc;
_option* oc;
last_button=MW_BTC;
oc=(_option*)cur_menu->o[cur_menu->cur_item];
mc=&m[oc->sbm];
if((cur_menu->type==MW_SUBMENU)||(cur_menu->type==MW_ROOT)){
if(bitRead(mc->flags,cur_user)==false){
return;
}
cur_menu=mc;
if(cur_menu->type==MW_VAR){
if(bitRead(m[cur_menu->parent].flags,MW_MENU_COLLAPSED)){
if ((((_var*)cur_menu->var)->type!=MW_LIST)&&((((_var*)cur_menu->var)->type!=MW_ACTION))) {
cur_menu=&m[cur_menu->parent];
MW_invar=false;
}
else{
MW_invar=true;
}
return;
}
else if((((_var*)cur_menu->var)->type==MW_ACTION) && (!bitRead(cur_menu->flags,MW_ACTION_CONFIRM))){
((_act*)cur_menu->var)->action();
cur_menu=&m[cur_menu->parent];
MW_invar=false;
return;
}
else if((((_var*)cur_menu->var)->type==MW_EDIT_TEXT)){
MW_invar=false;
return;
}
else{
MW_invar=true;
return;
}
}
}
else if(cur_menu->type==MW_VAR){
if(((_var*)cur_menu->var)->type==MW_LIST){
VINT(((_var*)cur_menu->var)->val)=cur_menu->cur_item;}
else if(((_var*)cur_menu->var)->type==MW_AUTO_INT){
VINT(((_var*)cur_menu->var)->old)=VINT(((_var*)cur_menu->var)->val);}
else if(((_var*)cur_menu->var)->type==MW_AUTO_FLOAT){
VFLOAT(((_var*)cur_menu->var)->old)=VFLOAT(((_var*)cur_menu->var)->val);}
else if(((_var*)cur_menu->var)->type==MW_AUTO_BYTE){
VBYTE(((_var*)cur_menu->var)->old)=VBYTE(((_var*)cur_menu->var)->val);}
else if(((_var*)cur_menu->var)->type==MW_BOOL){
VBOOL(((_var*)cur_menu->var)->old)=VBOOL(((_var*)cur_menu->var)->val);}
else if((((_var*)cur_menu->var)->type==MW_ACTION)&&(bitRead(cur_menu->flags,MW_ACTION_CONFIRM))){
((_act*)cur_menu->var)->action();}
else if(((_var*)cur_menu->var)->type==MW_EDIT_TEXT){
}
cur_menu=&m[cur_menu->parent];
MW_invar=false;
if(((_var*)cur_menu->var)->type!=MW_ACTION){ //we dont need eeprom or effect on action
#ifdef EEPROM_SUPPORT
if (eeprom_write_on_confirm) writeEeprom();
#endif
if (usrConfirmAction.fl) usrConfirmAction.fv();
//simple effect: blackout on save for 250ms
for (uint8_t j=0;j<100;j++){lcd->write(byte(255));}delay(250);
}
}
}
int menwiz::error = 0;
void menwiz::setError(int err){
error = err;
#ifdef MW_PRINT_ERRORS
getErrorMessage(true);
#endif
}
int menwiz::getErrorMessage(bool fl){
#ifdef MW_PRINT_ERRORS
if (fl){
switch(error){
case 0: break;
case 100: Serial.println(F("E100-Too many items. Increment MAX_MENU"));break;
case 130: Serial.println(F("E130-Invalid buttons number: allowed 4 or 6"));break;
case 200: Serial.println(F("E200-Root undefined"));break;
case 105: Serial.println(F("E105-Too many items. Increment MAX_OPTXMENU"));break;
case 110: Serial.println(F("E110-MW_VAR menu type required"));break;
case 120: Serial.println(F("E120-Bad 1st arg"));break;
case 300: Serial.println(F("E300-Undefined variable type"));break;
case 310: Serial.println(F("E310-Unknown behaviour"));break;
case 320: Serial.println(F("E320-Text variable pointer is NULL"));break;
case 410: Serial.println(F("E410-Behavihour available only with 6 buttons. ignored"));break;
case 500: Serial.println(F("E500-out of user range (1-3)"));break;
case 900: Serial.println(F("E900-Out of memory"));break;
default: Serial.println(F("E000-Unknown err: "));Serial.println(error);break;