forked from Ho-Ro/ComponentTester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.c
2556 lines (2128 loc) · 62 KB
/
user.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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ************************************************************************
*
* user interface functions
*
* (c) 2012-2021 by Markus Reschke
*
* ************************************************************************ */
/*
* local constants
*/
/* source management */
#define USER_C
/*
* include header files
*/
/* local includes */
#include "config.h" /* global configuration */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
#include "colors.h" /* color definitions */
/*
* local constants
*/
/* rotary encoder */
#define DIR_NONE 0b00000000 /* no turn or error */
#define DIR_RESET 0b00000001 /* reset state */
/* ************************************************************************
* values and scales
* ************************************************************************ */
/*
* get number of digits of a value
*/
uint8_t NumberOfDigits(uint32_t Value)
{
uint8_t Counter = 1; /* return value */
while (Value >= 10)
{
Value /= 10;
Counter++;
}
return Counter;
}
/*
* compare two scaled values
*
* requires:
* - first pair of value and scale (*10^x)
* - second pair of value and scale (*10^x)
*
* returns:
* - -1 if first value is smaller than seconds one
* - 0 if equal
* - 1 if first value is larger than second one
*/
int8_t CmpValue(uint32_t Value1, int8_t Scale1, uint32_t Value2, int8_t Scale2)
{
int8_t Flag; /* return value */
int8_t Len1, Len2; /* length */
/* determine virtual length */
Len1 = NumberOfDigits(Value1) + Scale1;
Len2 = NumberOfDigits(Value2) + Scale2;
if ((Value1 == 0) || (Value2 == 0)) /* special case */
{
Flag = 10; /* perform direct comparison */
}
else if (Len1 > Len2) /* more digits -> larger */
{
Flag = 1;
}
else if (Len1 == Len2) /* same length */
{
/* re-scale to longer value */
Len1 -= Scale1;
Len2 -= Scale2;
while (Len1 > Len2) /* up-scale Value #2 */
{
Value2 *= 10;
Len2++;
/* Scale2-- */
}
while (Len2 > Len1) /* up-scale Value #1 */
{
Value1 *= 10;
Len1++;
/* Scale1-- */
}
Flag = 10; /* perform direct comparison */
}
else /* less digits -> smaller */
{
Flag = -1;
}
if (Flag == 10) /* perform direct comparison */
{
if (Value1 > Value2) Flag = 1;
else if (Value1 < Value2) Flag = -1;
else Flag = 0;
}
return Flag;
}
/*
* rescale value
*
* requires:
* - value and scale (*10^x)
* - new scale (*10^x)
*/
uint32_t RescaleValue(uint32_t Value, int8_t Scale, int8_t NewScale)
{
uint32_t NewValue; /* return value */
NewValue = Value; /* take old value */
while (Scale != NewScale) /* processing loop */
{
if (NewScale > Scale) /* upscale */
{
NewValue /= 10;
Scale++;
}
else /* downscale */
{
NewValue *= 10;
Scale--;
}
}
return NewValue;
}
#ifdef UI_ROUND_DS18B20
/*
* round value while also scaling
*
* requires:
* - Value: value
* - Scale: number of decimal places (0 = none)
* - RoundScale: number of decimal places to round to
*
* returns:
* - rounded value
*/
int32_t RoundSignedValue(int32_t Value, uint8_t Scale, uint8_t RoundScale)
{
int8_t Offset = 5; /* rounding offset */
while (Scale < RoundScale) /* not enough decimal digits */
{
Value *= 10; /* scale by 0.1 */
Scale++; /* one decimal digit more */
}
if (Value < 0) /* negative value */
{
Offset = -5; /* negative rounding offset */
}
while (Scale > RoundScale) /* rounding loop */
{
Value += Offset; /* for rounding */
Value /= 10; /* /10 */
Scale--; /* one digit less */
}
return Value;
}
#endif
/* ************************************************************************
* conversion functions
* ************************************************************************ */
#ifdef FUNC_CELSIUS2FAHRENHEIT
/*
* convert temperature value from Celcius to Fahrenheit
*
* requires:
* - Value: temperature in °C
* - Scale: number of decimal places (0 = none)
*
* returns:
* - temperature in °F
*/
int32_t Celsius2Fahrenheit(int32_t Value, uint8_t Scale)
{
int32_t Offset = 32; /* offset */
/*
* convert °C to °F
* - T[°F] = T[°C] * 9/5 + 32
*/
/* scale offset to match temperature's scale */
while (Scale > 0)
{
Offset *= 10; /* scale by 10^1 */
Scale--; /* next digit */
}
/* convert into Fahrenheit */
Value *= 9;
Value /= 5;
Value += Offset; /* add scaled offset */
return Value;
}
#endif
/* ************************************************************************
* string functions
* ************************************************************************ */
#ifdef UI_KEY_HINTS
/*
* get length of a fixed string stored in EEPROM/Flash
*
* requires:
* - pointer to fixed string
*/
uint8_t EEStringLength(const unsigned char *String)
{
uint8_t Length = 0; /* return value */
unsigned char Char; /* character */
/* read characters until we get the terminating 0 */
while ((Char = DATA_read_byte(String)))
{
Length++; /* got another character */
String++; /* next one */
}
return Length;
}
#endif
/* ************************************************************************
* user input
* - test push button
* - rotary encoder
* - increase/decrease push buttons
* - touch screen
* ************************************************************************ */
#ifdef HW_ENCODER
/*
* read rotary encoder
* - adds delay of 0.5ms
* - A & B have pull-up resistors
* - encoder might be in parallel with LCD signal lines
*
* returns user action:
* - KEY_NONE for no turn or invalid signal
* - KEY_RIGHT for right/clockwise turn
* - KEY_LEFT for left/counter-clockwise turn
*/
uint8_t ReadEncoder(void)
{
uint8_t Key = KEY_NONE; /* return value */
uint8_t Temp; /* temporary value */
uint8_t AB = 0; /* new AB state */
uint8_t Old_AB; /* old AB state */
/* set encoder's A & B pins to input */
Old_AB = ENCODER_DDR; /* save current settings */
ENCODER_DDR &= ~((1 << ENCODER_A) | (1 << ENCODER_B));
wait500us(); /* settle time */
/* get A & B signals (1 = open / 0 = closed) */
Temp = ENCODER_PIN;
if (Temp & (1 << ENCODER_A)) AB = 0b00000010;
if (Temp & (1 << ENCODER_B)) AB |= 0b00000001;
/* restore port/pin settings */
ENCODER_DDR = Old_AB; /* restore old settings */
/* update state history */
if (UI.EncDir == DIR_RESET) /* first scan */
{
UI.EncState = AB; /* set as last state */
UI.EncDir = DIR_NONE; /* reset direction */
UI.EncTicks = 0; /* reset time counter */
}
/* time counter */
if (UI.EncTicks > 0) /* after first pulse */
{
if (UI.EncTicks < 250) /* prevent overflow */
{
UI.EncTicks++; /* increase counter */
}
}
Old_AB = UI.EncState; /* save last state */
UI.EncState = AB; /* and save new state */
/* process signals */
if (Old_AB != AB) /* signals changed */
{
/* check if only one bit has changed (Gray code) */
Temp = AB ^ Old_AB; /* get bit difference */
if (!(Temp & 0b00000001)) /* B hasn't changed */
{
Temp >>= 1; /* shift right to get A */
}
if (Temp == 1) /* valid change */
{
/*
* detect direction
* - check sequence: next AB for AB=11, AB=10, AB=01, AB=00
* - right turn (CW):
* Gray code for AB: 00-10-11-01 -> check sequence 01110010
* - left turn (CCW):
* Gray code for AB: 00-01-11-10 -> check sequence 10001101
*/
Temp = 0b01110010; /* expected values for a right turn */
Temp >>= (Old_AB * 2); /* get expected value by shifting */
Temp &= 0b00000011; /* select value */
if (Temp == AB) /* value matches */
Temp = KEY_RIGHT; /* turn to the right */
else /* value mismatches */
Temp = KEY_LEFT; /* turn to the left */
/* step/detent logic */
UI.EncPulses++; /* got a new pulse */
if (Temp != UI.EncDir) /* direction has changed */
{
UI.EncPulses = 1; /* first pulse for new direction */
UI.EncTicks = 1; /* enable time counter */
}
UI.EncDir = Temp; /* update direction */
if (UI.EncPulses >= ENCODER_PULSES) /* reached step */
{
UI.EncPulses = 0; /* reset pulses */
Key = Temp; /* signal valid step */
}
}
else /* invalid change */
{
UI.EncDir = DIR_RESET; /* reset direction state */
}
}
return Key;
}
#endif
#ifdef HW_INCDEC_KEYS
/*
* check increase/decrease push buttons
* - adds delay of 0.5ms
* - buttons might be in parallel with LCD signal lines
* - speed-up functionality (similar to rotary encoder's turning velocity)
*
* returns:
* - KEY_NONE for no button press
* - KEY_RIGHT for increase
* - KEY_LEFT for decrease
* - KEY_INCDEC for increase and decrease (both at the same time)
*/
uint8_t ReadIncDecKeys(void)
{
uint8_t Key = KEY_NONE; /* return value */
uint8_t RegState; /* register state */
uint8_t Run = 1; /* loop control */
uint8_t Temp; /* temporary value */
uint8_t TicksInc = 0; /* time counter */
uint8_t TicksDec = 0; /* time counter */
/* set pins to input */
RegState = KEY_DDR; /* save current settings */
KEY_DDR &= ~((1 << KEY_INC) | (1 << KEY_DEC));
wait500us(); /* settle time */
/* processing loop */
while (Run == 1)
{
/* check push button state (low active) */
Temp = KEY_PIN; /* get current state */
Temp = ~Temp; /* invert bits */
Temp &= (1 << KEY_INC) | (1 << KEY_DEC); /* filter buttons */
if (Temp == 0) /* not button pressed */
{
Run = 0; /* end loop */
}
else /* button pressed */
{
if (Temp & (1 << KEY_INC)) /* "increase" button */
{
TicksInc++; /* increase counter */
}
if (Temp & (1 << KEY_DEC)) /* "decrease" button */
{
TicksDec++; /* increase counter */
}
Temp = TicksInc + TicksDec;
if (Temp >= 10) /* long key press (300ms) */
{
Run = 2; /* end loop & signal long key press */
}
else
{
MilliSleep(30); /* time to debounce and delay */
}
}
}
/* process counters */
if (TicksInc > 0) /* "increase" button pressed */
{
Key = KEY_RIGHT;
}
if (TicksDec > 0) /* "decrease" button pressed */
{
if (Key == KEY_RIGHT) /* also "increase" button */
{
Key = KEY_INCDEC; /* both buttons pressed */
}
else /* just "decrease */
{
Key = KEY_LEFT;
}
}
/* speed-up functionality */
if (Key != KEY_NONE) /* button pressed */
{
Temp = 1; /* default step size */
if (Key == UI.KeyOld) /* same key as before */
{
if (Run == 2) /* long button press */
{
Temp = UI.KeyStepOld; /* get former step size */
if (Temp <= 6) /* limit step size to 7 */
{
Temp++; /* increase step size */
}
}
}
UI.KeyStepOld = Temp; /* update former step size */
UI.KeyStep = Temp; /* set new step size */
}
/* restore port/pin settings */
KEY_DDR = RegState; /* restore old settings */
return Key;
}
#endif
#ifdef HW_TOUCH
/*
* check touch screen
*
* requires:
* - Mode (bitfield):
* CHECK_KEY_TWICE check for two short test key presses
* any other flags ignore
*
* returns:
* - KEY_NONE for no touch event
* - KEY_SHORT for short press of center area
* - KEY_LONG for long press of center area
* - KEY_RIGHT for increase (right & bottom bar)
* - KEY_LEFT for decrease (left & top bar)
*/
uint8_t ReadTouchScreen(uint8_t Mode)
{
uint8_t Key = KEY_NONE; /* return value */
uint8_t OldKey = KEY_NONE; /* former key action */
uint8_t Run = 1; /* loop control */
uint8_t Flag; /* control flag */
uint8_t n = 0; /* counter */
uint8_t X, Y; /* char pos */
uint8_t X_Max, Y_Max; /* char pos limits */
X_Max = UI.CharMax_X; /* get limits */
Y_Max = UI.CharMax_Y;
/* processing loop */
while (Run == 1)
{
Flag = Touch_Check(); /* check touch controller */
if (Flag) /* touch event */
{
X = UI.TouchPos_X; /* get char pos */
Y = UI.TouchPos_Y;
/* derive "button" */
if (X <= 3) /* left touch bar (3 columns) */
{
Key = KEY_LEFT;
}
else if (X >= (X_Max - 2)) /* right touch bar (3 columns) */
{
Key = KEY_RIGHT;
}
else if (Y <= 2) /* top touch bar (2 rows) */
{
Key = KEY_LEFT;
}
else if (Y >= (Y_Max - 1)) /* bottom touch bar (2 rows) */
{
Key = KEY_RIGHT;
}
else /* center area */
{
Key = KEY_SHORT;
}
/* control logic */
if (Key == KEY_NONE) /* no valid Key */
{
Run = 0; /* end loop */
}
else /* valid key */
{
if (OldKey == KEY_NONE) /* first run */
{
OldKey = Key; /* set former key */
}
if (OldKey != Key) /* key has changed */
{
Key = KEY_NONE; /* reset key */
Run = 0; /* end loop */
}
}
n++; /* increase counter */
if (n >= 10) /* long "button" press (300ms) */
{
Run = 2; /* end loop & signal long press */
}
else /* less than 300ms */
{
MilliSleep(30); /* time to debounce and delay */
}
}
else /* no touch event */
{
Run = 0; /* end loop */
}
}
/* speed-up functionality */
if (Key != KEY_NONE) /* "button" pressed */
{
n = 1; /* default step size */
if (Key == KEY_SHORT) /* special case: center area */
{
if (Run == 2) /* long key press */
{
Key = KEY_LONG; /* change to long key press */
}
}
else if (Key == UI.KeyOld) /* same key as before */
{
if (Run == 2) /* long button press */
{
n = UI.KeyStepOld; /* get former step size */
if (n <= 6) /* limit step size to 7 */
{
n++; /* increase step size */
}
}
}
UI.KeyStepOld = n; /* update former step size */
UI.KeyStep = n; /* set new step size */
}
/* check for second key press (center area) if requested */
if ((Mode & CHECK_KEY_TWICE) && (Key == KEY_SHORT))
{
MilliSleep(30); /* delay for checking key again */
Run = 20; /* timeout of 200ms */
while (Run > 0) /* timeout loop */
{
Flag = Touch_Check(); /* check touch controller */
if (Flag) /* touch event */
{
X = UI.TouchPos_X; /* get char pos */
Y = UI.TouchPos_Y;
/* check for center area */
if ((X > 3) && (X < (X_Max - 2)) &&
(Y > 2) && (Y < (Y_Max - 1)))
{
Key = KEY_TWICE; /* signal two key presses */
MilliSleep(200); /* smooth UI */
}
/* either way (center area or some other bar) */
Run = 0; /* end loop */
}
else
{
Run--; /* decrease timeout */
MilliSleep(10); /* wait 10ms */
}
}
}
return Key;
}
#endif
/*
* get user feedback
* - test push button
* - optional rotary encoder incl. detection of turning velocity
* - optional increase/decrease push buttons
* - optional touch screen
* - processing can be terminated by signaling OP_BREAK_KEY via Cfg.OP_Control
* (e.g. set by an interrupt handler to deal with an infinite timeout)
* - optional auto-power-off for auto-hold mode (signaled by OP_PWR_TIMEOUT
* via Cfg.OP_Control)
*
* requires:
* - Timeout in ms (0 - 65535)
* 0 = no timeout, wait for key press or any other feedback
* - Mode (bitfield):
* CURSOR_NONE no cursor
* CURSOR_STEADY steady cursor
* CURSOR_BLINK blinking cursor
* CHECK_OP_MODE consider tester operation mode (Cfg.OP_Mode)
* CHECK_KEY_TWICE check for two short test key presses
* CHECK_BAT check battery (and power off on low battery)
* CURSOR_TEXT show text instead of cursor (UI.KeyHint)
*
* returns:
* - KEY_TIMEOUT reached timeout (no key press)
* - KEY_SHORT short press of test key
* - KEY_LONG long press of test key
* - KEY_TWICE two short presses of test key
* - KEY_RIGHT right key (e.g. rotary encoder turned right)
* - KEY_LEFT left key (e.g. rotary encoder turned left)
* - KEY_INCDEC increase and decrease keys both pressed
* - KEY_POWER_OFF power off requested
* The turning velocity (speed-up) is returned via UI.KeyStep (1-7).
*/
uint8_t TestKey(uint16_t Timeout, uint8_t Mode)
{
uint8_t Key = KEY_TIMEOUT; /* return value */
uint8_t Run = 1; /* loop control */
uint8_t Ticks = 0; /* time counter */
uint8_t Test; /* temp. value */
#ifdef HW_ENCODER
uint8_t Timeout2; /* step timeout */
uint8_t Steps = 0; /* step counter */
uint8_t MinSteps = 2; /* required steps */
uint16_t Temp; /* temp value */
#endif
#ifdef POWER_OFF_TIMEOUT
uint16_t PwrTimeout = 0; /* timeout for auto power-off (auto-hold mode) */
#endif
#ifdef UI_KEY_HINTS
uint8_t Pos = 0; /* X position for text */
#endif
#ifdef UI_COLORED_CURSOR
uint16_t Color; /* pen color */
#endif
/*
* init
*/
#ifdef HW_ENCODER
/* rotary encoder: sample each 2.5ms (1ms would be ideal) */
#define DELAY_TICK 2 /* 2ms + 0.5ms for ReadEncoder() */
#define DELAY_500 200 /* ticks for 500ms */
#define DELAY_100 40 /* ticks for 100ms */
#else
/* just the test key */
#define DELAY_TICK 5 /* 5ms */
#define DELAY_500 100 /* ticks for 500ms */
#define DELAY_100 20 /* ticks for 100ms */
#endif
#ifdef HW_ENCODER
/* init variables for rotary encoder */
UI.EncDir = DIR_RESET; /* resets also UI.EncState and .EncTicks */
UI.EncPulses = 0;
Timeout2 = 50;
#endif
#ifdef HW_KEYS
/* init variables for additional keys */
UI.KeyStep = 1; /* default level #1 */
#endif
#ifdef POWER_OFF_TIMEOUT
/* init power-off timeout */
if (Cfg.OP_Control & OP_PWR_TIMEOUT) /* power-off timeout enabled */
{
PwrTimeout = POWER_OFF_TIMEOUT * 2; /* in 500ms */
}
#endif
if (Mode & CHECK_OP_MODE) /* consider operation mode */
{
if (Cfg.OP_Mode & OP_AUTOHOLD) /* in auto-hold mode */
{
Timeout = 0; /* disable timeout */
/* and keep cursor mode */
}
else /* in continuous mode */
{
Mode &= ~(CURSOR_STEADY | CURSOR_BLINK); /* clear all cursor flags */
/* and keep timeout */
}
}
#ifdef UI_COLORED_CURSOR
Color = UI.PenColor; /* save current pen color */
UI.PenColor = COLOR_CURSOR; /* set cursor color */
#endif
#ifdef UI_KEY_HINTS
if (Mode & CURSOR_TEXT) /* show key hint */
{
Test = EEStringLength(UI.KeyHint); /* length of string */
Test--; /* adjust to X addressing */
if (UI.CharMax_X > Test) /* sanity check */
{
Pos = UI.CharMax_X - Test; /* text position */
/* disable cursor */
Mode &= ~(CURSOR_STEADY | CURSOR_BLINK); /* clear all cursor flags */
/* display key hint at bottom right */
LCD_CharPos(Pos, UI.CharMax_Y); /* set start position */
Display_EEString(UI.KeyHint); /* display hint */
}
}
#endif
if (Mode & (CURSOR_STEADY | CURSOR_BLINK)) /* cursor enabled */
{
LCD_Cursor(1); /* enable cursor on display */
}
/*
* wait for user feedback or timeout
*/
while (Run)
{
/* take care about feedback timeout */
if (Timeout > 0) /* timeout enabled */
{
if (Timeout > DELAY_TICK) /* some time left */
{
Timeout -= DELAY_TICK; /* decrease timeout */
}
else /* timeout */
{
Run = 0; /* end loop */
}
}
/*
* check for test push button
* - push button is low active
*/
Test = BUTTON_PIN & (1 << TEST_BUTTON); /* get button status */
if (Test == 0) /* test button pressed */
{
Ticks = 0; /* reset counter */
MilliSleep(30); /* time to debounce */
while (Run) /* detect how long key is pressed */
{
Test = BUTTON_PIN & (1 << TEST_BUTTON); /* get button status */
if (Test == 0) /* key still pressed */
{
Ticks++; /* increase counter */
if (Ticks > 26) Run = 0; /* end loop if 300ms are reached */
else MilliSleep(10); /* otherwise wait 10ms */
}
else /* key released */
{
Run = 0; /* end loop */
}
}
/* determine key press type */
if (Ticks > 26) /* long (>= 300ms) */
{
Key = KEY_LONG; /* signal long key press */
}
else /* short (< 300ms) */
{
Key = KEY_SHORT; /* signal short key press */
/* check for second key press if requested */
if (Mode & CHECK_KEY_TWICE)
{
MilliSleep(50); /* delay for checking key again */
Ticks = 20; /* timeout of 200ms */
while (Ticks > 0) /* timeout loop */
{
Test = BUTTON_PIN & (1 << TEST_BUTTON); /* get button status */
if (Test == 0) /* test button pressed */
{
MilliSleep(30); /* time to debounce */
Test = BUTTON_PIN & (1 << TEST_BUTTON); /* get button status */
if (Test == 0) /* test button still pressed */
{
Ticks = 1; /* end loop */
Key = KEY_TWICE; /* signal two key presses */
MilliSleep(200); /* smooth UI */
}
}
Ticks--; /* decrease timeout */
MilliSleep(10); /* wait 10ms */
}
}
}
}
else /* no key press */
{
/*
* touch screen
*/
#ifdef HW_TOUCH
Test = ReadTouchScreen(Mode);
if (Test) /* got user input */
{
Key = Test; /* save key */
break; /* exit loop */
}
#endif
/*
* increase/decrease push buttons
*/
#ifdef HW_INCDEC_KEYS
Test = ReadIncDecKeys();
if (Test) /* got user input */
{
Key = Test; /* save key */
break; /* exit loop */
}
#endif
/*
* rotary encoder
*/
#ifdef HW_ENCODER
Test = ReadEncoder(); /* read rotary encoder */
if (Test) /* got user input */
{
if (Steps == 0) /* first step */
{
Key = Test; /* save direction */
}
if (Test == Key) /* step in same direction */
{
Steps++; /* increase counter */
/* calculate turning velocity */
Test = UI.EncTicks / Steps; /* ticks per step */
Timeout2 += Test; /* add to timeout */
Timeout2 += 3 * ENCODER_PULSES; /* add some buffer */
/* adjustment for steps/360°: *(steps/16) */
Temp = Test * ENCODER_STEPS;
Temp /= 16;
Test = (uint8_t)Temp;
/* velocity levels: 0 (fast) - 5 (slow) */
if (Test > 40) Test = 40; /* limit ticks */
Test /= 8; /* get velocity level (0-5) */
/* require 3 steps for high velocities */
if (Test <= 2) MinSteps = 3;
if (Steps == MinSteps) /* got required steps */
{
/* reverse velocity level: 2 (slow) - 7 (fast) */
UI.KeyStep = 7 - Test; /* velocity level (2-7) */
break; /* exit loop */
}
}
else /* changed direction */
{
/* keep last direction and velocity level #1 */
break; /* exit loop */
}
}
if (Steps) /* after first step */
{
if (UI.EncTicks >= Timeout2) /* timeout for velocity detection */
{
/* keep velocity level #1 */