aboutsummaryrefslogtreecommitdiff
path: root/converter/usb_usb/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/usb_usb/matrix.c')
-rw-r--r--converter/usb_usb/matrix.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/converter/usb_usb/matrix.c b/converter/usb_usb/matrix.c
new file mode 100644
index 000000000..1cfecde20
--- /dev/null
+++ b/converter/usb_usb/matrix.c
@@ -0,0 +1,133 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <stdint.h>
19#include <stdbool.h>
20#include "usb_hid.h"
21#include "usb_keycodes.h"
22#include "util.h"
23#include "print.h"
24#include "debug.h"
25#include "matrix.h"
26
27/* KEY CODE to Matrix
28 *
29 * HID keycode(1 byte):
30 * Higher 5 bits indicates ROW and lower 3 bits COL.
31 *
32 * 7 6 5 4 3 2 1 0
33 * +---------------+
34 * | ROW | COL |
35 * +---------------+
36 *
37 * Matrix space(32 * 8):
38 * 01234567
39 * 0 +--------+
40 * : | |
41 * : | |
42 * 31 +--------+
43 */
44#define ROW_MASK 0xF8
45#define COL_MASK 0x07
46#define CODE(row, col) (((row) << 3) | (col))
47#define ROW(code) (((code) & ROW_MASK) >> 3)
48#define COL(code) ((code) & COL_MASK)
49#define ROW_BITS(code) (1 << COL(code))
50
51
52uint8_t matrix_rows(void) { return MATRIX_ROWS; }
53uint8_t matrix_cols(void) { return MATRIX_COLS; }
54void matrix_init(void) {}
55bool matrix_has_ghost(void) { return false; }
56
57static bool matrix_is_mod =false;
58
59uint8_t matrix_scan(void) {
60 static uint16_t last_time_stamp = 0;
61
62 if (last_time_stamp != usb_hid_time_stamp) {
63 last_time_stamp = usb_hid_time_stamp;
64 matrix_is_mod = true;
65 } else {
66 matrix_is_mod = false;
67 }
68 return 1;
69}
70
71bool matrix_is_modified(void) {
72
73 return matrix_is_mod;
74}
75
76bool matrix_is_on(uint8_t row, uint8_t col) {
77 uint8_t code = CODE(row, col);
78
79 if (IS_MOD(code)) {
80 if (usb_hid_keyboard_report.mods & ROW_BITS(code)) {
81 return true;
82 }
83 }
84 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
85 if (usb_hid_keyboard_report.keys[i] == code) {
86 return true;
87 }
88 }
89 return false;
90}
91
92uint8_t matrix_get_row(uint8_t row) {
93 uint8_t row_bits = 0;
94
95 if (IS_MOD(CODE(row, 0)) && usb_hid_keyboard_report.mods) {
96 row_bits |= usb_hid_keyboard_report.mods;
97 }
98
99 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
100 if (IS_ANY(usb_hid_keyboard_report.keys[i])) {
101 if (row == ROW(usb_hid_keyboard_report.keys[i])) {
102 row_bits |= ROW_BITS(usb_hid_keyboard_report.keys[i]);
103 }
104 }
105 }
106 return row_bits;
107}
108
109uint8_t matrix_key_count(void) {
110 uint8_t count = 0;
111
112 count += bitpop(usb_hid_keyboard_report.mods);
113 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
114 if (IS_ANY(usb_hid_keyboard_report.keys[i])) {
115 count++;
116 }
117 }
118 return count;
119}
120
121void matrix_print(void) {
122 print("\nr/c 01234567\n");
123 for (uint8_t row = 0; row < matrix_rows(); row++) {
124 phex(row); print(": ");
125 pbin_reverse(matrix_get_row(row));
126#ifdef MATRIX_HAS_GHOST
127 if (matrix_has_ghost_in_row(row)) {
128 print(" <ghost");
129 }
130#endif
131 print("\n");
132 }
133}