]>
git.gir.st - tmk_keyboard.git/blob - m0110_usb/matrix.c
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <util/delay.h>
35 #define CAPS_BREAK (CAPS | 0x80)
36 #define ROW(key) ((key)>>3&0x0F)
37 #define COL(key) ((key)&0x07)
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)
45 static bool is_modified
= false;
47 // matrix state buffer(1:on, 0:off)
48 static uint8_t *matrix
;
49 static uint8_t _matrix0
[MATRIX_ROWS
];
51 #ifdef MATRIX_HAS_GHOST
52 static bool matrix_has_ghost_in_row(uint8_t row
);
54 static void register_key(uint8_t key
);
58 uint8_t matrix_rows(void)
64 uint8_t matrix_cols(void)
69 void matrix_init(void)
74 debug_keyboard
= false;
76 print("debug enabled.\n");
79 // initialize matrix state: all keys off
80 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
85 uint8_t matrix_scan(void)
90 key
= m0110_recv_key();
92 #ifdef MATRIX_HAS_LOCKING_CAPS
93 // Send Caps key up event
94 if (matrix_is_on(ROW(CAPS
), COL(CAPS
))) {
96 register_key(CAPS_BREAK
);
99 if (key
== M0110_NULL
) {
101 } else if (key
== M0110_ERROR
) {
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.
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.
116 register_key(key
|M0110_CALC_OFFSET
);
118 #ifdef MATRIX_HAS_LOCKING_CAPS
119 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
)) {
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
;
127 // Ignore LockingCaps key up event
128 if (key
== CAPS_BREAK
) return 0;
136 print("key: "); phex(key
); print("\n");
141 bool matrix_is_modified(void)
147 bool matrix_has_ghost(void)
149 #ifdef MATRIX_HAS_GHOST
150 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
151 if (matrix_has_ghost_in_row(i
))
159 bool matrix_is_on(uint8_t row
, uint8_t col
)
161 return (matrix
[row
] & (1<<col
));
165 uint8_t matrix_get_row(uint8_t row
)
170 void matrix_print(void)
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
));
180 uint8_t matrix_key_count(void)
183 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
184 count
+= bitpop(matrix
[i
]);
189 #ifdef MATRIX_HAS_GHOST
191 static bool matrix_has_ghost_in_row(uint8_t row
)
193 // no ghost exists in case less than 2 keys on
194 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
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
])
207 static void register_key(uint8_t key
)
210 matrix
[ROW(key
)] &= ~(1<<COL(key
));
212 matrix
[ROW(key
)] |= (1<<COL(key
));