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_FREQ
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_status;
52 #ifdef DEBUG_MATRIX_FREQ
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();
77 // initialize matrix state: all keys off
78 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
80 matrix_debouncing[i] = 0;
83 #ifdef DEBUG_MATRIX_FREQ
84 matrix_timer = timer_read32();
85 matrix_scan_count = 0;
89 uint8_t matrix_scan(void)
91 #ifdef DEBUG_MATRIX_FREQ
94 uint32_t timer_now = timer_read32();
95 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
96 print("matrix scan frequency: ");
97 pdec(matrix_scan_count);
100 matrix_timer = timer_now;
101 matrix_scan_count = 0;
106 uint8_t layer = biton32(layer_state);
108 ergodox_board_led_off();
112 ergodox_left_led_1_on();
113 ergodox_left_led_2_on();
114 ergodox_left_led_3_on();
118 ergodox_left_led_1_off();
119 ergodox_left_led_2_on();
120 ergodox_left_led_3_off();
124 ergodox_left_led_1_off();
125 ergodox_left_led_2_off();
126 ergodox_left_led_3_on();
129 ergodox_board_led_on();
130 // break missed intentionally
134 ergodox_left_led_1_on();
135 ergodox_left_led_2_off();
136 ergodox_left_led_3_off();
140 ergodox_left_led_1_off();
141 ergodox_left_led_2_off();
142 ergodox_left_led_3_off();
146 mcp23018_status = ergodox_left_leds_update();
149 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
151 matrix_row_t cols = read_cols(i);
152 if (matrix_debouncing[i] != cols) {
153 matrix_debouncing[i] = cols;
155 debug("bounce!: "); debug_hex(debouncing); debug("\n");
157 debouncing = DEBOUNCE;
166 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
167 matrix[i] = matrix_debouncing[i];
175 bool matrix_is_modified(void)
177 if (debouncing) return false;
182 bool matrix_is_on(uint8_t row, uint8_t col)
184 return (matrix[row] & ((matrix_row_t)1<<col));
188 matrix_row_t matrix_get_row(uint8_t row)
193 void matrix_print(void)
195 print("\nr/c 0123456789ABCDEF\n");
196 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
197 phex(row); print(": ");
198 pbin_reverse16(matrix_get_row(row));
203 uint8_t matrix_key_count(void)
206 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
207 count += bitpop16(matrix[i]);
212 /* Column pin configuration
216 * pin: F0 F1 F4 F5 F6 F7
220 * pin: B5 B4 B3 B2 B1 B0
222 static void init_cols(void)
225 // not needed, already done as part of init_mcp23018()
228 // Input with pull-up(DDR:0, PORT:1)
229 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
230 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
233 static matrix_row_t read_cols(uint8_t row)
236 if (mcp23018_status) { // if there was an error
240 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
241 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
242 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
243 data = i2c_readNak();
250 _delay_us(30); // without this wait read unstable value.
253 (PINF&(1<<0) ? 0 : (1<<0)) |
254 (PINF&(1<<1) ? 0 : (1<<1)) |
255 (PINF&(1<<4) ? 0 : (1<<2)) |
256 (PINF&(1<<5) ? 0 : (1<<3)) |
257 (PINF&(1<<6) ? 0 : (1<<4)) |
258 (PINF&(1<<7) ? 0 : (1<<5)) ;
262 /* Row pin configuration
265 * row: 7 8 9 10 11 12 13
266 * pin: B0 B1 B2 B3 D2 D3 C6
270 * pin: A0 A1 A2 A3 A4 A5 A6
272 static void unselect_rows(void)
274 // unselect on mcp23018
275 if (mcp23018_status) { // if there was an error
278 // set all rows hi-Z : 1
279 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
280 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
281 mcp23018_status = i2c_write( 0xFF
282 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
283 ); if (mcp23018_status) goto out;
288 // unselect on teensy
289 // Hi-Z(DDR:0, PORT:0) to unselect
290 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
291 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
292 DDRD &= ~(1<<2 | 1<<3);
293 PORTD &= ~(1<<2 | 1<<3);
298 static void select_row(uint8_t row)
301 // select on mcp23018
302 if (mcp23018_status) { // if there was an error
305 // set active row low : 0
306 // set other rows hi-Z : 1
307 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
308 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
309 mcp23018_status = i2c_write( 0xFF & ~(1<<row)
310 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
311 ); if (mcp23018_status) goto out;
317 // Output low(DDR:1, PORT:0) to select