aboutsummaryrefslogtreecommitdiff
path: root/quantum/send_string.c
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-03-01 07:11:39 +1100
committerGitHub <noreply@github.com>2021-03-01 07:11:39 +1100
commit1a7f2c8f453d4da6ed8622e52d5b788bc6dad480 (patch)
tree1c65ce60dd863023173c4f18b2c78ba592703d54 /quantum/send_string.c
parente6905805bb5e272187ae9a780ab1d345249efbc3 (diff)
downloadqmk_firmware-1a7f2c8f453d4da6ed8622e52d5b788bc6dad480.tar.gz
qmk_firmware-1a7f2c8f453d4da6ed8622e52d5b788bc6dad480.zip
Extract sendstring into its own compilation unit (#12060)
* Extract sendstring into its own compilation unit * License headers? * Put this include in the header
Diffstat (limited to 'quantum/send_string.c')
-rw-r--r--quantum/send_string.c252
1 files changed, 252 insertions, 0 deletions
diff --git a/quantum/send_string.c b/quantum/send_string.c
new file mode 100644
index 000000000..0e8902ca3
--- /dev/null
+++ b/quantum/send_string.c
@@ -0,0 +1,252 @@
1/* Copyright 2021
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <ctype.h>
18
19#include "quantum.h"
20
21#include "send_string.h"
22
23// clang-format off
24
25/* Bit-Packed look-up table to convert an ASCII character to whether
26 * [Shift] needs to be sent with the keycode.
27 */
28__attribute__((weak)) const uint8_t ascii_to_shift_lut[16] PROGMEM = {
29 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
30 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
31 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
32 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
33
34 KCLUT_ENTRY(0, 1, 1, 1, 1, 1, 1, 0),
35 KCLUT_ENTRY(1, 1, 1, 1, 0, 0, 0, 0),
36 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
37 KCLUT_ENTRY(0, 0, 1, 0, 1, 0, 1, 1),
38 KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
39 KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
40 KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
41 KCLUT_ENTRY(1, 1, 1, 0, 0, 0, 1, 1),
42 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
43 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
44 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
45 KCLUT_ENTRY(0, 0, 0, 1, 1, 1, 1, 0)
46};
47
48/* Bit-Packed look-up table to convert an ASCII character to whether
49 * [AltGr] needs to be sent with the keycode.
50 */
51__attribute__((weak)) const uint8_t ascii_to_altgr_lut[16] PROGMEM = {
52 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
53 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
54 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
55 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
56
57 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
58 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
59 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
60 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
61 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
62 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
63 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
64 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
65 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
66 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
67 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
68 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0)
69};
70
71/* Bit-Packed look-up table to convert an ASCII character to whether
72 * [Space] needs to be sent after the keycode
73 */
74__attribute__((weak)) const uint8_t ascii_to_dead_lut[16] PROGMEM = {
75 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
76 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
77 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
78 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
79
80 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
81 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
82 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
83 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
84 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
85 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
86 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
87 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
88 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
89 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
90 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
91 KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0)
92};
93
94/* Look-up table to convert an ASCII character to a keycode.
95 */
96__attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = {
97 // NUL SOH STX ETX EOT ENQ ACK BEL
98 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
99 // BS TAB LF VT FF CR SO SI
100 KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
101 // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
102 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
103 // CAN EM SUB ESC FS GS RS US
104 XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
105
106 // ! " # $ % & '
107 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
108 // ( ) * + , - . /
109 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
110 // 0 1 2 3 4 5 6 7
111 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
112 // 8 9 : ; < = > ?
113 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
114 // @ A B C D E F G
115 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
116 // H I J K L M N O
117 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
118 // P Q R S T U V W
119 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
120 // X Y Z [ \ ] ^ _
121 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
122 // ` a b c d e f g
123 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
124 // h i j k l m n o
125 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
126 // p q r s t u v w
127 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
128 // x y z { | } ~ DEL
129 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
130};
131
132// clang-format on
133
134// Note: we bit-pack in "reverse" order to optimize loading
135#define PGM_LOADBIT(mem, pos) ((pgm_read_byte(&((mem)[(pos) / 8])) >> ((pos) % 8)) & 0x01)
136
137void send_string(const char *str) { send_string_with_delay(str, 0); }
138
139void send_string_P(const char *str) { send_string_with_delay_P(str, 0); }
140
141void send_string_with_delay(const char *str, uint8_t interval) {
142 while (1) {
143 char ascii_code = *str;
144 if (!ascii_code) break;
145 if (ascii_code == SS_QMK_PREFIX) {
146 ascii_code = *(++str);
147 if (ascii_code == SS_TAP_CODE) {
148 // tap
149 uint8_t keycode = *(++str);
150 tap_code(keycode);
151 } else if (ascii_code == SS_DOWN_CODE) {
152 // down
153 uint8_t keycode = *(++str);
154 register_code(keycode);
155 } else if (ascii_code == SS_UP_CODE) {
156 // up
157 uint8_t keycode = *(++str);
158 unregister_code(keycode);
159 } else if (ascii_code == SS_DELAY_CODE) {
160 // delay
161 int ms = 0;
162 uint8_t keycode = *(++str);
163 while (isdigit(keycode)) {
164 ms *= 10;
165 ms += keycode - '0';
166 keycode = *(++str);
167 }
168 while (ms--) wait_ms(1);
169 }
170 } else {
171 send_char(ascii_code);
172 }
173 ++str;
174 // interval
175 {
176 uint8_t ms = interval;
177 while (ms--) wait_ms(1);
178 }
179 }
180}
181
182void send_string_with_delay_P(const char *str, uint8_t interval) {
183 while (1) {
184 char ascii_code = pgm_read_byte(str);
185 if (!ascii_code) break;
186 if (ascii_code == SS_QMK_PREFIX) {
187 ascii_code = pgm_read_byte(++str);
188 if (ascii_code == SS_TAP_CODE) {
189 // tap
190 uint8_t keycode = pgm_read_byte(++str);
191 tap_code(keycode);
192 } else if (ascii_code == SS_DOWN_CODE) {
193 // down
194 uint8_t keycode = pgm_read_byte(++str);
195 register_code(keycode);
196 } else if (ascii_code == SS_UP_CODE) {
197 // up
198 uint8_t keycode = pgm_read_byte(++str);
199 unregister_code(keycode);
200 } else if (ascii_code == SS_DELAY_CODE) {
201 // delay
202 int ms = 0;
203 uint8_t keycode = pgm_read_byte(++str);
204 while (isdigit(keycode)) {
205 ms *= 10;
206 ms += keycode - '0';
207 keycode = pgm_read_byte(++str);
208 }
209 while (ms--) wait_ms(1);
210 }
211 } else {
212 send_char(ascii_code);
213 }
214 ++str;
215 // interval
216 {
217 uint8_t ms = interval;
218 while (ms--) wait_ms(1);
219 }
220 }
221}
222
223void send_char(char ascii_code) {
224#if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
225 if (ascii_code == '\a') { // BEL
226 PLAY_SONG(bell_song);
227 return;
228 }
229#endif
230
231 uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
232 bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code);
233 bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code);
234 bool is_dead = PGM_LOADBIT(ascii_to_dead_lut, (uint8_t)ascii_code);
235
236 if (is_shifted) {
237 register_code(KC_LSFT);
238 }
239 if (is_altgred) {
240 register_code(KC_RALT);
241 }
242 tap_code(keycode);
243 if (is_altgred) {
244 unregister_code(KC_RALT);
245 }
246 if (is_shifted) {
247 unregister_code(KC_LSFT);
248 }
249 if (is_dead) {
250 tap_code(KC_SPACE);
251 }
252} \ No newline at end of file