]> git.donarmstrong.com Git - tmk_firmware.git/blob - m0110_usb/matrix.c
Fix bug on RAW2SCAN. Add work around for M0110A.
[tmk_firmware.git] / m0110_usb / matrix.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "util.h"
27 #include "debug.h"
28 #include "host.h"
29 #include "led.h"
30 #include "m0110.h"
31 #include "matrix.h"
32
33
34 #define CAPS        0x39
35 #define CAPS_BREAK  (CAPS | 0x80)
36 #define ROW(key)    ((key)>>3&0x0F)
37 #define COL(key)    ((key)&0x07)
38
39 #define ARROW_UP_BREAK      (0x4D | 0x80)
40 #define ARROW_DOWN_BREAK    (0x48 | 0x80)
41 #define ARROW_LEFT_BREAK    (0x46 | 0x80)
42 #define ARROW_RIGHT_BREAK   (0x42 | 0x80)
43
44
45 static bool is_modified = false;
46
47 // matrix state buffer(1:on, 0:off)
48 static uint8_t *matrix;
49 static uint8_t _matrix0[MATRIX_ROWS];
50
51 #ifdef MATRIX_HAS_GHOST
52 static bool matrix_has_ghost_in_row(uint8_t row);
53 #endif
54 static void register_key(uint8_t key);
55
56
57 inline
58 uint8_t matrix_rows(void)
59 {
60     return MATRIX_ROWS;
61 }
62
63 inline
64 uint8_t matrix_cols(void)
65 {
66     return MATRIX_COLS;
67 }
68
69 void matrix_init(void)
70 {
71     print_enable = true;
72     debug_enable = true;
73     debug_matrix = false;
74     debug_keyboard = false;
75     debug_mouse = false;
76     print("debug enabled.\n");
77
78     m0110_init();
79     // initialize matrix state: all keys off
80     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
81     matrix = _matrix0;
82     return;
83 }
84
85 uint8_t matrix_scan(void)
86 {
87     uint8_t key;
88
89     is_modified = false;
90     key = m0110_recv_key();
91
92 #ifdef MATRIX_HAS_LOCKING_CAPS
93     // Send Caps key up event
94     if (matrix_is_on(ROW(CAPS), COL(CAPS))) {
95         is_modified = true;
96         register_key(CAPS_BREAK);
97     }
98 #endif
99     if (key == M0110_NULL) {
100         return 0;
101     } else if (key == M0110_ERROR) {
102         return 0;
103     } else if (key == ARROW_UP_BREAK ||
104                key == ARROW_DOWN_BREAK ||
105                key == ARROW_LEFT_BREAK ||
106                key == ARROW_RIGHT_BREAK) {
107         // WORK AROUND: exceptional handling for M0110A
108         // Unregister both Arrow key and coressponding Calc key when receive Arrow key break.
109         //
110         // Shift + Calc keys(=/*+):
111         //    Send no Shift break(0xF1) when release Calc keys. Can't be desinguished from Arrow keys.
112         //    (press: 0x71, 0x79, 0xXX      release: 0x79, 0xXX)
113         //    See m0110.c for key events and scan codes.
114         is_modified = true;
115         register_key(key);
116         register_key(key|M0110_CALC_OFFSET);
117     } else {
118 #ifdef MATRIX_HAS_LOCKING_CAPS    
119         if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
120             // CAPS LOCK on:
121             // Ignore LockingCaps key down event
122             if (key == CAPS) return 0;
123             // Convert LockingCaps key up event into down event
124             if (key == CAPS_BREAK) key = CAPS;
125         } else {
126             // CAPS LOCK off:
127             // Ignore LockingCaps key up event
128             if (key == CAPS_BREAK) return 0;
129         }
130 #endif        
131         is_modified = true;
132         register_key(key);
133     }
134
135     if (debug_enable) {
136         print("key: "); phex(key); print("\n");
137     }
138     return 1;
139 }
140
141 bool matrix_is_modified(void)
142 {
143     return is_modified;
144 }
145
146 inline
147 bool matrix_has_ghost(void)
148 {
149 #ifdef MATRIX_HAS_GHOST
150     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
151         if (matrix_has_ghost_in_row(i))
152             return true;
153     }
154 #endif
155     return false;
156 }
157
158 inline
159 bool matrix_is_on(uint8_t row, uint8_t col)
160 {
161     return (matrix[row] & (1<<col));
162 }
163
164 inline
165 uint8_t matrix_get_row(uint8_t row)
166 {
167     return matrix[row];
168 }
169
170 void matrix_print(void)
171 {
172     print("\nr/c 01234567\n");
173     for (uint8_t row = 0; row < matrix_rows(); row++) {
174         phex(row); print(": ");
175         pbin_reverse(matrix_get_row(row));
176         print("\n");
177     }
178 }
179
180 uint8_t matrix_key_count(void)
181 {
182     uint8_t count = 0;
183     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
184         count += bitpop(matrix[i]);
185     }
186     return count;
187 }
188
189 #ifdef MATRIX_HAS_GHOST
190 inline
191 static bool matrix_has_ghost_in_row(uint8_t row)
192 {
193     // no ghost exists in case less than 2 keys on
194     if (((matrix[row] - 1) & matrix[row]) == 0)
195         return false;
196
197     // ghost exists in case same state as other row
198     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
199         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
200             return true;
201     }
202     return false;
203 }
204 #endif
205
206 inline
207 static void register_key(uint8_t key)
208 {
209     if (key&0x80) {
210         matrix[ROW(key)] &= ~(1<<COL(key));
211     } else {
212         matrix[ROW(key)] |=  (1<<COL(key));
213     }
214 }