]> git.gir.st - tmk_keyboard.git/blob - hhkb/matrix.c
output previous key state on TP1684 when scaning matrix.
[tmk_keyboard.git] / hhkb / matrix.c
1 /*
2 * scan matrix
3 */
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <avr/io.h>
7 #include <util/delay.h>
8 #include "print.h"
9 #include "util.h"
10 #include "controller.h"
11 #include "matrix_skel.h"
12
13 // matrix is active low. (key on: 0/key off: 1)
14 //
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 // KEY_PREV: (on: 1/ off: 0)
23 // PE6,PE7(KEY, KEY_PREV)
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_STATE (PINE&(1<<6))
29 #define KEY_PREV_ON (PORTE |= (1<<7))
30 #define KEY_PREV_OFF (PORTE &= ~(1<<7))
31
32 // matrix state buffer
33 static uint8_t *matrix;
34 static uint8_t *matrix_prev;
35 static uint8_t _matrix0[MATRIX_ROWS];
36 static uint8_t _matrix1[MATRIX_ROWS];
37
38
39 inline
40 int matrix_rows(void)
41 {
42 return MATRIX_ROWS;
43 }
44
45 inline
46 int matrix_cols(void)
47 {
48 return MATRIX_COLS;
49 }
50
51 // this must be called once before matrix_scan.
52 void matrix_init(void)
53 {
54 // row & col output(PB0-6)
55 DDRB = 0xFF;
56 PORTB = KEY_SELELCT(0, 0);
57 // KEY: input with pullup(PE6)
58 // KEY_PREV: output(PE7)
59 DDRE = 0xBF;
60 PORTE = 0x40;
61
62 // initialize matrix state: all keys off
63 for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
64 for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
65 matrix = _matrix0;
66 matrix_prev = _matrix1;
67 }
68
69 int matrix_scan(void)
70 {
71 uint8_t *tmp;
72
73 tmp = matrix_prev;
74 matrix_prev = matrix;
75 matrix = tmp;
76
77 for (int row = 0; row < MATRIX_ROWS; row++) {
78 for (int col = 0; col < MATRIX_COLS; col++) {
79 KEY_SELELCT(row, col);
80 _delay_us(40); // from logic analyzer chart
81 if (matrix_prev[row] & (1<<col)) {
82 KEY_PREV_ON;
83 }
84 _delay_us(7); // from logic analyzer chart
85 KEY_ENABLE;
86 _delay_us(10); // from logic analyzer chart
87 if (KEY_STATE) {
88 matrix[row] &= ~(1<<col);
89 } else {
90 matrix[row] |= (1<<col);
91 }
92 KEY_PREV_OFF;
93 KEY_UNABLE;
94 _delay_us(150); // from logic analyzer chart
95 }
96 }
97 return 1;
98 }
99
100 bool matrix_is_modified(void)
101 {
102 for (int i = 0; i < MATRIX_ROWS; i++) {
103 if (matrix[i] != matrix_prev[i])
104 return true;
105 }
106 return false;
107 }
108
109 inline
110 bool matrix_has_ghost(void)
111 {
112 return false;
113 }
114
115 inline
116 bool matrix_is_on(int row, int col)
117 {
118 return (matrix[row] & (1<<col));
119 }
120
121 inline
122 uint16_t matrix_get_row(int row)
123 {
124 return matrix[row];
125 }
126
127 void matrix_print(void)
128 {
129 print("\nr/c 01234567\n");
130 for (int row = 0; row < matrix_rows(); row++) {
131 phex(row); print(": ");
132 pbin_reverse(matrix_get_row(row));
133 print("\n");
134 }
135 }
136
137 int matrix_key_count(void)
138 {
139 int count = 0;
140 for (int i = 0; i < MATRIX_ROWS; i++) {
141 count += bitpop(matrix[i]);
142 }
143 return count;
144 }
Imprint / Impressum