summaryrefslogtreecommitdiff
path: root/src/drivers/ao_matrix.c
blob: fa2d0c57c1af8bff5d39976a83b23ed37792c488 (plain) (blame)
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
/*
 * Copyright © 2017 Keith Packard <keithp@keithp.com>
 *
 * 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, or
 * (at your option) any later version.
 *
 * 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.
 */

#include <ao.h>
#include <ao_matrix.h>
#include <ao_event.h>
#include <ao_exti.h>

#define row_port(q)	AO_MATRIX_ROW_ ## q ## _PORT
#define row_bit(q) 	AO_MATRIX_ROW_ ## q ## _PIN
#define row_pin(q) 	AO_MATRIX_ROW_ ## q ## _PIN

#define col_port(q)	AO_MATRIX_COL_ ## q ## _PORT
#define col_bit(q) 	AO_MATRIX_COL_ ## q ## _PIN
#define col_pin(q) 	AO_MATRIX_COL_ ## q ## _PIN

static void
_ao_matrix_drive_row(uint8_t row, uint8_t val)
{
	switch (row) {
#define drive(n) case n: ao_gpio_set(row_port(n), row_bit(n), row_pin(n), val); break
		drive(0);
#if AO_MATRIX_ROWS > 1
		drive(1);
#endif
#if AO_MATRIX_ROWS > 2
		drive(2);
#endif
#if AO_MATRIX_ROWS > 3
		drive(3);
#endif
#if AO_MATRIX_ROWS > 4
		drive(4);
#endif
#if AO_MATRIX_ROWS > 5
		drive(5);
#endif
#if AO_MATRIX_ROWS > 6
		drive(6);
#endif
#if AO_MATRIX_ROWS > 7
		drive(7);
#endif
	}
}

static uint8_t
_ao_matrix_read_cols(void)
{
	uint8_t	v = 0;
#define read(n)	(v |= ao_gpio_get(col_port(n), col_bit(n), col_pin(n)) << n)

	read(0);
#if AO_MATRIX_ROWS > 1
	read(1);
#endif
#if AO_MATRIX_ROWS > 2
	read(2);
#endif
#if AO_MATRIX_ROWS > 3
	read(3);
#endif
#if AO_MATRIX_ROWS > 4
	read(4);
#endif
#if AO_MATRIX_ROWS > 5
	read(5);
#endif
#if AO_MATRIX_ROWS > 6
	read(6);
#endif
#if AO_MATRIX_ROWS > 7
	read(7);
#endif
	return v;
}

static uint8_t
_ao_matrix_read(uint8_t row) {
	uint8_t	state;
	_ao_matrix_drive_row(row, 0);
	state = _ao_matrix_read_cols();
	_ao_matrix_drive_row(row, 1);
	return state;
}

#define AO_MATRIX_DEBOUNCE_INTERVAL	AO_MS_TO_TICKS(50)

static uint8_t ao_matrix_keymap[AO_MATRIX_ROWS][AO_MATRIX_COLS] = AO_MATRIX_KEYCODES;

static uint8_t		ao_matrix_state[AO_MATRIX_ROWS];
static AO_TICK_TYPE	ao_matrix_tick[AO_MATRIX_ROWS];

static void
_ao_matrix_poll_one(uint8_t row) {
	uint8_t	state = _ao_matrix_read(row);

	if (state != ao_matrix_state[row]) {
		AO_TICK_TYPE	now = ao_time();

		if ((now - ao_matrix_tick[row]) >= AO_MATRIX_DEBOUNCE_INTERVAL) {
			uint8_t col;
			uint8_t changes = state ^ ao_matrix_state[row];

			for (col = 0; col < AO_MATRIX_COLS; col++) {
				if (changes & (1 << col)) {
					ao_event_put_isr(AO_EVENT_KEY,
							 ao_matrix_keymap[row][col],
							 ((state >> col) & 1) == 0);
				}
			}
			ao_matrix_state[row] = state;
		}
		ao_matrix_tick[row] = now;
	}
}

void
ao_matrix_poll(void)
{
	uint8_t	row;

	for (row = 0; row < AO_MATRIX_ROWS; row++)
		_ao_matrix_poll_one(row);
}

#define init_row(b) do {						\
		ao_enable_output(row_port(b), row_bit(b), row_pin(v), 1); \
		ao_gpio_set_output_mode(row_port(b), row_bit(b), row_pin(b), AO_OUTPUT_OPEN_DRAIN); \
	} while (0)

#define init_col(b) do { \
		ao_enable_input(col_port(b), col_bit(b), AO_EXTI_MODE_PULL_UP); \
	} while(0)

void
ao_matrix_init(void)
{
	uint8_t	row;

	init_row(0);
#if AO_MATRIX_ROWS > 1
	init_row(1);
#endif
#if AO_MATRIX_ROWS > 2
	init_row(2);
#endif
#if AO_MATRIX_ROWS > 3
	init_row(3);
#endif
#if AO_MATRIX_ROWS > 4
	init_row(4);
#endif
#if AO_MATRIX_ROWS > 5
	init_row(5);
#endif
#if AO_MATRIX_ROWS > 6
	init_row(6);
#endif
#if AO_MATRIX_ROWS > 7
	init_row(7);
#endif

	init_col(0);
#if AO_MATRIX_COLS > 1
	init_col(1);
#endif
#if AO_MATRIX_COLS > 2
	init_col(2);
#endif
#if AO_MATRIX_COLS > 3
	init_col(3);
#endif
#if AO_MATRIX_COLS > 4
	init_col(4);
#endif
#if AO_MATRIX_COLS > 5
	init_col(5);
#endif
#if AO_MATRIX_COLS > 6
	init_col(6);
#endif
#if AO_MATRIX_COLS > 7
	init_col(7);
#endif
	for (row = 0; row < AO_MATRIX_ROWS; row++) {
		ao_matrix_state[row] = _ao_matrix_read(row);
		ao_matrix_tick[row] = ao_time();
	}
}