]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/onekey/matrix.c
Fix rn42_linked() to use pullup
[tmk_firmware.git] / keyboard / onekey / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29
30
31 #ifndef DEBOUNCE
32 #   define DEBOUNCE     5
33 #endif
34 static uint8_t debouncing = DEBOUNCE;
35
36 /* matrix state(1:on, 0:off) */
37 static matrix_row_t matrix[MATRIX_ROWS];
38 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39
40 static matrix_row_t read_cols(void);
41 static void init_cols(void);
42 static void unselect_rows(void);
43 static void select_row(uint8_t row);
44
45
46 inline
47 uint8_t matrix_rows(void)
48 {
49     return MATRIX_ROWS;
50 }
51
52 inline
53 uint8_t matrix_cols(void)
54 {
55     return MATRIX_COLS;
56 }
57
58 void matrix_init(void)
59 {
60     debug_enable = true;
61     debug_matrix = true;
62     debug_mouse = true;
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
74 uint8_t matrix_scan(void)
75 {
76     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
77         select_row(i);
78         _delay_us(30);  // without this wait read unstable value.
79         matrix_row_t cols = read_cols();
80         if (matrix_debouncing[i] != cols) {
81             matrix_debouncing[i] = cols;
82             if (debouncing) {
83                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
84             }
85             debouncing = DEBOUNCE;
86         }
87         unselect_rows();
88     }
89
90     if (debouncing) {
91         if (--debouncing) {
92             _delay_ms(1);
93         } else {
94             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
95                 matrix[i] = matrix_debouncing[i];
96             }
97         }
98     }
99
100     return 1;
101 }
102
103 bool matrix_is_modified(void)
104 {
105     if (debouncing) return false;
106     return true;
107 }
108
109 inline
110 bool matrix_is_on(uint8_t row, uint8_t col)
111 {
112     return (matrix[row] & ((matrix_row_t)1<<col));
113 }
114
115 inline
116 matrix_row_t matrix_get_row(uint8_t row)
117 {
118     return matrix[row];
119 }
120
121 void 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 uint8_t matrix_key_count(void)
132 {
133     uint8_t count = 0;
134     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
135         count += bitpop16(matrix[i]);
136     }
137     return count;
138 }
139
140 /* Column pin configuration
141  * col: 0
142  * pin: B0
143  */
144 static void  init_cols(void)
145 {
146     // Input with pull-up(DDR:0, PORT:1)
147     DDRB  &= ~(1<<0);
148     PORTB |=  (1<<0);
149 }
150
151 static matrix_row_t read_cols(void)
152 {
153     return (PINB&(1<<0) ? 0 : (1<<0));
154 }
155
156 /* Row pin configuration
157  * row: 0
158  * pin: B1
159  */
160 static void unselect_rows(void)
161 {
162     // Hi-Z(DDR:0, PORT:0) to unselect
163     DDRB  &= ~0b00000010;
164     PORTB &= ~0b00000010;
165 }
166
167 static void select_row(uint8_t row)
168 {
169     // Output low(DDR:1, PORT:0) to select
170     switch (row) {
171         case 0:
172             DDRB  |= (1<<1);
173             PORTB &= ~(1<<1);
174             break;
175     }
176 }