]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergoinu/matrix.c
Keyboard: add ergoinu keyboard (#3655)
[qmk_firmware.git] / keyboards / ergoinu / 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 <avr/wdt.h>
25 #include <avr/interrupt.h>
26 #include <util/delay.h>
27 #include "print.h"
28 #include "debug.h"
29 #include "util.h"
30 #include "matrix.h"
31 #include "split_util.h"
32
33 #include "serial.h"
34
35 // from pro_micro.h
36 #define TX_RX_LED_INIT  DDRD |= (1<<5), DDRB |= (1<<0)
37
38 #ifndef DISABLE_PROMICRO_LEDs
39   #define TXLED0          PORTD |= (1<<5)
40   #define TXLED1          PORTD &= ~(1<<5)
41   #define RXLED0          PORTB |= (1<<0)
42   #define RXLED1          PORTB &= ~(1<<0)
43 #else
44   #define TXLED0
45   #define TXLED1
46   #define RXLED0
47   #define RXLED1
48 #endif
49
50 #ifndef DEBOUNCE
51 #  define DEBOUNCE      5
52 #endif
53
54 #define ERROR_DISCONNECT_COUNT 5
55
56 static uint8_t debouncing = DEBOUNCE;
57 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
58 static uint8_t error_count = 0;
59 uint8_t is_master = 0 ;
60
61 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
62 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
63
64 /* matrix state(1:on, 0:off) */
65 static matrix_row_t matrix[MATRIX_ROWS];
66 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
67
68 static matrix_row_t read_cols(void);
69 static void init_cols(void);
70 static void unselect_rows(void);
71 static void select_row(uint8_t row);
72 static uint8_t matrix_master_scan(void);
73
74
75 __attribute__ ((weak))
76 void matrix_init_kb(void) {
77   matrix_init_user();
78 }
79
80 __attribute__ ((weak))
81 void matrix_scan_kb(void) {
82   matrix_scan_user();
83 }
84
85 __attribute__ ((weak))
86 void matrix_init_user(void) {
87 }
88
89 __attribute__ ((weak))
90 void matrix_scan_user(void) {
91 }
92
93 inline
94 uint8_t matrix_rows(void) {
95   return MATRIX_ROWS;
96 }
97
98 inline
99 uint8_t matrix_cols(void) {
100   return MATRIX_COLS;
101 }
102
103 void matrix_init(void) {
104   debug_enable = true;
105   debug_matrix = true;
106   debug_mouse = true;
107   // initialize row and col
108   unselect_rows();
109   init_cols();
110
111   TX_RX_LED_INIT;
112
113   #ifdef DISABLE_PROMICRO_LEDs
114     PORTD |= (1<<5);
115     PORTB |= (1<<0);
116   #endif
117
118   // initialize matrix state: all keys off
119   for (uint8_t i=0; i < MATRIX_ROWS; i++) {
120       matrix[i] = 0;
121       matrix_debouncing[i] = 0;
122   }
123
124   is_master = has_usb();
125
126   matrix_init_quantum();
127 }
128
129 uint8_t _matrix_scan(void) {
130   // Right hand is stored after the left in the matirx so, we need to offset it
131   int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
132
133   for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
134       select_row(i);
135       _delay_us(30);  // without this wait read unstable value.
136       matrix_row_t cols = read_cols();
137       if (matrix_debouncing[i+offset] != cols) {
138           matrix_debouncing[i+offset] = cols;
139           debouncing = DEBOUNCE;
140       }
141       unselect_rows();
142   }
143
144   if (debouncing) {
145       if (--debouncing) {
146           _delay_ms(1);
147       } else {
148           for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
149               matrix[i+offset] = matrix_debouncing[i+offset];
150           }
151       }
152   }
153
154   return 1;
155 }
156
157 int serial_transaction(void) {
158   int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
159   int ret=serial_update_buffers();
160   if (ret ) {
161       if(ret==2)RXLED1;
162       return 1;
163   }
164 RXLED0;
165   for (int i = 0; i < ROWS_PER_HAND; ++i) {
166       matrix[slaveOffset+i] = serial_slave_buffer[i];
167   }
168   return 0;
169 }
170
171 uint8_t matrix_scan(void) {
172   if (is_master) {
173     matrix_master_scan();
174   }else{
175     matrix_slave_scan();
176
177 //  if(serial_slave_DATA_CORRUPT()){
178 //    TXLED0;
179     int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
180
181     for (int i = 0; i < ROWS_PER_HAND; ++i) {
182       matrix[offset+i] = serial_master_buffer[i];
183     }
184
185 //  }else{
186 //    TXLED1;
187 //  }
188
189     matrix_scan_quantum();
190   }
191   return 1;
192 }
193
194
195 uint8_t matrix_master_scan(void) {
196
197   int ret = _matrix_scan();
198
199   int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
200
201   for (int i = 0; i < ROWS_PER_HAND; ++i) {
202     serial_master_buffer[i] = matrix[offset+i];
203   }
204
205   if( serial_transaction() ) {
206     // turn on the indicator led when halves are disconnected
207     TXLED1;
208
209     error_count++;
210
211     if (error_count > ERROR_DISCONNECT_COUNT) {
212         // reset other half if disconnected
213       int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
214       for (int i = 0; i < ROWS_PER_HAND; ++i) {
215           matrix[slaveOffset+i] = 0;
216       }
217     }
218   } else {
219     // turn off the indicator led on no error
220     TXLED0;
221     error_count = 0;
222   }
223   matrix_scan_quantum();
224   return ret;
225 }
226
227 void matrix_slave_scan(void) {
228   _matrix_scan();
229
230   int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
231
232   for (int i = 0; i < ROWS_PER_HAND; ++i) {
233     serial_slave_buffer[i] = matrix[offset+i];
234   }
235 }
236
237 bool matrix_is_modified(void)
238 {
239   if (debouncing) return false;
240   return true;
241 }
242
243 inline
244 bool matrix_is_on(uint8_t row, uint8_t col)
245 {
246   return (matrix[row] & ((matrix_row_t)1<<col));
247 }
248
249 inline
250 matrix_row_t matrix_get_row(uint8_t row)
251 {
252   return matrix[row];
253 }
254
255 void matrix_print(void)
256 {
257   print("\nr/c 0123456789ABCDEF\n");
258   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
259     phex(row); print(": ");
260     pbin_reverse16(matrix_get_row(row));
261     print("\n");
262   }
263 }
264
265 uint8_t matrix_key_count(void)
266 {
267   uint8_t count = 0;
268   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
269     count += bitpop16(matrix[i]);
270   }
271   return count;
272 }
273
274 static void  init_cols(void)
275 {
276   for(int x = 0; x < MATRIX_COLS; x++) {
277     _SFR_IO8((col_pins[x] >> 4) + 1) &=  ~_BV(col_pins[x] & 0xF);
278     _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
279   }
280 }
281
282 static matrix_row_t read_cols(void)
283 {
284   matrix_row_t result = 0;
285   for(int x = 0; x < MATRIX_COLS; x++) {
286     result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
287   }
288   return result;
289 }
290
291 static void unselect_rows(void)
292 {
293   for(int x = 0; x < ROWS_PER_HAND; x++) {
294     _SFR_IO8((row_pins[x] >> 4) + 1) &=  ~_BV(row_pins[x] & 0xF);
295     _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
296   }
297 }
298
299 static void select_row(uint8_t row)
300 {
301   _SFR_IO8((row_pins[row] >> 4) + 1) |=  _BV(row_pins[row] & 0xF);
302   _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
303 }