]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - hhkb/matrix.c
ADD: keymap macro for human to read easier
[max/tmk_keyboard.git] / hhkb / matrix.c
1 /*
2  * scan matrix
3  */
4 #include <avr/io.h>
5 #include <util/delay.h>
6 #include "matrix.h"
7 #include "print.h"
8
9 // matrix is active low. (key on: 0/key off: 1)
10 //
11 // HHKB has no ghost and no bounce.
12 // row: HC4051 select input channel(0-8)
13 //      PB0, PB1, PB2(A, B, C)
14 // col: LS145 select low output line(0-8)
15 //      PB3, PB4, PB5, PB6(A, B, C, D)
16 //      use D as ENABLE: (enable: 0/unenable: 1)
17 // key: KEY: (on: 0/ off:1)
18 //      UNKNOWN: unknown whether input or output
19 //      PE6,PE7(KEY, UNKNOWN)
20 #define COL_ENABLE              (1<<6)
21 #define KEY_SELELCT(ROW, COL)   (PORTB = COL_ENABLE|(((COL)&0x07)<<3)|((ROW)&0x07))
22 #define KEY_ENABLE              (PORTB &= ~COL_ENABLE)
23 #define KEY_UNABLE              (PORTB |=  COL_ENABLE)
24 #define KEY_ON                  ((PINE&(1<<6)) ? false : true)
25
26 // matrix state buffer
27 uint8_t *matrix;
28 uint8_t *matrix_prev;
29 static uint8_t _matrix0[MATRIX_ROWS];
30 static uint8_t _matrix1[MATRIX_ROWS];
31
32
33 static bool matrix_has_ghost_in_row(int row);
34 static int bit_pop(uint8_t bits);
35
36
37 inline
38 int matrix_rows(void)
39 {
40     return MATRIX_ROWS;
41 }
42
43 inline
44 int matrix_cols(void)
45 {
46     return MATRIX_COLS;
47 }
48
49 // this must be called once before matrix_scan.
50 void matrix_init(void)
51 {
52     // row & col output(PB0-6)
53     DDRB = 0xFF;
54     PORTB = KEY_SELELCT(0, 0);
55     // KEY & VALID input with pullup(PE6,7)
56     DDRE = 0x3F;
57     PORTE = 0xC0;
58
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;
62     matrix = _matrix0;
63     matrix_prev = _matrix1;
64 }
65
66 int matrix_scan(void)
67 {
68     uint8_t *tmp;
69
70     tmp = matrix_prev;
71     matrix_prev = matrix;
72     matrix = tmp;
73
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
78             KEY_ENABLE;
79             _delay_us(10);  // from logic analyzer chart
80             if (KEY_ON) {
81                 matrix[row] |= (1<<col);
82             } else {
83                 matrix[row] &= ~(1<<col);
84             }
85             KEY_UNABLE;
86             _delay_us(150);  // from logic analyzer chart
87         }
88     }
89     return 1;
90 }
91
92 bool matrix_is_modified(void)
93 {
94     for (int i = 0; i < MATRIX_ROWS; i++) {
95         if (matrix[i] != matrix_prev[i])
96             return true;
97     }
98     return false;
99 }
100
101 inline
102 bool matrix_has_ghost(void)
103 {
104     return false;
105 }
106
107 inline
108 bool matrix_is_on(int row, int col)
109 {
110     return (matrix[row] & (1<<col));
111 }
112
113 inline
114 uint16_t matrix_get_row(int row)
115 {
116     return matrix[row];
117 }
118
119 void matrix_print(void)
120 {
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));
125         if (matrix_has_ghost_in_row(row)) {
126             print(" <ghost");
127         }
128         print("\n");
129     }
130 }
131
132 int matrix_key_count(void)
133 {
134     int count = 0;
135     for (int i = 0; i < MATRIX_ROWS; i++) {
136         count += bit_pop(matrix[i]);
137     }
138     return count;
139 }
140
141 inline
142 static bool matrix_has_ghost_in_row(int row)
143 {
144     return false;
145 }
146
147 inline
148 static int bit_pop(uint8_t bits)
149 {
150     int c;
151     for (c = 0; bits; c++)
152         bits &= bits -1;
153     return c;
154 }