]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - matrix_hhkb.c
change keymap.
[max/tmk_keyboard.git] / matrix_hhkb.c
1 /*
2  * scan matrix
3  */
4 #include <avr/io.h>
5 #include <util/delay.h>
6 #include "keymap.h"
7 #include "matrix.h"
8 #include "print.h"
9
10 // matrix is active low. (key on: 0/key off: 1)
11 //
12 // HHKB has no ghost and no bounce.
13 // row: HC4051 select input channel(0-8)
14 //      PB0, PB1, PB2(A, B, C)
15 // col: LS145 select low output line(0-8)
16 //      PB3, PB4, PB5, PB6(A, B, C, D)
17 //      use D as ENABLE: (enable: 0/unenable: 1)
18 // key: KEY: (on: 0/ off:1)
19 //      UNKNOWN: unknown whether input or output
20 //      PE6,PE7(KEY, UNKNOWN)
21 #define COL_ENABLE              (1<<6)
22 #define KEY_SELELCT(ROW, COL)   (PORTB = COL_ENABLE|(((COL)&0x07)<<3)|((ROW)&0x07))
23 #define KEY_ENABLE              (PORTB &= ~COL_ENABLE)
24 #define KEY_UNABLE              (PORTB |=  COL_ENABLE)
25 #define KEY_ON                  ((PINE&(1<<6)) ? false : true)
26
27 // matrix state buffer
28 uint8_t *matrix;
29 uint8_t *matrix_prev;
30 static uint8_t _matrix0[MATRIX_ROWS];
31 static uint8_t _matrix1[MATRIX_ROWS];
32
33
34 // this must be called once before matrix_scan.
35 void matrix_init(void)
36 {
37     // row & col output(PB0-6)
38     DDRB = 0xFF;
39     PORTB = KEY_SELELCT(0, 0);
40     // KEY & VALID input with pullup(PE6,7)
41     DDRE = 0x3F;
42     PORTE = 0xC0;
43
44     // initialize matrix state: all keys off
45     for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0xFF;
46     for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0xFF;
47     matrix = _matrix0;
48     matrix_prev = _matrix1;
49 }
50
51 uint8_t matrix_scan(void)
52 {
53     uint8_t state;
54     uint8_t *tmp;
55
56     tmp = matrix_prev;
57     matrix_prev = matrix;
58     matrix = tmp;
59
60     for (int row = 0; row < MATRIX_ROWS; row++) {
61         for (int col = 0; col < MATRIX_COLS; col++) {
62 //print("scan: "); phex(row); phex(col); print("\n");
63             KEY_SELELCT(row, col);
64             _delay_us(50);  // from logic analyzer chart
65             KEY_ENABLE;
66             _delay_us(10);  // from logic analyzer chart
67 //print("PINE: "); phex(PINE); print("\n");
68             if (KEY_ON) {
69                 matrix[row] &= ~(1<<col);
70             } else {
71                 matrix[row] |= (1<<col);
72             }
73             KEY_UNABLE;
74             _delay_us(150);  // from logic analyzer chart
75         }
76     }
77     return 1;
78 }
79
80 bool matrix_is_modified(void) {
81     for (int i=0; i <MATRIX_ROWS; i++) {
82         if (matrix[i] != matrix_prev[i])
83             return true;
84     }
85     return false;
86 }
87
88 bool matrix_has_ghost(void) {
89     return false;
90 }
91
92 bool matrix_has_ghost_in_row(uint8_t row) {
93     return false;
94 }