-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixKeypad.h
More file actions
229 lines (202 loc) · 7.51 KB
/
Copy pathMatrixKeypad.h
File metadata and controls
229 lines (202 loc) · 7.51 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
#include "Arduino.h"
#define TBUF_LEN 8
#define INPUT_EVENTS_FIFO_SIZE 32
#define MATRIX_ROWS_MAX 8
typedef enum {
MATRIX_SWITCH_ON_OFF = 0,/* events for ON and OFF (8XX and 4XX)*/
MATRIX_SWITCH_ON, /* events for ON only (1XX)*/
MATRIX_SWITCH_ON_DBLFUNC /* events for ON and long pressing >1.5 sec (1XX and (1XX + 0x80))*/
} matrix_node_type;
typedef struct {
char id;
matrix_node_type mode;
char debounce;
short long_pressing_time;
char counter;
char signal;
short long_pressing_timer;
} KEY_SETTINGS;
class MatrixKeypad
{
public:
typedef struct {
volatile int push_indx;
volatile int pop_indx;
uint16_t buff[INPUT_EVENTS_FIFO_SIZE];
} INPUT_EVENTS_FIFO;
INPUT_EVENTS_FIFO fifo;
typedef struct {
uint8_t* port;
uint8_t mask;
} LINES_DESCRIPTION;
MatrixKeypad(KEY_SETTINGS *settings_table, int keys_number, uint8_t *rows_pins, uint8_t rows_number, uint8_t *columns_pins, uint8_t columns_number)
{
table = settings_table;
keys_max = keys_number;
fifo.push_indx = 0;
fifo.pop_indx = 0;
rows_num = rows_number;
columns_num = columns_number;
// Initialize rows as inputs with internal pull-ups
for(int i = 0; i < rows_num; i++) {
pinMode(rows_pins[i], INPUT_PULLUP);
rows[i].port = portInputRegister(digitalPinToPort(rows_pins[i]));
rows[i].mask = digitalPinToBitMask(rows_pins[i]);
}
// Initialize columns as outputs, set them default to HIGH
for(int i = 0; i < columns_num; i++) {
pinMode(columns_pins[i], OUTPUT);
columns[i].port = portOutputRegister(digitalPinToPort(columns_pins[i]));
columns[i].mask = digitalPinToBitMask(columns_pins[i]);
*columns[i].port |= columns[i].mask; // Idle state for active-low scanning is HIGH
keys_raw_state[i] = 0x00;
}
scan_index = 0;
}
~MatrixKeypad() {}
// SAFE TO CALL INSIDE HIGH-FREQUENCY INTERRUPTS (Takes ~1 microsecond)
// Progresses the active-low hardware scanning matrix by exactly one column
void scan_step(void)
{
uint8_t result = 0;
uint8_t mask = 1;
// 1. Read all rows for the CURRENT active column
for(int i = 0; i < rows_num; i++) {
if ((*rows[i].port & rows[i].mask) == 0) {
result |= mask;
}
mask <<= 1;
}
keys_raw_state[scan_index] = result;
// 2. Deactivate the current column (return it to HIGH)
*columns[scan_index].port |= columns[scan_index].mask;
// 3. Move onto the next column index
scan_index++;
if (scan_index >= columns_num) scan_index = 0;
// 4. Activate the next column (pull it to LOW)
*columns[scan_index].port &= ~columns[scan_index].mask;
}
// CALL ONLY INSIDE THE MAIN loop() (Asynchronous debouncing logic)
// Processes state machines, edge detection, and pushes new events to FIFO
void periodic(void)
{
uint16_t utmp16;
for(int i = 0; i < keys_max; i++)
{
// Process critical delayed timers for rotary/delayed switches
if(table[i].long_pressing_timer)
{
if(--table[i].long_pressing_timer == 0)
{
if(table[i].mode == MATRIX_SWITCH_ON_DBLFUNC)
{
utmp16 = 0x180 | table[i].id;
fifo_push(utmp16);
}
}
}
uint8_t b = get_raw_key_state(i);
if(b) // Key is physically pressed
{
if(table[i].counter < table[i].debounce)
{
if(++(table[i].counter) >= table[i].debounce) {
if(table[i].signal == 0) {
// Rising edge detected (__0__/--1--)
if(table[i].long_pressing_timer) {
table[i].long_pressing_timer = table[i].long_pressing_time;
}
if(table[i].mode == MATRIX_SWITCH_ON)
{
utmp16 = 0x100 | table[i].id; // Triggers {1XX}
fifo_push(utmp16);
}
else if(table[i].mode == MATRIX_SWITCH_ON_OFF)
{
utmp16 = 0x800 | table[i].id; // Triggers {8XX}
fifo_push(utmp16);
}
}
table[i].signal = 1;
}
}
}
else // Key is physically released
{
if(table[i].counter > 0) {
if(--(table[i].counter) <= 0) {
if(table[i].signal)
{
// Falling edge detected (--1--\___0___)
if(table[i].mode == MATRIX_SWITCH_ON_OFF)
{
utmp16 = 0x400 | table[i].id; // Triggers {4XX}
fifo_push(utmp16);
}
else if((table[i].mode == MATRIX_SWITCH_ON_DBLFUNC) && table[i].long_pressing_timer)
{
utmp16 = 0x100 | table[i].id; // Multi-position switch fallback
fifo_push(utmp16);
}
table[i].long_pressing_timer = 0;
}
table[i].signal = 0;
}
}
}
}
}
int fifo_pop(uint16_t *data)
{
int indx;
if (fifo.push_indx == fifo.pop_indx) return 0;
indx = (fifo.pop_indx + 1) % INPUT_EVENTS_FIFO_SIZE;
*data = fifo.buff[indx];
fifo.pop_indx = indx;
return 1;
}
// CALL ONLY INSIDE loop() (Streams queued matrix actions out to XPIOHUB)
void manage_events(void)
{
uint16_t event;
if(fifo_pop(&event))
{
tbuf[0] = '{';
tbuf[1] = nibble_to_hex((event >> 8) & 0x0F);
tbuf[2] = nibble_to_hex((event >> 4) & 0x0F);
tbuf[3] = nibble_to_hex(event & 0x0F);
tbuf[4] = '}';
Serial.write(tbuf, 5); // Emits the parsed operational frame instantly
}
}
private:
uint8_t tbuf[TBUF_LEN];
KEY_SETTINGS *table;
int keys_max;
LINES_DESCRIPTION rows[MATRIX_ROWS_MAX];
LINES_DESCRIPTION columns[MATRIX_ROWS_MAX];
uint8_t rows_num;
uint8_t columns_num;
uint8_t keys_raw_state[MATRIX_ROWS_MAX];
uint8_t scan_index;
// Resolves linear key index to its row/column bit position inside state buffer
int get_raw_key_state(uint8_t index)
{
uint8_t row = index % rows_num;
uint8_t col = index / rows_num;
return ((keys_raw_state[col] & (1 << row)) != 0);
}
int fifo_push(uint16_t data)
{
int indx = (fifo.push_indx + 1) % INPUT_EVENTS_FIFO_SIZE;
if (indx == fifo.pop_indx) return 0;
fifo.buff[indx] = data;
fifo.push_indx = indx;
return 1;
}
int8_t nibble_to_hex(int8_t nibble)
{
static const char hex_table[] = "0123456789ABCDEF";
return hex_table[nibble & 0x0F];
}
};