aboutsummaryrefslogtreecommitdiff
path: root/keyboards/chibios_test/stm32_f072_onekey/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/chibios_test/stm32_f072_onekey/matrix.c')
-rw-r--r--keyboards/chibios_test/stm32_f072_onekey/matrix.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/keyboards/chibios_test/stm32_f072_onekey/matrix.c b/keyboards/chibios_test/stm32_f072_onekey/matrix.c
new file mode 100644
index 000000000..a05b38cd4
--- /dev/null
+++ b/keyboards/chibios_test/stm32_f072_onekey/matrix.c
@@ -0,0 +1,163 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "ch.h"
19#include "hal.h"
20
21/*
22 * scan matrix
23 */
24#include "print.h"
25#include "debug.h"
26#include "util.h"
27#include "matrix.h"
28#include "wait.h"
29
30#ifndef DEBOUNCE
31# define DEBOUNCE 5
32#endif
33static uint8_t debouncing = DEBOUNCE;
34
35/* matrix state(1:on, 0:off) */
36static matrix_row_t matrix[MATRIX_ROWS];
37static matrix_row_t matrix_debouncing[MATRIX_ROWS];
38
39static matrix_row_t read_cols(void);
40static void init_cols(void);
41static void unselect_rows(void);
42static void select_row(uint8_t row);
43
44
45inline
46uint8_t matrix_rows(void)
47{
48 return MATRIX_ROWS;
49}
50
51inline
52uint8_t matrix_cols(void)
53{
54 return MATRIX_COLS;
55}
56
57#define LED_ON() do { palSetPad(GPIOC, GPIOC_LED_BLUE) ;} while (0)
58#define LED_OFF() do { palClearPad(GPIOC, GPIOC_LED_BLUE); } while (0)
59#define LED_TGL() do { palTogglePad(GPIOC, GPIOC_LED_BLUE); } while (0)
60
61void matrix_init(void)
62{
63 // initialize row and col
64 unselect_rows();
65 init_cols();
66
67 // initialize matrix state: all keys off
68 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
69 matrix[i] = 0;
70 matrix_debouncing[i] = 0;
71 }
72
73 //debug
74 debug_matrix = true;
75 LED_ON();
76 wait_ms(500);
77 LED_OFF();
78}
79
80uint8_t matrix_scan(void)
81{
82 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
83 select_row(i);
84 wait_us(30); // without this wait read unstable value.
85 matrix_row_t cols = read_cols();
86 if (matrix_debouncing[i] != cols) {
87 matrix_debouncing[i] = cols;
88 if (debouncing) {
89 debug("bounce!: "); debug_hex(debouncing); debug("\n");
90 }
91 debouncing = DEBOUNCE;
92 }
93 unselect_rows();
94 }
95
96 if (debouncing) {
97 if (--debouncing) {
98 wait_ms(1);
99 } else {
100 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 matrix[i] = matrix_debouncing[i];
102 }
103 }
104 }
105
106 return 1;
107}
108
109inline
110bool matrix_is_on(uint8_t row, uint8_t col)
111{
112 return (matrix[row] & ((matrix_row_t)1<<col));
113}
114
115inline
116matrix_row_t matrix_get_row(uint8_t row)
117{
118 return matrix[row];
119}
120
121void matrix_print(void)
122{
123 print("\nr/c 0123456789ABCDEF\n");
124 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
125 phex(row); print(": ");
126 pbin_reverse16(matrix_get_row(row));
127 print("\n");
128 }
129}
130
131/* Column pin configuration
132 */
133static void init_cols(void)
134{
135 // don't need pullup/down, since it's pulled down in hardware
136 palSetPadMode(GPIOA, GPIOA_BUTTON, PAL_MODE_INPUT);
137}
138
139/* Returns status of switches(1:on, 0:off) */
140static matrix_row_t read_cols(void)
141{
142 return ((palReadPad(GPIOA, GPIOA_BUTTON)==PAL_LOW) ? 0 : (1<<0));
143 // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
144}
145
146/* Row pin configuration
147 */
148static void unselect_rows(void)
149{
150 // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_INPUT); // hi-Z
151}
152
153static void select_row(uint8_t row)
154{
155 (void)row;
156 // Output low to select
157 // switch (row) {
158 // case 0:
159 // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_OUTPUT_PUSHPULL);
160 // palSetPad(GPIOA, GPIOA_PIN10, PAL_LOW);
161 // break;
162 // }
163}