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