2 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
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.
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.
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/>.
24 #include <util/delay.h>
25 #include "action_layer.h"
31 #include "i2cmaster.h"
32 #ifdef DEBUG_MATRIX_SCAN_RATE
39 static uint8_t debouncing = DEBOUNCE;
41 /* matrix state(1:on, 0:off) */
42 static matrix_row_t matrix[MATRIX_ROWS];
43 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
45 static matrix_row_t read_cols(uint8_t row);
46 static void init_cols(void);
47 static void unselect_rows(void);
48 static void select_row(uint8_t row);
50 static uint8_t mcp23018_reset_loop;
52 #ifdef DEBUG_MATRIX_SCAN_RATE
53 uint32_t matrix_timer;
54 uint32_t matrix_scan_count;
58 uint8_t matrix_rows(void)
64 uint8_t matrix_cols(void)
69 void matrix_init(void)
71 // initialize row and col
73 mcp23018_status = init_mcp23018();
74 ergodox_blink_all_leds();
78 // initialize matrix state: all keys off
79 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
81 matrix_debouncing[i] = 0;
84 #ifdef DEBUG_MATRIX_SCAN_RATE
85 matrix_timer = timer_read32();
86 matrix_scan_count = 0;
90 uint8_t matrix_scan(void)
92 if (mcp23018_status) { // if there was an error
93 if (++mcp23018_reset_loop == 0) {
94 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
95 // this will be approx bit more frequent than once per second
96 print("trying to reset mcp23018\n");
97 mcp23018_status = init_mcp23018();
98 if (mcp23018_status) {
99 print("left side not responding\n");
101 print("left side attached\n");
102 ergodox_blink_all_leds();
107 #ifdef DEBUG_MATRIX_SCAN_RATE
110 uint32_t timer_now = timer_read32();
111 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
112 print("matrix scan frequency: ");
113 pdec(matrix_scan_count);
116 matrix_timer = timer_now;
117 matrix_scan_count = 0;
122 uint8_t layer = biton32(layer_state);
124 ergodox_board_led_off();
125 ergodox_left_led_1_off();
126 ergodox_left_led_2_off();
127 ergodox_left_led_3_off();
131 ergodox_left_led_1_on();
132 ergodox_left_led_2_on();
133 ergodox_left_led_3_on();
137 ergodox_left_led_2_on();
141 ergodox_left_led_2_on();
142 // break missed intentionally
145 ergodox_left_led_3_on();
148 ergodox_board_led_on();
149 // break missed intentionally
154 ergodox_left_led_1_on();
158 ergodox_left_led_1_on();
159 ergodox_left_led_3_on();
166 mcp23018_status = ergodox_left_leds_update();
169 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
171 matrix_row_t cols = read_cols(i);
172 if (matrix_debouncing[i] != cols) {
173 matrix_debouncing[i] = cols;
175 debug("bounce!: "); debug_hex(debouncing); debug("\n");
177 debouncing = DEBOUNCE;
186 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
187 matrix[i] = matrix_debouncing[i];
195 bool matrix_is_modified(void)
197 if (debouncing) return false;
202 bool matrix_is_on(uint8_t row, uint8_t col)
204 return (matrix[row] & ((matrix_row_t)1<<col));
208 matrix_row_t matrix_get_row(uint8_t row)
213 void matrix_print(void)
215 print("\nr/c 0123456789ABCDEF\n");
216 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
217 phex(row); print(": ");
218 pbin_reverse16(matrix_get_row(row));
223 uint8_t matrix_key_count(void)
226 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
227 count += bitpop16(matrix[i]);
232 /* Column pin configuration
236 * pin: F0 F1 F4 F5 F6 F7
240 * pin: B5 B4 B3 B2 B1 B0
242 static void init_cols(void)
245 // not needed, already done as part of init_mcp23018()
248 // Input with pull-up(DDR:0, PORT:1)
249 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
250 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
253 static matrix_row_t read_cols(uint8_t row)
256 if (mcp23018_status) { // if there was an error
260 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
261 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
262 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
263 data = i2c_readNak();
270 _delay_us(30); // without this wait read unstable value.
273 (PINF&(1<<0) ? 0 : (1<<0)) |
274 (PINF&(1<<1) ? 0 : (1<<1)) |
275 (PINF&(1<<4) ? 0 : (1<<2)) |
276 (PINF&(1<<5) ? 0 : (1<<3)) |
277 (PINF&(1<<6) ? 0 : (1<<4)) |
278 (PINF&(1<<7) ? 0 : (1<<5)) ;
282 /* Row pin configuration
285 * row: 7 8 9 10 11 12 13
286 * pin: B0 B1 B2 B3 D2 D3 C6
290 * pin: A0 A1 A2 A3 A4 A5 A6
292 static void unselect_rows(void)
294 // unselect on mcp23018
295 if (mcp23018_status) { // if there was an error
298 // set all rows hi-Z : 1
299 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
300 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
301 mcp23018_status = i2c_write( 0xFF
302 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
303 ); if (mcp23018_status) goto out;
308 // unselect on teensy
309 // Hi-Z(DDR:0, PORT:0) to unselect
310 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
311 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
312 DDRD &= ~(1<<2 | 1<<3);
313 PORTD &= ~(1<<2 | 1<<3);
318 static void select_row(uint8_t row)
321 // select on mcp23018
322 if (mcp23018_status) { // if there was an error
325 // set active row low : 0
326 // set other rows hi-Z : 1
327 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
328 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
329 mcp23018_status = i2c_write( 0xFF & ~(1<<row)
330 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
331 ); if (mcp23018_status) goto out;
337 // Output low(DDR:1, PORT:0) to select