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();
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();
161 mcp23018_status = ergodox_left_leds_update();
164 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
166 matrix_row_t cols = read_cols(i);
167 if (matrix_debouncing[i] != cols) {
168 matrix_debouncing[i] = cols;
170 debug("bounce!: "); debug_hex(debouncing); debug("\n");
172 debouncing = DEBOUNCE;
181 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
182 matrix[i] = matrix_debouncing[i];
190 bool matrix_is_modified(void)
192 if (debouncing) return false;
197 bool matrix_is_on(uint8_t row, uint8_t col)
199 return (matrix[row] & ((matrix_row_t)1<<col));
203 matrix_row_t matrix_get_row(uint8_t row)
208 void matrix_print(void)
210 print("\nr/c 0123456789ABCDEF\n");
211 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
212 phex(row); print(": ");
213 pbin_reverse16(matrix_get_row(row));
218 uint8_t matrix_key_count(void)
221 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
222 count += bitpop16(matrix[i]);
227 /* Column pin configuration
231 * pin: F0 F1 F4 F5 F6 F7
235 * pin: B5 B4 B3 B2 B1 B0
237 static void init_cols(void)
240 // not needed, already done as part of init_mcp23018()
243 // Input with pull-up(DDR:0, PORT:1)
244 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
245 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
248 static matrix_row_t read_cols(uint8_t row)
251 if (mcp23018_status) { // if there was an error
255 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
256 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
257 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
258 data = i2c_readNak();
265 _delay_us(30); // without this wait read unstable value.
268 (PINF&(1<<0) ? 0 : (1<<0)) |
269 (PINF&(1<<1) ? 0 : (1<<1)) |
270 (PINF&(1<<4) ? 0 : (1<<2)) |
271 (PINF&(1<<5) ? 0 : (1<<3)) |
272 (PINF&(1<<6) ? 0 : (1<<4)) |
273 (PINF&(1<<7) ? 0 : (1<<5)) ;
277 /* Row pin configuration
280 * row: 7 8 9 10 11 12 13
281 * pin: B0 B1 B2 B3 D2 D3 C6
285 * pin: A0 A1 A2 A3 A4 A5 A6
287 static void unselect_rows(void)
289 // unselect on mcp23018
290 if (mcp23018_status) { // if there was an error
293 // set all rows hi-Z : 1
294 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
295 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
296 mcp23018_status = i2c_write( 0xFF
297 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
298 ); if (mcp23018_status) goto out;
303 // unselect on teensy
304 // Hi-Z(DDR:0, PORT:0) to unselect
305 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
306 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
307 DDRD &= ~(1<<2 | 1<<3);
308 PORTD &= ~(1<<2 | 1<<3);
313 static void select_row(uint8_t row)
316 // select on mcp23018
317 if (mcp23018_status) { // if there was an error
320 // set active row low : 0
321 // set other rows hi-Z : 1
322 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
323 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
324 mcp23018_status = i2c_write( 0xFF & ~(1<<row)
325 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
326 ); if (mcp23018_status) goto out;
332 // Output low(DDR:1, PORT:0) to select