-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.c
986 lines (838 loc) · 24.2 KB
/
ui.c
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
//---------------------------------------------------------------------------
// Copyright (C) 2012 Robin Gilks
//
//
// ui.c - User interface - drives the LCD and scans the keyboard for user input
//
// History: 1.0 - First release.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// include files
#include <cfg/debug.h>
#include <cpu/irq.h>
#include <cpu/power.h>
#include <cpu/pgm.h>
#include <avr/eeprom.h>
#include <stdlib.h>
#include <algo/crc8.h>
#include <drv/timer.h>
#include <drv/ser.h>
#include <drv/lcd_hd44.h>
#include <drv/term.h>
#include "features.h"
#if PUSHBUTTONS == 1
#include <drv/kbd.h>
#endif
#include <drv/ow_1wire.h>
#include <drv/ow_ds2438.h>
#include <drv/ow_ds2413.h>
#include <avr/eeprom.h>
#include "control.h"
#include "measure.h"
#include "rpm.h"
#include "tlog.h"
#include "rtc.h"
#include "eeprommap.h"
#include "graph.h"
#include "ui.h"
//static const char lcd_degree[8] = { 0x1c, 0x14, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00 }; /* degree - char set B doesn't have it!! */
static const char lcd_sdcard[8] = { 0x1f, 0x11, 0x11, 0x11, 0x11, 0x11, 0x12, 0x1c }; /* sd card - bent rectangle! */
char degreestr[] = { DEGREE, 'C', 0 };
// a table of fields that are flashing
#define MAXFLASH 10
static int8_t flashing[MAXFLASH];
// Timings (in mS) for various activities
#define CAROSEL 5000L
#define HEADINGS 1500L
#define BACKLIGHT 5000L
#define REFRESH 300L
#define FLASHON 700L
#define FLASHOFF 300L
extern Serial serial;
static Term term;
int16_t gUSdate;
typedef int8_t (*IncFunc_t) (int8_t field, int8_t dirn);
typedef struct vars
{
int16_t *value;
int16_t min;
int16_t max;
int16_t defval;
uint8_t style;
IncFunc_t get_inc; // pointer to field increment function for this field
} Vars;
// forward reference
Vars variables[eNUMVARS];
float gVoffset = 1.03;
ticks_t carosel_timer;
static int8_t
null_inc (int8_t field, int8_t dirn)
{
(void) field;
(void) dirn;
return 0;
}
static int8_t
deca_inc (int8_t field, int8_t dirn)
{
(void) dirn;
(void) field;
return 10;
}
static int8_t
int_inc (int8_t field, int8_t dirn)
{
(void) dirn;
(void) field;
return 1;
}
static int8_t
var_inc (int8_t field, int8_t dirn)
{
(void) dirn;
if (*variables[field].value > 999)
return 20;
if (*variables[field].value > 99)
return 5;
return 1;
}
static int8_t
six_inc (int8_t field, int8_t dirn)
{
(void) dirn;
(void) field;
return 6;
}
static int8_t
cal_inc (int8_t field, int8_t dirn)
{
// only the calibration field uses this so cheat!
// need direction!!
(void) field;
float offset;
offset = 1.0 / (float) gVolts;
switch (dirn)
{
case 1:
gVoffset += offset;
break;
case -1:
gVoffset -= offset;
break;
case 0:
gVoffset = 1.0;
break;
}
return 0;
}
// the way I'm going to display the int16_t value - with decimal places, fixed width etc
enum STYLE
{
eNORMAL,
eDATE,
eLARGE,
eDECIMAL,
eBOOLEAN,
eTRILEAN
};
// value, min, max, default, style, increment function
// Note: Its only worth having (real) limits for those values that can be changed.
Vars variables[eNUMVARS] = {
{NULL, 0, 0, 0, eNORMAL, null_inc}, // dummy 1st entry
{&gVolts , 0, 0, 0, eDECIMAL, null_inc}, // volts
{&gCharge, 0, 0, 0, eNORMAL, null_inc}, // charge
{&gPower, 0, 0, 0, eNORMAL, null_inc}, // power
{&gAmps, 0, 0, 0, eDECIMAL, null_inc}, // amps
{&gDump, 0, 0, 0, eNORMAL, null_inc}, // shunt regulator (dump load)
{&gRPM, 0, 0, 0, eNORMAL, null_inc}, // rpm
{&gMaxhour, 0, 0, 0, eNORMAL, null_inc}, // max hours
{&gMaxday, 0, 0, 0, eNORMAL, null_inc}, // max day
{&gMinhour, 0, 0, 0, eNORMAL, null_inc}, // min hours
{&gMinday, 0, 0, 0, eNORMAL, null_inc}, // min day
{(int16_t *) & gCCA, 0, 65535, 0, eLARGE, null_inc}, // total
{(int16_t *) & gDCA, 0, 65535, 0, eLARGE, null_inc}, // used
{&gTemp, 0, 0, 0, eDECIMAL, null_inc}, // temperature
{&gVlower, 2000, 2400, ddVlower, eDECIMAL, deca_inc}, // volt limit low
{&gVupper, 2600, 3200, ddVupper, eDECIMAL, deca_inc}, // volt limit high
{&gFloatVolts, 2600, 3200, ddFloatVolts, eDECIMAL, deca_inc}, // shunt volt low
{&gAbsorbVolts, 2600, 3200, ddAbsorbVolts, eDECIMAL, deca_inc}, // shunt volt high
{&gBankSize, 1, 9999, ddBankSize, eNORMAL, var_inc}, // charge high
{&gMinCharge, 0, 9999, ddMinCharge, eNORMAL, var_inc}, // charge low
{&gMaxCharge, 1, 9999, ddMaxCharge, eNORMAL, var_inc}, // charge high
{&gMaxDischarge, 1, 99, 0, eNORMAL, var_inc}, // number of discharge cycles
{&gCharge, 0, 9999, ddCharge, eNORMAL, var_inc}, // sync
{&gVoltage, 6, 48, ddVoltage, eNORMAL, six_inc}, // system voltage
{&gVolts, 0, 0, 0, eDECIMAL, cal_inc}, // calibrate
{&gHOUR, 0, 23, ddHOUR, eDATE, int_inc}, // hour
{&gMINUTE, 0, 59, ddMINUTE, eDATE, int_inc}, // minute
{&gSECOND, 0, 59, ddSECOND, eDATE, int_inc}, // second
{&gDAY, 1, 31, ddDAY, eDATE, int_inc}, // day
{&gMONTH, 1, 12, ddMONTH, eDATE, int_inc}, // month
{&gYEAR, 12, 99, ddYEAR, eDATE, int_inc}, // year
{&gInverter, 0, 2, ddInverter, eTRILEAN, int_inc}, // control active
{&gLoad, 0, 1, ddLoad, eBOOLEAN, null_inc}, // manual on/off - don't change value, just toggle!!
{&gRPMMax, 1, 999, ddRPMMax, eNORMAL, int_inc}, // rpm at which to want shutdown
{&gRPMSafe, 1, 999, ddRPMSafe, eNORMAL, int_inc}, // rpm at which to apply short to stop turbine
{&gShunt, 0, 9999, ddShunt, eNORMAL, int_inc}, // shunt conductance in Siemens
{&gPoles, 0, 99, ddPoles, eNORMAL, int_inc}, // magnetic poles in generator
{&gSelfDischarge, 1, 90, ddSelfDischarge, eNORMAL, int_inc}, // battery leakage in days for 1% loss
{&gIdleCurrent, 0, 999, ddIdleCurrent, eDECIMAL, int_inc}, // idle current of controller, router etc
{&gAdjustTime, -719, 719, ddAdjustTime, eNORMAL, int_inc}, // clock adjuster
{&gUSdate, 0, 1, ddUsdate, eBOOLEAN, int_inc}, // date format
};
Vars daymonth[2] = {
{&gDAY, 1, 31, ddDAY, eDATE, int_inc}, // day
{&gMONTH, 1, 12, ddMONTH, eDATE, int_inc}, // month
};
typedef struct screen
{
int8_t field; // global field number (relevant across all screens).
// -1 = no value (text only)
// -2 = end of array of structs
int8_t row; // row of where to start text
int8_t col; // column of where to start text
const char *text; // the text!!
int8_t vcol; // the column of where to display the value
int8_t width; // width of the field
} Screen;
Screen screen1[] = {
{eVOLTS, 0, 0, "Volts ", 8, 6},
{eCHARGE, 1, 0, "Charge amp-hrs", 8, 4},
{ePOWER, 2, 0, "Power watts", 8, 5},
{eAMPS, 3, 0, "Current amps", 8, 6},
{-2, 0, 0, "", 0, 0}
};
Screen screen2[] = {
{eDUMP, 0, 0, "Dump %", 8, 3},
{eRPM, 1, 0, "Speed RPM", 8, 3},
{eTEMPERATURE, 2, 0, "Temp", 8, 6},
{-1, 2, 15, degreestr, 0, 0},
{eHOUR, 3, 0, " :", 0, 2},
{eMINUTE, 3, 3, " :", 3, 2},
{eSECOND, 3, 6, "", 6, 2},
{eDAY, 3, 11, " -", 11, 2},
{eMONTH, 3, 14, " -", 14, 2},
{eYEAR, 3, 17, "", 17, 2},
{-2, 0, 0, "", 0, 0}
};
Screen screen3[] = {
{-1, 0, 3, "Max/Min/Total", 0, 0},
{eMAXHOUR, 1, 0, "Hr ", 4, 5},
{eMAXDAY, 1, 10, "Day ", 14, 5},
{eMINHOUR, 2, 0, "Hr ", 4, 5},
{eMINDAY, 2, 10, "Day ", 14, 5},
{eTOTAL, 3, 0, "In ", 4, 5},
{eUSED, 3, 10, "Out ", 14, 5},
{-2, 0, 0, "", 0, 0}
};
Screen system[] = {
{-1, 0, 3, "System " VERSION, 0, 0},
{eSYSTEM_VOLTS, 1, 0, "Voltage ", 10, 5},
{eCAL_VOLTS, 2, 0, "Calibrate ", 10, 6},
{eHOUR, 3, 0, " :", 0, 2},
{eMINUTE, 3, 3, " :", 3, 2},
{eSECOND, 3, 6, "", 6, 2},
{eDAY, 3, 11, " -", 11, 2},
{eMONTH, 3, 14, " -", 14, 2},
{eYEAR, 3, 17, "", 17, 2},
{-2, 0, 0, "", 0, 0}
};
Screen setup1[] = {
{-1, 0, 3, "Voltage", 0, 0},
{eVOLT_LIMIT_LO, 1, 0, "Min", 4, 5},
{eVOLT_LIMIT_HI, 1, 10, "Max", 14, 5},
{eVOLT_FLOAT, 2, 0, "Float", 10, 5},
{eVOLT_ABSORB, 3, 0, "Absorb", 10, 5},
{-2, 0, 0, "", 0, 0}
};
Screen setup2[] = {
{-1, 0, 3, "Battery", 0, 0},
{eBANK_SIZE, 1, 0, "Bank Size", 12, 4},
{eMIN_CHARGE, 2, 0, "Min", 5, 4},
{eMAX_CHARGE, 2, 10, "Max", 15, 4},
{eDISCHARGE, 3, 0, "Cycle", 7, 2},
{eSYNC, 3, 10, "Sync", 15, 4},
{-2, 0, 0, "", 0, 0}
};
Screen setup3[] = {
{-1, 0, 3, "Miscellaneous", 0, 0},
{eSHUNT, 1, 0, "Shunt", 6, 4},
{ePOLES, 1, 11, "Poles", 17, 2},
{eSELFDISCHARGE, 2, 0, "Leak", 6, 3},
{eIDLE_CURRENT, 2, 10, "Idle", 15, 5},
{eADJUSTTIME, 3, 0, "Time", 6, 4},
{eUSDATE, 3, 10, "Date", 15, 3},
{-2, 0, 0, "", 0, 0}
};
Screen control[] = {
{-1, 0, 3, "Control", 0, 0},
{eINVERTER, 1, 0, "Inverter", 14, 4},
{eMANUAL, 2, 0, "Override", 14, 4},
{eRPMMAX, 3, 0, "RPMMax", 7, 3},
{eRPMSAFE, 3, 11, "Safe", 16, 3},
{-2, 0, 0, "", 0, 0}
};
#define NUM_INFO 3
#define NUM_SETUPS 5
#define MAXSCREENS NUM_INFO + NUM_SETUPS
static Screen *screen_list[] = { screen1, screen2, screen3, system, setup1, setup2, setup3, control };
static void set_month_day(uint8_t us)
{
if (us)
{
variables[eDAY] = daymonth[1];
variables[eMONTH] = daymonth[0];
}
// if already in US Date mode but want EURO mode then swap back
else
{
variables[eDAY] = daymonth[0];
variables[eMONTH] = daymonth[1];
}
}
void get_month_day (uint8_t *month, uint8_t *day)
{
*month = (uint8_t)*variables[eMONTH].value;
*day = (uint8_t)*variables[eDAY].value;
}
void set_flash(int8_t field, int8_t set)
{
int8_t i;
for (i = 0; i < MAXFLASH; i++)
{
if (set)
{
// find a free slot or already set then set it
if ((flashing[i] == 0) || (flashing[i] == field))
{
flashing[i] = field;
break;
}
}
else
{
// find which slot its in and clear it
if (flashing[i] == field)
{
flashing[i] = 0;
break;
}
}
}
}
// return an indicator on whether this field is flashing and should currently be blanked (true) or displayed (false)
static int8_t check_flash(int8_t field)
{
int8_t i;
static int8_t flash_state = true;
static ticks_t flash_on_timer, flash_off_timer;
// always toggle flash_state with the correct cadence then see if we need it!!
if (flash_state) // currently on, see if on time has expired
{
if (timer_clock () - flash_off_timer > ms_to_ticks (FLASHOFF))
{
// timer expired, set off timer and turn off (blank) field
flash_on_timer = timer_clock ();
flash_state = false; // signify its now displayed
}
}
else // currently on, turn it off
{
if (timer_clock () - flash_on_timer > ms_to_ticks (FLASHON))
{
flash_off_timer = timer_clock ();
flash_state = true; // signify its now blanked out
}
}
for (i = 0; i < MAXFLASH; i++)
{
if (flashing[i] == field)
return flash_state;
}
return false;
}
// display a variable or blanks of the correct length at the coordinates for this field in this screen
static void print_field(int16_t value, int8_t field, uint8_t screen)
{
int8_t i;
int16_t whole, part;
char spaces[10] = " ";
char tritext[4][5] = {"off", "on", "auto", "oops" };
Screen *scrn = screen_list[screen];
for (i = 0; scrn[i].field != -2; i++)
{
if (scrn[i].field == field) // found the correct one
{
kfile_printf (&term.fd, "%c%c%c", TERM_CPC, TERM_ROW + scrn[i].row, TERM_COL + scrn[i].vcol);
kfile_printf (&term.fd, "%.*s", scrn[i].width, spaces);
if (check_flash (field))
break;
kfile_printf (&term.fd, "%c%c%c", TERM_CPC, TERM_ROW + scrn[i].row, TERM_COL + scrn[i].vcol);
switch (variables[field].style)
{
case eNORMAL:
kfile_printf (&term.fd, "%d", value);
break;
case eDATE:
kfile_printf (&term.fd, "%02d", value);
break;
case eLARGE:
kfile_printf (&term.fd, "%u", (uint16_t) value);
break;
case eDECIMAL:
// split the value into those bits before and after the decimal point
// if the whole part is less than 1 then we loose the sign bit so do it manually in all cases
whole = abs (value / 100);
part = abs (value % 100);
kfile_printf (&term.fd, "%.*s%d.%02u", value < 0 ? 1 : 0, "-", whole, part);
break;
case eBOOLEAN:
kfile_printf (&term.fd, "%s", tritext[value & 1]);
break;
case eTRILEAN:
kfile_printf (&term.fd, "%s", tritext[value & 3]);
break;
}
break;
}
}
}
// scan screen to determine the min and max field numbers
// direction can be -1 for up, 1 for down or 0 for current field
// min & max handle the wrap round
// return the new field
// assumes fields are contiguous on a screen
static int8_t find_next_field (int8_t field, int8_t screen, int8_t dirn)
{
int8_t i, min = 99, max = -1;
Screen *scrn = screen_list[screen];
for (i = 0; scrn[i].field != -2; i++)
{
if (scrn[i].field < 0)
continue;
if (scrn[i].field > max)
max = scrn[i].field;
if (scrn[i].field < min)
min = scrn[i].field;
}
field += dirn;
if (field > max)
field = min;
if (field < min)
field = max;
return field;
}
// find out what line this field is on
static int8_t get_line(int8_t field, int8_t screen)
{
Screen *scrn = screen_list[screen];
int8_t i;
// get the line this field is on
for (i = 0; scrn[i].field != -2; i++)
{
if (scrn[i].field < 0)
continue;
if (field == scrn[i].field)
{
return scrn[i].row;
}
}
// if field not found then return something odd!!
return -1;
}
// find the next line by scanning fields in the direction requested
static int8_t find_next_line (int8_t field, int8_t screen, int8_t dirn)
{
int8_t startline, line = 0;
int8_t startfield = field;
startline = get_line(field, screen);
while (1)
{
// move fields in the direction specified
field = find_next_field (field, screen, dirn);
line = get_line(field, screen);
// moved to another line
if (line != startline)
break;
// if only 1 line then wrapped back to where we started
if (field == startfield)
break;
}
return field;
}
// display the text and optional field for all lines on a screen
static void print_screen (int8_t screen)
{
int8_t i = 0;
static int8_t last_screen = -1;
Screen *scrn = screen_list[screen];
char tmp[4];
if (screen != last_screen)
{
kfile_printf (&term.fd, "%c", TERM_CLR);
last_screen = screen;
}
while (scrn[i].field != -2)
{
kfile_printf (&term.fd, "%c%c%c", TERM_CPC, TERM_ROW + scrn[i].row, TERM_COL + scrn[i].col);
kfile_printf (&term.fd, "%s", scrn[i].text);
if (scrn[i].field != -1)
{
print_field (*variables[scrn[i].field].value, scrn[i].field, screen);
}
i++;
}
// indicate there is an sd card plugged in (or not!!), charge mode and if inverter on
tmp[0] = gLoad ? 'I' : 0x20;
tmp[1] = charge_mode;
tmp[2] = sd_ok ? SDCARD : 0x20;
kfile_printf (&term.fd, "%c%c%c", TERM_CPC, TERM_ROW + 0, TERM_COL + 17);
for (i = 0; i < 3; i++)
kfile_putc (tmp[i], &term.fd); // user defined character is a null so can't put into a string!!
}
// scan through a few variables and check their limits to see if they should be flashing
static void flag_warnings(void)
{
if ((gVolts < gVlower) || (gVolts > gAbsorbVolts))
set_flash(eVOLTS, true);
else
set_flash(eVOLTS, false);
if (gCharge < gMinCharge)
set_flash(eCHARGE, true);
else
set_flash(eCHARGE, false);
}
// used externally to verify that a value is within the correct range and set it if so.
bool check_value(enum VARS var, int16_t value)
{
if ((value >= variables[var].min) && (value <= variables[var].max))
{
*variables[var].value = value;
return false;
}
else
return true;
}
// initialise the module!
void ui_init (void)
{
lcd_init ();
lcd_display (1, 0, 0);
// lcd_remapChar (lcd_degree, DEGREE); // put the degree symbol on character 0x01
lcd_remapChar (lcd_sdcard, SDCARD); // put the sd card symbol on character 0x02
term_init (&term);
#if PUSHBUTTONS == 1
kbd_init();
kbd_setRepeatMask(K_UP | K_DOWN);
#endif
carosel_timer = timer_clock ();
set_month_day(gUSdate);
}
// mode values
#define SETUP 1
#define MONITOR 2
#define PAGEEDIT 3
#define FIELDEDIT 4
#define GRAPH 5
void run_ui (void)
{
static int8_t screen_number = 0, field = 0, mode = MONITOR, graph_number = MINGRAPH;
static ticks_t backlight_timer, refresh_timer;
static int16_t working_value;
flag_warnings();
#if PUSHBUTTONS == 1
keymask_t key;
key = kbd_peek();
#else
int16_t key;
#define K_UP 'u'
#define K_DOWN 'd'
#define K_LEFT 'l'
#define K_RIGHT 'r'
#define K_CENTRE 'c'
key = kfile_getc (&serial.fd);
if (key == EOF)
key = 0;
#endif
// if key pressed then ignite backlight for a short while
if (key)
{
lcd_backlight(1);
backlight_timer = timer_clock ();
key &= K_CENTRE | K_RIGHT | K_LEFT | K_UP | K_DOWN | K_LONG;
}
else
{
if (timer_clock () - backlight_timer > ms_to_ticks (BACKLIGHT))
{
lcd_backlight(0);
}
}
if (timer_clock () - refresh_timer > ms_to_ticks (REFRESH))
{
refresh_timer = timer_clock ();
if (mode == GRAPH)
print_graph(&term.fd, graph_number, GRAPHSTYLE);
else
print_screen (screen_number);
}
switch (mode)
{
case FIELDEDIT:
// the working value during calibration needs to be updated from the real value
if (field == eCAL_VOLTS)
working_value = gVolts;
// refresh the value to place the cursor on the screen in the right place
print_field (working_value, field, screen_number);
switch (key)
{
int16_t inc;
case K_CENTRE:
// save value and exit field edit mode
// save the working value into the real one
*variables[field].value = working_value;
// some fields require special action
switch (field)
{
case eCAL_VOLTS:
// turn off backlight to reduce current before calibration
lcd_backlight(0);
do_calibration ();
break;
case eSYNC:
set_charge (working_value);
break;
case eHOUR:
case eMINUTE:
case eSECOND:
case eDAY:
case eMONTH:
case eYEAR:
case eADJUSTTIME:
// set Unix time in seconds, save adjustment in eeprom
set_epoch_time ();
break;
case eUSDATE:
set_month_day(gUSdate);
break;
case eMANUAL:
if (gLoad == LOADOFF)
do_command (MANUALON);
else
do_command (MANUALOFF);
break;
}
save_eeprom_values();
mode = PAGEEDIT;
set_flash(field, false);
break;
case K_UP:
/// increase by increment
inc = variables[field].get_inc (field, 1);
if (working_value + inc <= variables[field].max)
working_value += inc;
else // wrap
working_value = variables[field].min;
break;
case K_DOWN:
// decrease by increment
inc = variables[field].get_inc (field, -1);
if (working_value - inc >= variables[field].min)
working_value -= inc;
else // wrap
working_value = variables[field].max;
break;
case K_LEFT:
mode = PAGEEDIT;
// abort - reload previous values
load_eeprom_values();
set_flash(field, false); // make sure flash is off
break;
case K_RIGHT:
// inc of zero special case for voltage calibration to set default
variables[field].get_inc (field, 0);
// load default
working_value = variables[field].defval;
break;
}
break;
// centre enters field edit mode
// left/right together exits field navigation and moves round setup screens
case PAGEEDIT:
// refresh the value to place the cursor on the screen in the right place
print_field (*variables[field].value, field, screen_number);
switch (key)
{
case K_CENTRE:
// enter this field to change it
mode = FIELDEDIT;
set_flash(field, true);
// refresh the value
working_value = *variables[field].value;
break;
case K_CENTRE | K_LONG:
// exit edit mode
// turn off cursor
kfile_printf (&term.fd, "%c", TERM_BLINK_OFF);
mode = SETUP;
break;
case K_UP:
// field on previous line
field = find_next_line (field, screen_number, -1);
break;
case K_DOWN:
// field on next line
field = find_next_line (field, screen_number, 1);
break;
case K_LEFT:
// previous field
field = find_next_field (field, screen_number, -1);
break;
case K_RIGHT:
// next field
field = find_next_field (field, screen_number, 1);
break;
}
break;
// up/down moves round monitor screens
// left right moves round setup screens
// when in setup screen then centre enters field navigation mode
case SETUP:
switch (key)
{
case K_CENTRE:
// enter edit mode
mode = PAGEEDIT;
// turn on cursor
kfile_printf (&term.fd, " %c", TERM_BLINK_ON);
// get the first field on this screen and display it
field = find_next_field (0, screen_number, 0);
print_field(*variables[field].value, field, screen_number);
break;
case K_RIGHT:
screen_number++;
if (screen_number >= MAXSCREENS)
screen_number = NUM_INFO;
print_screen (screen_number);
break;
case K_LEFT:
screen_number--;
if (screen_number < NUM_INFO)
screen_number = MAXSCREENS - 1;
print_screen (screen_number);
break;
case K_UP:
mode = MONITOR;
screen_number = NUM_INFO - 1;
print_screen (screen_number);
break;
case K_DOWN:
mode = MONITOR;
screen_number = 0;
print_screen (screen_number);
break;
}
break;
// up/down moves round monitor screens
// left right moves round setup screens
// when in monitor screen then centre toggles carosel mode
case MONITOR:
switch (key)
{
case K_CENTRE:
if (carosel_timer == 0)
carosel_timer = timer_clock ();
else
carosel_timer = 0;
screen_number = 0;
print_screen (screen_number);
break;
case K_UP:
screen_number = (screen_number + 1) % NUM_INFO;
print_screen (screen_number);
break;
case K_DOWN:
screen_number = (screen_number - 1 + NUM_INFO) % NUM_INFO;
print_screen (screen_number);
break;
case K_LEFT:
mode = SETUP;
screen_number = MAXSCREENS - 1;
print_screen (screen_number);
break;
case K_RIGHT:
mode = SETUP;
screen_number = NUM_INFO;
print_screen (screen_number);
break;
case K_CENTRE | K_LONG:
// enter graph mode
mode = GRAPH;
graph_number = MINGRAPH;
refresh_timer = timer_clock () + ms_to_ticks (HEADINGS);
print_graph (&term.fd, graph_number, TEXTSTYLE);
break;
}
if (carosel_timer && timer_clock () - carosel_timer > ms_to_ticks (CAROSEL))
{
carosel_timer = timer_clock ();
refresh_timer = timer_clock ();
if (++screen_number >= (int8_t) NUM_INFO)
screen_number = 0;
print_screen (screen_number);
}
break;
// up/down moves round graph screens
// left/right displays the scale and time, resets the refresh timer so the graph is displayed after a short delay
// centre toggles carosel mode
case GRAPH:
switch (key)
{
case K_CENTRE:
graph_number = MINGRAPH;
if (carosel_timer == 0)
carosel_timer = timer_clock ();
else
carosel_timer = 0;
break;
case K_UP:
graph_number = (graph_number + 1) % NUMGRAPH;
break;
case K_DOWN:
graph_number = (graph_number - 1 + NUMGRAPH) % NUMGRAPH;
break;
case K_CENTRE | K_LONG:
// enter text monitor mode
mode = MONITOR;
// force immediate display
refresh_timer = timer_clock () - ms_to_ticks (REFRESH);
key = 0;
break;
}
if (key)
{
refresh_timer = timer_clock () + ms_to_ticks (HEADINGS);
print_graph (&term.fd, graph_number, TEXTSTYLE);
}
if (carosel_timer && timer_clock () - carosel_timer > ms_to_ticks (CAROSEL))
{
carosel_timer = timer_clock ();
refresh_timer = timer_clock () + ms_to_ticks (HEADINGS);
if (++graph_number >= (int8_t) NUMGRAPH)
graph_number = MINGRAPH;
print_graph (&term.fd, graph_number, TEXTSTYLE);
}
break;
}
}