7 #include <util/delay.h>
10 #include "controller.h"
11 #include "matrix_skel.h"
13 // matrix is active low. (key on: 0/key off: 1)
15 // HHKB has no ghost and no bounce.
16 // row: HC4051 select input channel(0-8)
17 // PB0, PB1, PB2(A, B, C)
18 // col: LS145 select low output line(0-8)
19 // PB3, PB4, PB5, PB6(A, B, C, D)
20 // use D as ENABLE: (enable: 0/unenable: 1)
21 // key: KEY: (on: 0/ off:1)
22 // UNKNOWN: unknown whether input or output
23 // PE6,PE7(KEY, UNKNOWN)
24 #define COL_ENABLE (1<<6)
25 #define KEY_SELELCT(ROW, COL) (PORTB = COL_ENABLE|(((COL)&0x07)<<3)|((ROW)&0x07))
26 #define KEY_ENABLE (PORTB &= ~COL_ENABLE)
27 #define KEY_UNABLE (PORTB |= COL_ENABLE)
28 #define KEY_ON ((PINE&(1<<6)) ? false : true)
30 // matrix state buffer
31 static uint8_t *matrix;
32 static uint8_t *matrix_prev;
33 static uint8_t _matrix0[MATRIX_ROWS];
34 static uint8_t _matrix1[MATRIX_ROWS];
49 // this must be called once before matrix_scan.
50 void matrix_init(void)
52 // row & col output(PB0-6)
54 PORTB = KEY_SELELCT(0, 0);
55 // KEY & VALID input with pullup(PE6,7)
59 // initialize matrix state: all keys off
60 for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
61 for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
63 matrix_prev = _matrix1;
74 for (int row = 0; row < MATRIX_ROWS; row++) {
75 for (int col = 0; col < MATRIX_COLS; col++) {
76 KEY_SELELCT(row, col);
77 _delay_us(50); // from logic analyzer chart
79 _delay_us(10); // from logic analyzer chart
81 matrix[row] |= (1<<col);
83 matrix[row] &= ~(1<<col);
86 _delay_us(150); // from logic analyzer chart
92 bool matrix_is_modified(void)
94 for (int i = 0; i < MATRIX_ROWS; i++) {
95 if (matrix[i] != matrix_prev[i])
102 bool matrix_has_ghost(void)
108 bool matrix_is_on(int row, int col)
110 return (matrix[row] & (1<<col));
114 uint16_t matrix_get_row(int row)
119 void matrix_print(void)
121 print("\nr/c 01234567\n");
122 for (int row = 0; row < matrix_rows(); row++) {
123 phex(row); print(": ");
124 pbin_reverse(matrix_get_row(row));
129 int matrix_key_count(void)
132 for (int i = 0; i < MATRIX_ROWS; i++) {
133 count += bitpop(matrix[i]);