-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceKeypad.h
More file actions
134 lines (114 loc) · 3.62 KB
/
Copy pathServiceKeypad.h
File metadata and controls
134 lines (114 loc) · 3.62 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
#ifndef __SERVICEKEYPAD_H__
#define __SERVICEKEYPAD_H__
typedef void (*SERVICE_KEY_EVENT_FUNC)(int ev, int id);
enum
{
SERVICE_KEYPAD_FSM_JUST_STARTED = 0,
SERVICE_KEYPAD_WAITING_FRONT,
SERVICE_KEYPAD_AFTER_LONG_PRESSED
};
enum
{
SERVICE_KEY_JUST_PRESSED,
SERVICE_KEY_JUST_RELEASED,
SERVICE_KEY_LONG_PRESSED,
SERVICE_KEY_RELEASED_AFTER_LONG_PRESSED
};
typedef struct
{
char arduino_pin;
short debounce;
short long_pressing_time;
short state;
short counter;
char signal;
short long_pressing_timer;
volatile uint8_t* port;
uint8_t mask;
} SERVICE_KEYS_DESCRIPTION;
class ServiceKeypad
{
public:
ServiceKeypad(SERVICE_KEYS_DESCRIPTION *settings_table, int keys_num, SERVICE_KEY_EVENT_FUNC ev)
{
keys_number = keys_num;
table = settings_table;
ev_func = ev;
// Pre-calculate AVR input registers and bitmasks
for(int i = 0; i < keys_number; i++)
{
pinMode(table[i].arduino_pin, INPUT_PULLUP);
table[i].port = portInputRegister(digitalPinToPort(table[i].arduino_pin));
table[i].mask = digitalPinToBitMask(table[i].arduino_pin);
}
}
~ServiceKeypad() {}
void periodic(void)
{
int i;
for(i=0; i < keys_number; i++)
{
if(table[i].long_pressing_timer > 0)
{
if(--table[i].long_pressing_timer == 0)
{
//long pressing;
table[i].state = SERVICE_KEYPAD_AFTER_LONG_PRESSED;
ev_func(SERVICE_KEY_LONG_PRESSED,i);
}
}
uint8_t bit = (*table[i].port & table[i].mask) ? 0 : 1;
if(bit)
{
if(table[i].counter < table[i].debounce || table[i].debounce == 0)
{
if(++(table[i].counter) >= table[i].debounce || table[i].debounce == 0)
{
if(table[i].signal == 0)
{
if(table[i].long_pressing_time)
table[i].long_pressing_timer = table[i].long_pressing_time;
//key just pressed;
ev_func(SERVICE_KEY_JUST_PRESSED,i);
}
table[i].signal = 1;
}
}
}
else
{
if(table[i].counter > 0 || table[i].debounce == 0)
{
if(--(table[i].counter) <= 0)
{
if(table[i].signal)
{
if(table[i].state == SERVICE_KEYPAD_AFTER_LONG_PRESSED)
{
// key just released after long pressing
table[i].state = SERVICE_KEYPAD_WAITING_FRONT;
ev_func(SERVICE_KEY_RELEASED_AFTER_LONG_PRESSED,i);
}
else
{
// key just released (simple)
ev_func(SERVICE_KEY_JUST_RELEASED,i);
}
table[i].long_pressing_timer = 0;
}
table[i].signal = 0;
}
}
}
}
}
int get_signal(int idx)
{
return table[idx].signal;
}
private:
uint8_t keys_number;
SERVICE_KEYS_DESCRIPTION *table;
SERVICE_KEY_EVENT_FUNC ev_func;
};
#endif