-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathuib.c
132 lines (114 loc) · 2.76 KB
/
uib.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
#include "uib.h"
#include "adm.h"
/** User Input Interface Component */
#define NUM_OF_BUT (4u)
#define ADC_SPAN (ADC_MAX/(NUM_OF_BUT))
#define RANGE1 ((1*ADC_SPAN)-(ADC_SPAN/2))
#define RANGE2 ((2*ADC_SPAN)-(ADC_SPAN/2))
#define RANGE3 ((3*ADC_SPAN)-(ADC_SPAN/2))
#define RANGE4 ((4*ADC_SPAN)-(ADC_SPAN/2))
/* Button Debounce Time */
#define uib_INIT_DEBOUNCE (5u)
#define uib_SECOND_DEBOUNCE (100u)
#define uib_REPEAT_DEBOUNCE (25u) /* debounce time for repeated pushes */
enum UIB_states {
INIT_DEBOUNCE = 0u,SECOND_DEBOUNCE,REPEAT_DEBOUNCE,
};
/* Debug Code */
#define DBG 0
#if DBG
#include "print.h"
#define PRINT(x) printStr((void*)x, RAM);
#else /* if DBG */
#define PRINT(x)
#endif
/* public variable */
uint8_t UIB_buttonPressed = NOT_PRESSED;
uint8_t UIB_buttonChanged = 0u;
static uint8_t uib_lastButtonPressed;
static uint16_t uib_debounceCtr = 0u;
static uint8_t uib_state = INIT_DEBOUNCE;
void UIB_Task(void);
static void uib_buttonChanged(void){
UIB_buttonChanged = 1u;
uib_debounceCtr = 0u;
}
void UIB_Task(void){
uib_lastButtonPressed = UIB_buttonPressed;
/* The if comparison goes from highest ADC value to the lowest one*/
if(ADM_GetAdcValue() > RANGE4)
{
UIB_buttonPressed = RANGE4_PRESSED;
}
else if(ADM_GetAdcValue() > RANGE3 )
{
UIB_buttonPressed = RANGE3_PRESSED;
}
else if(ADM_GetAdcValue() > RANGE2)
{
UIB_buttonPressed = RANGE2_PRESSED;
}
else if(ADM_GetAdcValue() > RANGE1)
{
UIB_buttonPressed = RANGE1_PRESSED;
}
else
{
UIB_buttonPressed = NOT_PRESSED;
}
if(uib_lastButtonPressed == UIB_buttonPressed)
{
uib_debounceCtr++;
}
else
{
uib_state = INIT_DEBOUNCE;
uib_debounceCtr = 0u;
}
// by default buttonChanged = 0
UIB_buttonChanged = 0u;
switch (uib_state)
{
case INIT_DEBOUNCE:
if( uib_debounceCtr >= uib_INIT_DEBOUNCE )
{
uib_buttonChanged();
uib_state++;
}
break;
case SECOND_DEBOUNCE:
if( uib_debounceCtr >= uib_SECOND_DEBOUNCE )
{
uib_buttonChanged();
uib_state++;
}
break;
case REPEAT_DEBOUNCE:
if( uib_debounceCtr >= uib_REPEAT_DEBOUNCE )
{
uib_buttonChanged();
}
break;
}
#if DBG
if(UIB_buttonChanged == 1)
{
switch(UIB_buttonPressed) {
case LEFT:
PRINT("LEFT\n");
break;
case UP:
PRINT("UP\n");
break;
case RIGHT:
PRINT("RIGHT\n");
break;
case DOWN:
PRINT("DOWN\n");
break;
default:
return;
}
}
#endif
}