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