]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - adb_usb/matrix.c
fixed adb_usb to comply new API.
[max/tmk_keyboard.git] / adb_usb / 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 "debug.h"
11 #include "adb.h"
12 #include "matrix.h"
13
14
15 #if (MATRIX_COLS > 16)
16 #   error "MATRIX_COLS must not exceed 16"
17 #endif
18 #if (MATRIX_ROWS > 255)
19 #   error "MATRIX_ROWS must not exceed 255"
20 #endif
21
22
23 static bool _matrix_is_modified = false;
24
25 // matrix state buffer(1:on, 0:off)
26 #if (MATRIX_COLS <= 8)
27 static uint8_t *matrix;
28 static uint8_t _matrix0[MATRIX_ROWS];
29 #else
30 static uint16_t *matrix;
31 static uint16_t _matrix0[MATRIX_ROWS];
32 #endif
33
34 #ifdef MATRIX_HAS_GHOST
35 static bool matrix_has_ghost_in_row(uint8_t row);
36 #endif
37 static void _register_key(uint8_t key);
38
39
40 inline
41 uint8_t matrix_rows(void)
42 {
43     return MATRIX_ROWS;
44 }
45
46 inline
47 uint8_t matrix_cols(void)
48 {
49     return MATRIX_COLS;
50 }
51
52 void matrix_init(void)
53 {
54     adb_host_init();
55
56     // initialize matrix state: all keys off
57     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
58     matrix = _matrix0;
59
60     print_enable = true;
61     debug_enable = true;
62     debug_matrix = true;
63     debug_keyboard = true;
64     debug_mouse = true;
65     print("debug enabled.\n");
66     return;
67 }
68
69 uint8_t matrix_scan(void)
70 {
71     uint16_t codes;
72     uint8_t key0, key1;
73
74     _matrix_is_modified = false;
75     codes = adb_host_kbd_recv();
76     key0 = codes>>8;
77     key1 = codes&0xFF;
78
79     if (codes == 0) {                           // no keys
80         return 0;
81     } else if (key0 == 0xFF && key1 != 0xFF) {  // error
82         return codes&0xFF;
83     } else {
84         _matrix_is_modified = true;
85         _register_key(key0);
86         if (key1 != 0xFF)       // key1 is 0xFF when no second key.
87             _register_key(key1);
88     }
89
90     if (debug_matrix && matrix_is_modified()) {
91         print("adb_host_kbd_recv: "); phex16(codes); print("\n");
92     }
93     return 1;
94 }
95
96 bool matrix_is_modified(void)
97 {
98     return _matrix_is_modified;
99 }
100
101 inline
102 bool matrix_has_ghost(void)
103 {
104 #ifdef MATRIX_HAS_GHOST
105     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
106         if (matrix_has_ghost_in_row(i))
107             return true;
108     }
109 #endif
110     return false;
111 }
112
113 inline
114 bool matrix_is_on(uint8_t row, uint8_t col)
115 {
116     return (matrix[row] & (1<<col));
117 }
118
119 inline
120 #if (MATRIX_COLS <= 8)
121 uint8_t matrix_get_row(uint8_t row)
122 #else
123 uint16_t matrix_get_row(uint8_t row)
124 #endif
125 {
126     return matrix[row];
127 }
128
129 void matrix_print(void)
130 {
131 #if (MATRIX_COLS <= 8)
132     print("\nr/c 01234567\n");
133 #else
134     print("\nr/c 0123456789ABCDEF\n");
135 #endif
136     for (uint8_t row = 0; row < matrix_rows(); row++) {
137         phex(row); print(": ");
138 #if (MATRIX_COLS <= 8)
139         pbin_reverse(matrix_get_row(row));
140 #else
141         pbin_reverse16(matrix_get_row(row));
142 #endif
143 #ifdef MATRIX_HAS_GHOST
144         if (matrix_has_ghost_in_row(row)) {
145             print(" <ghost");
146         }
147 #endif
148         print("\n");
149     }
150 }
151
152 uint8_t matrix_key_count(void)
153 {
154     uint8_t count = 0;
155     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
156 #if (MATRIX_COLS <= 8)
157         count += bitpop(matrix[i]);
158 #else
159         count += bitpop16(matrix[i]);
160 #endif
161     }
162     return count;
163 }
164
165 #ifdef MATRIX_HAS_GHOST
166 inline
167 static bool matrix_has_ghost_in_row(uint8_t row)
168 {
169     // no ghost exists in case less than 2 keys on
170     if (((matrix[row] - 1) & matrix[row]) == 0)
171         return false;
172
173     // ghost exists in case same state as other row
174     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
175         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
176             return true;
177     }
178     return false;
179 }
180 #endif
181
182 inline
183 static void _register_key(uint8_t key)
184 {
185     uint8_t col, row;
186     col = key&0x07;
187     row = (key>>3)&0x0F;
188     if (key&0x80) {
189         matrix[row] &= ~(1<<col);
190     } else {
191         matrix[row] |=  (1<<col);
192     }
193 }