forked from liudr/phi_prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphi_prompt.cpp
More file actions
1919 lines (1758 loc) · 81.3 KB
/
phi_prompt.cpp
File metadata and controls
1919 lines (1758 loc) · 81.3 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
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
/*
.______ __ __ __ .______ .______ ______ .___ ___. .______ .___________.
| _ \ | | | | | | | _ \ | _ \ / __ \ | \/ | | _ \ | |
| |_) | | |__| | | | | |_) | | |_) | | | | | | \ / | | |_) | `---| |----`
| ___/ | __ | | | | ___/ | / | | | | | |\/| | | ___/ | |
| | | | | | | | | | | |\ \----.| `--' | | | | | | | | |
| _| |__| |__| |__| _____| _| | _| `._____| \______/ |__| |__| | _| |__|
_______ .______ __ __ __ __
| \ | _ \ | | | | | | | |
| .--. || |_) | | | | | | | | |
| | | || / | | | | | | | |
| '--' || |\ \----. __ | `----.| | | `--' |
|_______/ | _| `._____| (__) |_______||__| \______/
http://liudr.wordpress.com
*/
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <avr/pgmspace.h>
#include <phi_interfaces.h>
#include <phi_prompt.h>
const char phi_prompt_lcd_ch0[] PROGMEM ={ 4,14,31,64,31,31,31,31,0}; ///< Custom LCD character: Up triangle with block
const char phi_prompt_lcd_ch1[] PROGMEM ={ 4,14,31,64,64,64,64,64,0}; ///< Custom LCD character: Up triangle
const char phi_prompt_lcd_ch2[] PROGMEM ={31,31,31,31,64,64,64,64,0}; ///< Custom LCD character: Top block
const char phi_prompt_lcd_ch3[] PROGMEM ={64,64,64,64,31,31,31,31,0}; ///< Custom LCD character: Bottom block
const char phi_prompt_lcd_ch4[] PROGMEM ={64,64,64,64,64,31,14, 4,0}; ///< Custom LCD character: Down triangle
const char phi_prompt_lcd_ch5[] PROGMEM ={31,31,31,31,64,31,14, 4,0}; ///< Custom LCD character: Down triangle with block
const char phi_prompt_lcd_ch6[] PROGMEM ={B01000, B01100, B01010, B01001, B01001, B11010, B11000, B00000,0}; ///< Custom LCD character: Music
const char phi_prompt_lcd_ch7[] PROGMEM ={B00111, B00100, B00000, B11100, B10000, B11101, B00010, B00101,0}; ///< Custom LCD character: Lux
const char * const phi_prompt_lcd_ch_item[] PROGMEM = {phi_prompt_lcd_ch0, phi_prompt_lcd_ch1, phi_prompt_lcd_ch2, phi_prompt_lcd_ch3, phi_prompt_lcd_ch4, phi_prompt_lcd_ch5, phi_prompt_lcd_ch6, phi_prompt_lcd_ch7}; ///< Custom LCD character char array addresses.
const char PROGMEM yn_00[] PROGMEM =" YES >NO<"; ///< This list item is used to render Y/N dialog
const char PROGMEM yn_01[] PROGMEM =">YES< NO "; ///< This list item is used to render Y/N dialog
const char * const yn_items[] PROGMEM = {yn_00,yn_01}; ///< This list is used to render Y/N dialog
static LCD * lcd; ///< This pointer stores the LiquidCrystal object for display purpose.
static int lcd_w; ///< This is the width of the LCD in number of characters.
static int lcd_h; ///< This is the height of the LCD in number of characters.
static char indicator; ///< This is the character used as indicator in lists/menus. The highlighted item is indicated by this character. Use '~' for a right arrow.
static char bullet; ///< This is the bullet used in lists/menus. The non-highlighted items are indicated by this character. Use '\xA5' for a center dot.
static char ** function_keys; ///< This points to an array of pointers that each is a zero-terminated string representing function keys.
static multiple_button_input ** mbi_ptr; ///< This points to an array of pointers that each points to a multiple_button_input object.
static byte lcd_type; ///< This indicates the type of lcd, such as HD44780 or GLCD.
static boolean key_repeat_enable=1; ///< This is not used in this version. A future version may make use of it.
static boolean multi_tap_enable=0; ///< This is not used in this version. A future version may make use of it.
static int simple_option=0; ///< This stores the required menu option for simple list.
#ifndef disable_simple_fns
static phi_prompt_struct shared_struct; ///< This struct is shared among simple function calls.
#endif
//Utilities
/**
* \details This initializes the phi_prompt library. It needs to be called before any phi_prompt functions are called.
* \param l This is the address of your LCD object, which you already used begin() on, such as &LCD.
* \param k This is the name of the pointer array that stores the address of (pointer to) all your input keypads. The last element of the array needs to be 0 to terminate the array.
* \param fk This is the name of the array that stores the names of all char arrays with function keys. Make sure you use strings such as "U" for each array to indicate function keys instead of char like 'U'.
* \param w This is the width of the LCD in number of characters.
* \param h This is the height of the LCD in number of characters.
* \param i This is the character used as indicator in lists/menus. The highlighted item is indicated by this character. Use '~' for a right arrow.
*/
void init_phi_prompt(LCD *l, multiple_button_input *k[], char ** fk, int w, int h, char i)
{
lcd=l;
mbi_ptr=k;
function_keys=fk;
lcd_w=w;
lcd_h=h;
indicator=i;
bullet='\xA5';
byte ch_buffer[10]; // This buffer is required for custom characters on the LCD.
if (lcd!=0)
{
for (int i=0;i<8;i++)
{
strcpy_P((char*)ch_buffer,(char*)pgm_read_word(&(phi_prompt_lcd_ch_item[i])));
lcd->createChar(i, ch_buffer);
}
}
lcd_type=HD44780_lcd;
}
void set_indicator(char i)
{
indicator=i;
}
void set_bullet(char i)
{
bullet=i;
}
void set_repeat_time(int i)
{
mbi_ptr[0]->set_repeat(i);
}
void enable_key_repeat(boolean i)
{
key_repeat_enable=i;
}
void enable_multi_tap(boolean i)
{
//if (!multi_tap_enable) multi_tap(init);
multi_tap_enable=i;
}
/**
* \details Increment character ch according to options set in para. This function is used in input panel with up/down key to increment the current character to the next.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* Option 0: default, option 1: include 0-9 as valid inputs, option 2: only 0-9 are valid, option 3: only 0-9 are valid and the first digit can be '-', option 4: only 0-9 are valid and number increments, option 5: only 0-9 are valid and the first digit can be '-' and number increments.
* To do:
* Option 2-4, automatically increment or decrement a whole series of numbers when they are connected, such as File0009.txt when increase 9 (if only 0-9 are allowed) will give File0010.txt.
* If you are not interested in the inner working of this library, don't call it.
* \return It returns the character after the increment.
*/
char inc(char ch, phi_prompt_struct *para)
{
if ((ch<para->high.c)&&(ch>=para->low.c)) return (++ch);
else
{
switch (para->option) /// The following is the switch-case for character increment processing.
{
case 0: /// No options. The high and low determine range of characters you can enter.
if (ch==para->high.c) ch=para->low.c;
break;
case 1: /// Include 0-9
if (ch=='9') ch=para->low.c;
else if ((ch>='0')&&(ch<'9')) ch++;
else if (ch==para->high.c) ch='0';
break;
case 2: /// only 0-9
case 4: /// only 0-9 and automatically decrements (done at higher level)
if (ch=='9') ch='0';
else ch++;
break;
case 3: /// only 0-9 and '-'
case 5: /// only 0-9 and '-' and automatically increments (done at higher level)
if (ch=='9') ch='-';
else if (ch=='-') ch='0';
else ch++;
break;
}
}
return ch;
}
/**
* \details Decrement character ch according to options set in para. This function is used in input panel with up/down key to decrement the current character to the previous.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* Option 0: default, option 1: include 0-9 as valid inputs, option 2: only 0-9 are valid, option 3: only 0-9 are valid and the first digit can be '-', option 4: only 0-9 are valid and number increments, option 5: only 0-9 are valid and the first digit can be '-' and number increments.
* To do:
* Option 2-4, automatically increment or decrement a whole series of numbers when they are connected, such as File0009.txt when increase 9 (if only 0-9 are allowed) will give File0010.txt.
* If you are not interested in the inner working of this library, don't call it.
* \return It returns the character after the decrement.
*/
char dec(char ch, phi_prompt_struct *para)// Decrease character. Used in input panels
{
if ((ch<=para->high.c)&&(ch>para->low.c)) return (--ch);
else
{
switch (para->option) /// The following is the switch-case for character decrement processing.
{
case 0: /// No options. The high and low determine range of characters you can enter.
if (ch==para->low.c) ch=para->high.c;
break;
case 1: /// Include 0-9
if (ch=='0') ch=para->high.c;
else if ((ch>'0')&&(ch<='9')) ch--;
else if (ch==para->low.c) ch='9';
break;
case 2: /// only 0-9
case 4: /// only 0-9 and automatically decrements (done at higher level)
if (ch=='0') ch='9';
else ch--;
break;
case 3: /// only 0-9 and '-'
case 5: /// only 0-9 and '-' and automatically decrements (done at higher level)
if (ch=='0') ch='-';
else if (ch=='-') ch='9';
else ch--;
break;
}
}
return ch;
}
/**
* \details This function translates key press returned from all keypads. This function is only called by wait_on_escape.
* \return If a key is defined as a function key, it returns the code of the function key defined in the function_key_base define section.
* If the key is not a function key, it returns the key unaltered.
* If you are not interested in the inner working of this library, don't call it.
*/
char phi_prompt_translate(char key)
{
for (byte i=0;i<total_function_keys;i++)
{
byte j=0;
while(function_keys[i][j])
{
if (key==function_keys[i][j]) return (function_key_code_base+i);
j++;
}
}
return key;
}
//Text renderers
/**
* \details This function displays a short message in SRAM centered with the display size.
* This outputs a text center-aligned on the display depending on the display size.
* Eg. center_text("Introduction") outputs ">>>>Introduction<<<<" on 20 column display.
* \param src This is the string to be displayed.
*/
void center_text(char * src)
{
byte j=0;
for (byte i=0;i<lcd_w;i++)
{
if (i<lcd_w/2-(strlen(src)-strlen(src)/2)) lcd->write('>');
else if (i>=lcd_w/2+strlen(src)/2) lcd->write('<');
else
{
lcd->write(src[j]);
j++;
}
}
}
/**
* \details This function displays a short message in PROGMEM centered with the display size.
* This outputs a text center-aligned on the display depending on the display size.
* Eg. center_text("Introduction") outputs ">>>>Introduction<<<<" on 20 column display.
* \param src This is the string to be displayed.
*/
void center_text_P(PGM_P src)
{
byte j=0;
for (byte i=0;i<lcd_w;i++)
{
if (i<lcd_w/2-(strlen_P(src)-strlen_P(src)/2)) lcd->write('>');
else if (i>=lcd_w/2+strlen_P(src)/2) lcd->write('<');
else
{
lcd->write(pgm_read_byte_near(src+j));
j++;
}
}
}
/**
* \details This copies the right amount of text into a narrow space so it can be displayed and scrolled to show the entire message.
* If you are not very interested in the inner working of this library, this is not for you. It is used to auto scroll long list items.
* \param src This points to the long string that needs to be scrolled inside a narrow line.
* \param dst This points to the buffer that has a length of the narrow line width + 1 (for 0 termination). The part of the text that will be displayed is copied to this buffer after the function call.
* \param dst_len This is the width of the narrow line. Only this many characters from the source will be copied to the dst buffer.
* \param pos This is the position of the long text. When pos=0, the text's first portion displays left justified in the dst buffer. When pos<0, spaces are padded before the text, useful for scrolling from right to left. When pos>dst_len, space is padded to the end of the text to make it scroll all the way to the left and eventually disappear.
* \return It returns the text to be displayed in the buffer and no value is directly returned.
*/
void scroll_text(char * src, char * dst, char dst_len, short pos)
{
for (byte j=0;j<dst_len;j++)
{
if ((pos<0)||(pos>strlen(src)-1))
{
dst[j]=' ';
}
else dst[j]=src[pos];
pos++;
}
dst[dst_len]=0;
}
/**
* \details This copies the right amount of text (stored in PROGMEM) into a narrow space so it can be displayed and scrolled to show the entire message.
* If you are not very interested in the inner working of this library, this is not for you. It is used to auto scroll long list items.
* \param src This points to the long string that needs to be scrolled inside a narrow line.
* \param dst This points to the buffer that has a length of the narrow line width + 1 (for 0 termination). The part of the text that will be displayed is copied to this buffer after the function call.
* \param dst_len This is the width of the narrow line. Only this many characters from the source will be copied to the dst buffer.
* \param pos This is the position of the long text. When pos=0, the text's first portion displays left justified in the dst buffer. When pos<0, spaces are padded before the text, useful for scrolling from right to left. When pos>dst_len, space is padded to the end of the text to make it scroll all the way to the left and eventually disappear.
* \return It returns the text to be displayed in the buffer and no value is directly returned.
*/
void scroll_text_P(PGM_P src, char * dst, char dst_len, short pos)
{
for (byte j=0;j<dst_len;j++)
{
if ((pos<0)||(pos>strlen_P(src)-1))
{
dst[j]=' ';
}
else dst[j]=pgm_read_byte_near(src+pos);
pos++;
}
dst[dst_len]=0;
}
/**
* \details This is a quick and easy way to display a string in the PROGMEM to the LCD. Note the string should have a maximum of 20 characters.
* \param msg_line This is the name of the char string stored in PROGMEM.
*/
void msg_lcd(char* msg_line)
{
byte i=0,ch;
while (ch=pgm_read_byte_near(msg_line+i))
{
i++;
lcd->write(ch);
}
// lcd->print(phi_prompt_max_item_display_length);
}
/**
* \details Seeks previous line in a long message stored in SRAM. This seems easy until you start adding \n and \t etc. into the picture.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* This function assumes the long message position has been always saught properly. Any improper movement of the long message pointer could produce unpredictable results.
* If you are not interested in the inner working of this library, use text_area instead.
* Return values are updated throught the struct.
*/
void prev_line(phi_prompt_struct* para)
{
byte columns=para->step.c_arr[1];
if (para->low.i<=0)
{
para->low.i=0;
return;
}
if (para->ptr.msg[para->low.i-1]=='\n')
{ //Seek beginning of a paragraph.
int dec=para->low.i-2;
while(para->ptr.msg[dec]!='\n')
{
dec--;
if (dec==0)
{
para->low.i=0;
return;
}
}
para->low.i-=((para->low.i-1-dec-1)%columns+1);
}
else para->low.i-=columns;
return;
}
/**
* \details Seeks next line in a long message stored in SRAM. This seems easy until you start adding \n and \t etc. into the picture.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* This function assumes the long message position has been always saught properly. Any improper movement of the long message pointer could produce unpredictable results.
* If you are not interested in the inner working of this library, use text_area instead.
* Return values are updated throught the struct.
*/
void next_line(phi_prompt_struct* para)
{
byte columns=para->step.c_arr[1];
for (int i=para->low.i;i<para->low.i+columns;i++)
{
if (para->ptr.msg[i]=='\n')
{
para->low.i=i+1;
return;
}
if (i==strlen(para->ptr.msg))
{
return;
}
}
para->low.i+=columns;
}
/**
* \details Seeks previous line in a long message stored in PROGMEM. This seems easy until you start adding \n and \t etc. into the picture.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* This function assumes the long message position has been always saught properly. Any improper movement of the long message pointer could produce unpredictable results.
* If you are not interested in the inner working of this library, use text_area instead.
* Return values are updated throught the struct.
*/
void prev_line_P(phi_prompt_struct* para)
{
byte columns=para->step.c_arr[1];
if (para->low.i<=0)
{
para->low.i=0;
return;
}
if (pgm_read_byte_near(para->ptr.msg_P+para->low.i-1)=='\n')
{ //Seek beginning of a paragraph.
int dec=para->low.i-2;
while(pgm_read_byte_near(para->ptr.msg_P+dec)!='\n')
{
dec--;
if (dec==0)
{
para->low.i=0;
return;
}
}
para->low.i-=((para->low.i-1-dec-1)%columns+1);
}
else para->low.i-=columns;
return;
}
/**
* \details Seeks next line in a long message stored in PROGMEM. This seems easy until you start adding \n and \t etc. into the picture.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* This function assumes the long message position has been always saught properly. Any improper movement of the long message pointer could produce unpredictable results.
* If you are not interested in the inner working of this library, use text_area instead.
* Return values are updated throught the struct.
*/
void next_line_P(phi_prompt_struct* para)
{
byte columns=para->step.c_arr[1];
for (int i=para->low.i;i<para->low.i+columns;i++)
{
if (pgm_read_byte_near(para->ptr.msg_P+i)=='\n')
{
para->low.i=i+1;
return;
}
if (i==strlen_P(para->ptr.msg_P))
{
return;
}
}
para->low.i+=columns;
}
/**
* \details Displays a static long message stored in SRAM that could span multiple lines.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* Option 0: just display message. Option 1: display message with a scrollbar to the right.
* If you are not interested in the inner working of this library, use text_area instead.
* Return values are updated throught the struct.
*/
void long_msg_lcd(phi_prompt_struct* para)
{
byte columns=para->step.c_arr[1], rows=para->step.c_arr[0], ch;
int inc=0;
// lcd->noBlink();
for (byte i=0;i<rows;i++)
{
if ((para->low.i+inc>=strlen(para->ptr.msg))||(para->ptr.msg[para->low.i+inc]=='\n'))
{
ch=0;
inc++;
}
else ch=para->ptr.msg[para->low.i+inc];
lcd->setCursor(para->col,para->row+i);
for (byte j=0;j<columns;j++)
{
if (ch==0) lcd->write(' ');
else
{
lcd->write(ch);
ch=para->ptr.msg[para->low.i+(++inc)];
if ((ch=='\n')&&(j<columns-1))
{
ch=0;
inc++;
}
}
}
}
if (para->option==1)
{
scroll_bar_v(((long)para->low.i)*100/strlen(para->ptr.msg),para->col+columns,para->row,rows);
}
}
/**
* \details Displays a static long message stored in PROGMEM that could span multiple lines.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* Option 0: just display message. Option 1: display message with a scrollbar to the right.
* If you are not interested in the inner working of this library, use text_area instead.
* Return values are updated throught the struct.
*/
void long_msg_lcd_P(phi_prompt_struct* para) // Displays a long message stored in PROGMEM that could span multiple lines.
{
byte columns=para->step.c_arr[1], rows=para->step.c_arr[0], ch;
int inc=0;
// lcd->noBlink();
for (byte i=0;i<rows;i++)
{
if ((para->low.i+inc>=strlen_P(para->ptr.msg_P))||(pgm_read_byte_near(para->ptr.msg_P+para->low.i+inc)=='\n'))
{
ch=0;
inc++;
}
else ch=pgm_read_byte_near(para->ptr.msg_P+para->low.i+inc);
lcd->setCursor(para->col,para->row+i);
for (byte j=0;j<columns;j++)
{
if (ch==0) lcd->write(' ');
else
{
lcd->write(ch);
ch=pgm_read_byte_near(para->ptr.msg_P+para->low.i+(++inc));
if ((ch=='\n')&&(j<columns-1))
{
ch=0;
inc++;
}
}
}
}
if (para->option==1)
{
scroll_bar_v(((long)para->low.i)*100/strlen_P(para->ptr.msg_P),para->col+columns,para->row,rows);
}
}
/**
* \details Displays a scroll bar at column/row with height and percentage.
* If you are not very interested in the inner working of this library, this is not for you.
* This is the only function in phi_prompt library to use custom characters.
* If you used custom characters and want to use this function or a long message with scroll bar, run the init again to reinitialize custom characters.
* \param percentage This goes between 0 and 99, representing the location indicator on the bar.
* \param column This is the column location of the scroll bar's top.
* \param row This is the row location of the scroll bar's top.
* \param v_height This is the height of the bar in number of rows.
*/
void scroll_bar_v(byte percent, byte column, byte row, byte v_height)
{
int mapped;
if (percent>99) percent=99;
mapped=(int)(v_height*2-2)*percent/100; // This is mapped position, 2 per row of bar.
for (byte i=0;i<v_height;i++)
{
lcd->setCursor(column,row+i);
if (i==(mapped+1)/2)
{
if (i==0)
{
lcd->write((uint8_t)0);
}
else if (i==v_height-1)
{
lcd->write(5);
}
else
{
if (mapped+1==(mapped+1)/2*2) lcd->write(2);
else lcd->write(3);
}
}
else
{
if (i==0)
{
lcd->write(1);
}
else if (i==v_height-1)
{
lcd->write(4);
}
else
{
lcd->write(' ');
}
}
}
}
/**
* \details Displays a static list or menu stored in SRAM or PROGMEM that could span multiple lines.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* Option is very extensive and please refer to the documentation and option table.
* The options are combined with OR operation with "Render list option bits" you can find in the library include file.
* To find out what each option does exactly, run phi_prompt_big_show demo code and try the options out and write the number down.
* If you are not interested in the inner working of this library, use text_area instead.
* \return If further update is needed, it returns 1. The caller needs to call it again to update display, such as scrolling item.
* If it returns 0 then no further display update is needed and the caller can stop calling it.
*/
byte render_list(phi_prompt_struct* para)
{
byte ret=0, _first_item, _last_item, columns=para->step.c_arr[1], rows=para->step.c_arr[0], item_per_screen=columns*rows, x1=para->col, y1=para->row, x2=para->step.c_arr[3], y2=para->step.c_arr[2]; // Which items to display.
char list_buffer[phi_prompt_max_item_display_length+2]; ///< Each list item can have an indicator such as dot or arrow and the item needs to be zero-terminated so 2 extra characters.
long pos=millis()/500;
if (para->width>phi_prompt_max_item_display_length) para->width=phi_prompt_max_item_display_length; ///< Make sure the caller doesn't demand a list item that is longer than the list_buffer can hold.
if (para->option&phi_prompt_center_choice) // Determine first item on whether choice is displayed centered.
{
_first_item=para->low.i-item_per_screen/2;
if (_first_item>127) _first_item=0;
else if ((para->high.i+1>=item_per_screen) && (para->low.i-item_per_screen/2+item_per_screen>para->high.i)) _first_item=para->high.i+1-item_per_screen;
}
else
{
_first_item=(para->low.i/item_per_screen)*item_per_screen;
}
_last_item=_first_item+item_per_screen-1; // Determine last item based on first item, total item per screen, and total item.
if (_last_item>para->high.i) _last_item=para->high.i;
for (byte i=_first_item;i<_first_item+item_per_screen;i++)
{
if (i<=_last_item) // Copy item
{
if ((para->option&phi_prompt_list_in_SRAM))
{
if ((para->option&phi_prompt_auto_scroll)&&(i==para->low.i)&&strlen(*(para->ptr.list+i))>para->width) // Determine what portion of the item to be copied. In case of no auto scrolling, only first few characters are copied till the display buffer fills. In case of auto scrolling, a certain portion of the item is copied.
{
pos=pos%(strlen(*(para->ptr.list+i))+para->width)-para->width;
scroll_text(*(para->ptr.list+i), list_buffer, para->width, pos);//Does the actual copy
ret=1; // More update is needed to scroll text.
}
else //Does the actual truncation
{
byte len=strlcpy(list_buffer,*(para->ptr.list+i), para->width+1);
if (len<para->width)
{
for (byte k=len;k<para->width;k++)
{
list_buffer[k]=' ';
}
list_buffer[para->width]=0;
}
}
}
else // The list is in PROGMEM
{
if ((para->option&phi_prompt_auto_scroll)&&(i==para->low.i)&&strlen_P((char*)pgm_read_word(para->ptr.list+i))>para->width) // Determine what portion of the item to be copied. In case of no auto scrolling, only first few characters are copied till the display buffer fills. In case of auto scrolling, a certain portion of the item is copied.
{
pos=pos%(strlen_P((char*)pgm_read_word(para->ptr.list+i))+para->width)-para->width;
scroll_text_P((char*)pgm_read_word(para->ptr.list+i), list_buffer, para->width, pos);//Does the actual copy with scroll_text_P
ret=1; // More update is needed to scroll text.
}
else //Does the actual truncation
{
byte len=strlcpy_P(list_buffer,(char*)pgm_read_word(para->ptr.list+i), para->width+1);
if (len<para->width)
{
for (byte k=len;k<para->width;k++)
{
list_buffer[k]=' ';
}
list_buffer[para->width]=0;
}
}
}
}
else // Fill blank
{
byte j;
for (j=0;j<para->width;j++)
{
list_buffer[j]=' ';
}
list_buffer[j]=0;
}
//Display item on LCD
lcd->setCursor(para->col+((i-_first_item)/rows)*(para->width+1), para->row+(i-_first_item)%rows);
if (para->option&phi_prompt_arrow_dot) // Determine whether to render arrow and dot. In case of yes, the buffer is shifted to the right one character.
{
if (i<=_last_item)
{
lcd->write((i==para->low.i)?indicator:bullet);// Show ">" or a dot
}
else
{
lcd->write(' ');
}
}
lcd->print(list_buffer);
}
if (para->option&phi_prompt_index_list) // Determine whether to display 1234567890 index
{
lcd->setCursor(x2,y2);
for (byte i=0;i<=para->high.i;i++)
{
if (i==para->low.i) lcd->write(indicator); // Display indicator on index
else lcd->write(i%10+'1');
}
}
else if (para->option&phi_prompt_current_total) // Determine whether to display current/total index
{
if (para->high.i+1<10) sprintf(list_buffer,"%c%d/%d", indicator,para->low.i+1, para->high.i+1); // Make sure the index will occupy 1 digit+/+1 digit when there are less than 10 items
else sprintf(list_buffer,"%c%2d/%2d", indicator,para->low.i+1, para->high.i+1); // Make sure the index will occupy 2 digit+/+2 digit when there are 10 or more items
lcd->setCursor(x2,y2);
lcd->print(list_buffer);// Prints index
}
if (para->option&phi_prompt_scroll_bar) // Determine whether to display scroll bar
{
scroll_bar_v(((int)para->low.i+1)*100/(para->high.i+1),para->col+columns*(para->width+1)-1*(!(para->option&phi_prompt_arrow_dot)),para->row,rows);
}
if (para->option&phi_prompt_flash_cursor) // Determine whether to display flashing cursor
{
lcd->setCursor(para->col+((para->low.i-_first_item)/rows)*(para->width+1), para->row+(para->low.i-_first_item)%rows);
lcd->blink();
}
else lcd->noBlink();
if (para->option&phi_prompt_invert_text) // Determine whether to invert highlighted item
{
}
return ret;
}
//Interactions
/**
* \details This function is the center of phi_prompt key sensing. It polls all input keypads for inputs for the length of ref_time in ms
* If a key press is sensed, it attempts to translate it into function keys or pass the result unaltered if it is not a function key.
* It only detects one key presses so holding multiple keys will not produce what you want.
* For function key codes, refer to the "Internal function key codes" section in the library header.
* \param ref_time This is the time the function is allowed to wait, while polling on all input keypads.
* \return Returns translated key or NO_KEY if no key is detected after the time expires.
*/
int wait_on_escape(int ref_time)
{
//Wait on button push.
long temp0;
byte temp1;
temp0=millis();
do
{
byte i=0;
while(mbi_ptr[i])
{
temp1=mbi_ptr[i]->getKey();
if (temp1!=NO_KEY) return (phi_prompt_translate(temp1));
i++;
}
} while ((millis()-temp0<ref_time));
return (NO_KEY);
}
//Inputs
/**
* \details Input an integer value with wrap-around capability. Integers are inputted with up and down function keys. The value has upper and lower limits and step.
* Pressing number keys has no effect since the input needs to be restricted with limits and steps.
* This function prints the initial value first so the caller doesn't need to.
* Function traps until the update is finalized by the left, right, enter button or escape button.
* Return values are updated throught the pointer.
* \param para This is the phi_prompt struct to carry information between callers and functions.
* Display options for integers:
* 0: space pad right, 1: zero pad left, 2: space pad left.
* \return Returns function keys pushed so the caller can determine what to do:
* Go back to the last slot with left (-3)
* Go forward to the next slot with right (-4)
* Enter(1)
* Escape(-1).
*/
int input_integer(phi_prompt_struct *para)
{
int number=*(para->ptr.i_buffer);
byte temp1;
byte space=para->width;
char msg[space+1];
char format[]="%00d";
for (byte i=0;i<space;i++) msg[i]=' '; // Create mask the size of the output space.
msg[space]=0;
lcd->setCursor(para->col,para->row); // Write mask to erase previous content.
lcd->print(msg);
lcd->setCursor(para->col,para->row); // Write initial value.
switch (para->option) // Prints out the content once before accepting user inputs.
{
case 0://padding with space to the right (padding done in erasing phase)
sprintf(msg,"%d",number);
break;
case 1://padding with zero to the left
format[2]='0'+space;
sprintf(msg,format,number);
break;
case 2://padding with space to the left
format[1]='0'+space;
format[2]='d';
format[3]='\0';
sprintf(msg,format,number);
break;
}
lcd->print(msg);
lcd->setCursor(para->col,para->row); // Write initial value.
lcd->cursor();
while(true)
{
temp1=wait_on_escape(50);
switch (temp1) /// The following is the switch-case for key press processing.
{
case phi_prompt_up: /// Up is pressed. The number increments with a step size of para->step.i. The number wraps around.
if (number+(para->step.i)<=(para->high.i)) number+=(para->step.i);
else number=para->low.i;
lcd->setCursor(para->col,para->row);
for (byte i=0;i<space;i++) msg[i]=' '; // Create mask the size of the output space.
msg[space]=0;
lcd->print(msg);
lcd->setCursor(para->col,para->row);
switch (para->option)
{
case 0://padding with space to the right (padding done in erasing phase)
sprintf(msg,"%d",number);
break;
case 1://padding with zero to the left
format[2]='0'+space;
sprintf(msg,format,number);
break;
case 2://padding with space to the left
format[1]='0'+space;
format[2]='d';
format[3]='\0';
sprintf(msg,format,number);
break;
}
lcd->print(msg);
lcd->setCursor(para->col,para->row);
break;
case phi_prompt_down: /// Down is pressed. The number decrements with a step size of para->step.i. The number wraps around.
if (number-para->step.i>=para->low.i) number-=para->step.i;
else number=para->high.i;
lcd->setCursor(para->col,para->row);
for (byte i=0;i<space;i++) msg[i]=' '; // Create mask the size of the output space.
msg[space]=0;
lcd->print(msg);
lcd->setCursor(para->col,para->row);
switch (para->option)
{
case 0://padding with space to the right (padding done in erasing phase)
sprintf(msg,"%d",number);
break;
case 1://padding with zero to the left
format[2]='0'+space;
sprintf(msg,format,number);
break;
case 2://padding with space to the left
format[1]='0'+space;
format[2]='d';
format[3]='\0';
sprintf(msg,format,number);
break;
}
lcd->print(msg);
lcd->setCursor(para->col,para->row);
break;
case phi_prompt_left: /// Left is pressed. The function returns -3. This can be used to switch to an entry to the left.
*(para->ptr.i_buffer)=number;
lcd->noCursor();
return (-3);
break;
case phi_prompt_right: /// Right is pressed. The function returns -4. This can be used to switch to an entry to the right.
*(para->ptr.i_buffer)=number;
lcd->noCursor();
return (-4);
break;
case phi_prompt_enter: /// Enter is pressed. The function returns 1.
*(para->ptr.i_buffer)=number;
lcd->noCursor();
return(1);
break;
case phi_prompt_escape: /// Escape is pressed. The function returns -1.
lcd->noCursor();
return (-1);
break;
default:
break;
}
}
}
/*
This is still supported but not number keypad support has not been expanded to it and it will disappear from the library in future releases.
Input a floating point value with fixed decimal point. Ironic but true.
This function prints the initial value first so the caller doesn't need to.
Function traps until the update is finalized by the enter button or escape button.
Return values are updated throught the pointer.
Returns buttons pushed so the caller can determine what to do:
Go back to the last slot with left (-3)
Go forward to the next slot with right (-4)
Enter(1)
Escape(-1).
Display options for floats:
0: only positive numbers allowed, 1: only negative numbers allowed, 2: both positive and negative numbers are allowed.
*/
int input_float(phi_prompt_struct *para)
{
phi_prompt_struct myTextInput;
char number[17]; // This is the buffer that will store the content of the text panel.
char format[]="%04d.%02d";
byte before=para->step.c_arr[1], after=para->step.c_arr[0]; // How many digits to display before and after decimal point.
int ret;
format[2]=before+'0';
format[7]=after+'0';
sprintf(number,format,int(*para->ptr.f_buffer),int((*para->ptr.f_buffer-int(*para->ptr.f_buffer))*pow(10,after)*(*para->ptr.f_buffer>0?1:-1)+0.5)); // Form the string to represent current value. This string will be the default value of the float input.
myTextInput.ptr.msg=number; // Assign the text buffer address
myTextInput.width=before+after+1; // Length of the input panel is 12 characters.
myTextInput.col=para->col; // Display input panel at required column
myTextInput.row=para->row; // Display input panel at required row
switch (para->option)
{
case 0:
myTextInput.low.c='0'; // Text panel valid input starts with character '-'.
myTextInput.high.c='9'; // Text panel valid input ends with character '-'. Could end with '.' if real floating number is enabled.
myTextInput.option=0; // Option 1 incluess 0-9 as valid characters. Option 0, default, option 1 include 0-9 as valid inputs.
break;
case 1:
number[0]='-';
myTextInput.low.c='0'; // Text panel valid input starts with character '-'.
myTextInput.high.c='9'; // Text panel valid input ends with character '-'. Could end with '.' if real floating number is enabled.
myTextInput.option=0; // Option 1 incluess 0-9 as valid characters. Option 0, default, option 1 include 0-9 as valid inputs.
break;
case 2:
myTextInput.low.c='-'; // Text panel valid input starts with character '-'.
myTextInput.high.c='-'; // Text panel valid input ends with character '-'. Could end with '.' if real floating number is enabled.
myTextInput.option=1; // Option 1 incluess 0-9 as valid characters. Option 0, default, option 1 include 0-9 as valid inputs.
break;
default:
break;
}
ret=input_panel(&myTextInput);
if (ret!=-1)
{
sscanf(number,"%d%*c%d",&myTextInput.high.i,&myTextInput.low.i);
*para->ptr.f_buffer=(-1)*(myTextInput.high.i<0?1:-1)*myTextInput.low.i/pow(10.0,(float)after)+myTextInput.high.i;
}
return ret;
}
/**
* \details Sense the keypad and reacts to the button pushes.
* \param para This is the phi_prompt struct to carry information between callers and functions.
*
* This function doesn't print the list so please call render_list before calling this. In fact, please call simple_select_list or select_list instead of using this.
* Function traps for phi_prompt_select_list_update_ms with wait_on_escape call.
* Updates are made through the struct.
* \return Returns buttons pushed so the caller can determine what to do. The function returns 0 if nothing important happens, 1 if enter is pressed, 127 if a refresh is needed such as due to up/down button.
* It returns -1 if the input is cancelled, -2 if left is pressed on a single column list and -3 if right is pressed on a single column list.
*/
int sense_select_list(phi_prompt_struct * para)
{
int temp1;
// byte width=para->width;
temp1=wait_on_escape(phi_prompt_select_list_update_ms);
byte columns=para->step.c_arr[1], rows=para->step.c_arr[0];
switch (temp1) /// The following is the switch-case for key press processing.
{
case NO_KEY: /// If no key is pressed, render the list only when needed, such as when the highlighted item is long and needs scrolling.
return NO_KEY;
break;
case phi_prompt_up: /// Up is pressed. Move to the previous item.
if (para->low.i-1>=0) para->low.i--;
else para->low.i=para->high.i;
break;
case phi_prompt_down: /// Down is pressed. Move to the next item.
if ((para->low.i+1)<=(para->high.i)) para->low.i++;
else para->low.i=0;
break;