2 Copyright 2014 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/>.
21 #include <util/delay.h>
26 #include "action_util.h"
27 #include "protocol/serial.h"
49 uint8_t matrix_rows(void)
55 uint8_t matrix_cols(void)
60 void matrix_init(void)
69 static void type_key(uint16_t keycode)
71 if (keycode == 0) return;
73 uint8_t mods = keycode>>8;
74 uint8_t key = keycode&0xFF;
77 send_keyboard_report();
81 send_keyboard_report();
84 send_keyboard_report();
88 send_keyboard_report();
91 static uint16_t code2key(uint8_t code)
93 // ASCII to key combination in US laout
95 case 0x01 ... 0x08: // Ctrl-[a-z]
96 return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
97 case 0x09: return KC_TAB; // TAB(Ctrl-i)
98 case 0x0A ... 0x0C: // Ctrl-[a-z]
99 return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
100 case 0x0D: return KC_ENTER; // Enter(Ctrl-m)
101 case 0x0E ... 0x1A: // Ctrl-[a-z]
102 return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
103 case 0x1B: return KC_ESC;
104 case 0x1C: return KC_RIGHT;
105 case 0x1D: return KC_LEFT;
106 case 0x1E: return KC_UP;
107 case 0x1F: return KC_DOWN;
108 case 0x20: return KC_SPACE;
109 case 0x21: return MOD_BIT(KC_LSHIFT)<<8 | KC_1; // !
110 case 0x22: return MOD_BIT(KC_LSHIFT)<<8 | KC_QUOTE; // "
111 case 0x23: return MOD_BIT(KC_LSHIFT)<<8 | KC_3; // #
112 case 0x24: return MOD_BIT(KC_LSHIFT)<<8 | KC_4; // $
113 case 0x25: return MOD_BIT(KC_LSHIFT)<<8 | KC_5; // %
114 case 0x26: return MOD_BIT(KC_LSHIFT)<<8 | KC_7; // &
115 case 0x27: return KC_QUOTE; // '
116 case 0x28: return MOD_BIT(KC_LSHIFT)<<8 | KC_9; // (
117 case 0x29: return MOD_BIT(KC_LSHIFT)<<8 | KC_0; // )
118 case 0x2A: return MOD_BIT(KC_LSHIFT)<<8 | KC_8; // *
119 case 0x2B: return MOD_BIT(KC_LSHIFT)<<8 | KC_EQUAL; // +
120 case 0x2C: return KC_COMMA; // ,
121 case 0x2D: return KC_MINUS; // -
122 case 0x2E: return KC_DOT; // .
123 case 0x2F: return KC_SLASH; // /
124 case 0x30: return KC_0;
125 case 0x31 ... 0x39: // 1-9
126 return KC_1 + (code-0x31);
127 case 0x3A: return MOD_BIT(KC_LSHIFT)<<8 | KC_SCLN; // :
128 case 0x3B: return KC_SCLN; // ;
129 case 0x3C: return MOD_BIT(KC_LSHIFT)<<8 | KC_COMMA; // <
130 case 0x3D: return KC_EQUAL; // =
131 case 0x3E: return MOD_BIT(KC_LSHIFT)<<8 | KC_DOT; // >
132 case 0x3F: return MOD_BIT(KC_LSHIFT)<<8 | KC_SLASH; // ?
133 case 0x40: return MOD_BIT(KC_LSHIFT)<<8 | KC_2; // @
134 case 0x41 ... 0x5A: // A-Z
135 return MOD_BIT(KC_LSHIFT)<<8 | (KC_A + (code-0x41));
136 case 0x5B: return KC_LBRACKET; // [
137 case 0x5C: return KC_BSLASH; //
138 case 0x5D: return KC_RBRACKET; // ]
139 case 0x5E: return MOD_BIT(KC_LSHIFT)<<8 | KC_6; // ^
140 case 0x5F: return MOD_BIT(KC_LSHIFT)<<8 | KC_MINUS; // _
141 case 0x61 ... 0x7A: // a-z
142 return KC_A + (code-0x61);
143 case 0x7B: return MOD_BIT(KC_LSHIFT)<<8 | KC_LBRACKET; // {
144 case 0x7C: return MOD_BIT(KC_LSHIFT)<<8 | KC_BSLASH; // |
145 case 0x7D: return MOD_BIT(KC_LSHIFT)<<8 | KC_RBRACKET; // }
146 case 0x7E: return MOD_BIT(KC_LSHIFT)<<8 | KC_GRAVE; // }
147 case 0x7F: return KC_DELETE; //
152 uint8_t matrix_scan(void)
154 uint16_t code = serial_recv2();
159 print_hex8(code); print(" ");
163 type_key(code2key(code));
170 bool matrix_has_ghost(void)
176 bool matrix_is_on(uint8_t row, uint8_t col)
182 matrix_row_t matrix_get_row(uint8_t row)
187 void matrix_print(void)
189 print("\nr/c 0123456789ABCDEF\n");
190 for (uint8_t row = 0; row < matrix_rows(); row++) {
191 phex(row); print(": ");
192 pbin_reverse(matrix_get_row(row));