diff options
Diffstat (limited to 'quantum')
-rw-r--r-- | quantum/quantum.c | 232 | ||||
-rw-r--r-- | quantum/quantum.h | 27 | ||||
-rw-r--r-- | quantum/send_string.c | 252 | ||||
-rw-r--r-- | quantum/send_string.h | 47 |
4 files changed, 300 insertions, 258 deletions
diff --git a/quantum/quantum.c b/quantum/quantum.c index db99e80fa..80c4e8f00 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c | |||
@@ -14,7 +14,6 @@ | |||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | #include <ctype.h> | ||
18 | #include "quantum.h" | 17 | #include "quantum.h" |
19 | 18 | ||
20 | #ifdef BLUETOOTH_ENABLE | 19 | #ifdef BLUETOOTH_ENABLE |
@@ -325,237 +324,6 @@ bool process_record_quantum(keyrecord_t *record) { | |||
325 | return process_action_kb(record); | 324 | return process_action_kb(record); |
326 | } | 325 | } |
327 | 326 | ||
328 | // clang-format off | ||
329 | |||
330 | /* Bit-Packed look-up table to convert an ASCII character to whether | ||
331 | * [Shift] needs to be sent with the keycode. | ||
332 | */ | ||
333 | __attribute__((weak)) const uint8_t ascii_to_shift_lut[16] PROGMEM = { | ||
334 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
335 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
336 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
337 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
338 | |||
339 | KCLUT_ENTRY(0, 1, 1, 1, 1, 1, 1, 0), | ||
340 | KCLUT_ENTRY(1, 1, 1, 1, 0, 0, 0, 0), | ||
341 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
342 | KCLUT_ENTRY(0, 0, 1, 0, 1, 0, 1, 1), | ||
343 | KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1), | ||
344 | KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1), | ||
345 | KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1), | ||
346 | KCLUT_ENTRY(1, 1, 1, 0, 0, 0, 1, 1), | ||
347 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
348 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
349 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
350 | KCLUT_ENTRY(0, 0, 0, 1, 1, 1, 1, 0), | ||
351 | }; | ||
352 | |||
353 | /* Bit-Packed look-up table to convert an ASCII character to whether | ||
354 | * [AltGr] needs to be sent with the keycode. | ||
355 | */ | ||
356 | __attribute__((weak)) const uint8_t ascii_to_altgr_lut[16] PROGMEM = { | ||
357 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
358 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
359 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
360 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
361 | |||
362 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
363 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
364 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
365 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
366 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
367 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
368 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
369 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
370 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
371 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
372 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
373 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
374 | }; | ||
375 | |||
376 | /* Bit-Packed look-up table to convert an ASCII character to whether | ||
377 | * [Space] needs to be sent after the keycode | ||
378 | */ | ||
379 | __attribute__((weak)) const uint8_t ascii_to_dead_lut[16] PROGMEM = { | ||
380 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
381 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
382 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
383 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
384 | |||
385 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
386 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
387 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
388 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
389 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
390 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
391 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
392 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
393 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
394 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
395 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
396 | KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0), | ||
397 | }; | ||
398 | |||
399 | /* Look-up table to convert an ASCII character to a keycode. | ||
400 | */ | ||
401 | __attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = { | ||
402 | // NUL SOH STX ETX EOT ENQ ACK BEL | ||
403 | XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, | ||
404 | // BS TAB LF VT FF CR SO SI | ||
405 | KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, | ||
406 | // DLE DC1 DC2 DC3 DC4 NAK SYN ETB | ||
407 | XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, | ||
408 | // CAN EM SUB ESC FS GS RS US | ||
409 | XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, | ||
410 | |||
411 | // ! " # $ % & ' | ||
412 | KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT, | ||
413 | // ( ) * + , - . / | ||
414 | KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH, | ||
415 | // 0 1 2 3 4 5 6 7 | ||
416 | KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, | ||
417 | // 8 9 : ; < = > ? | ||
418 | KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH, | ||
419 | // @ A B C D E F G | ||
420 | KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G, | ||
421 | // H I J K L M N O | ||
422 | KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O, | ||
423 | // P Q R S T U V W | ||
424 | KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W, | ||
425 | // X Y Z [ \ ] ^ _ | ||
426 | KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS, | ||
427 | // ` a b c d e f g | ||
428 | KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G, | ||
429 | // h i j k l m n o | ||
430 | KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O, | ||
431 | // p q r s t u v w | ||
432 | KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W, | ||
433 | // x y z { | } ~ DEL | ||
434 | KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL | ||
435 | }; | ||
436 | |||
437 | // clang-format on | ||
438 | |||
439 | // Note: we bit-pack in "reverse" order to optimize loading | ||
440 | #define PGM_LOADBIT(mem, pos) ((pgm_read_byte(&((mem)[(pos) / 8])) >> ((pos) % 8)) & 0x01) | ||
441 | |||
442 | void send_string(const char *str) { send_string_with_delay(str, 0); } | ||
443 | |||
444 | void send_string_P(const char *str) { send_string_with_delay_P(str, 0); } | ||
445 | |||
446 | void send_string_with_delay(const char *str, uint8_t interval) { | ||
447 | while (1) { | ||
448 | char ascii_code = *str; | ||
449 | if (!ascii_code) break; | ||
450 | if (ascii_code == SS_QMK_PREFIX) { | ||
451 | ascii_code = *(++str); | ||
452 | if (ascii_code == SS_TAP_CODE) { | ||
453 | // tap | ||
454 | uint8_t keycode = *(++str); | ||
455 | tap_code(keycode); | ||
456 | } else if (ascii_code == SS_DOWN_CODE) { | ||
457 | // down | ||
458 | uint8_t keycode = *(++str); | ||
459 | register_code(keycode); | ||
460 | } else if (ascii_code == SS_UP_CODE) { | ||
461 | // up | ||
462 | uint8_t keycode = *(++str); | ||
463 | unregister_code(keycode); | ||
464 | } else if (ascii_code == SS_DELAY_CODE) { | ||
465 | // delay | ||
466 | int ms = 0; | ||
467 | uint8_t keycode = *(++str); | ||
468 | while (isdigit(keycode)) { | ||
469 | ms *= 10; | ||
470 | ms += keycode - '0'; | ||
471 | keycode = *(++str); | ||
472 | } | ||
473 | while (ms--) wait_ms(1); | ||
474 | } | ||
475 | } else { | ||
476 | send_char(ascii_code); | ||
477 | } | ||
478 | ++str; | ||
479 | // interval | ||
480 | { | ||
481 | uint8_t ms = interval; | ||
482 | while (ms--) wait_ms(1); | ||
483 | } | ||
484 | } | ||
485 | } | ||
486 | |||
487 | void send_string_with_delay_P(const char *str, uint8_t interval) { | ||
488 | while (1) { | ||
489 | char ascii_code = pgm_read_byte(str); | ||
490 | if (!ascii_code) break; | ||
491 | if (ascii_code == SS_QMK_PREFIX) { | ||
492 | ascii_code = pgm_read_byte(++str); | ||
493 | if (ascii_code == SS_TAP_CODE) { | ||
494 | // tap | ||
495 | uint8_t keycode = pgm_read_byte(++str); | ||
496 | tap_code(keycode); | ||
497 | } else if (ascii_code == SS_DOWN_CODE) { | ||
498 | // down | ||
499 | uint8_t keycode = pgm_read_byte(++str); | ||
500 | register_code(keycode); | ||
501 | } else if (ascii_code == SS_UP_CODE) { | ||
502 | // up | ||
503 | uint8_t keycode = pgm_read_byte(++str); | ||
504 | unregister_code(keycode); | ||
505 | } else if (ascii_code == SS_DELAY_CODE) { | ||
506 | // delay | ||
507 | int ms = 0; | ||
508 | uint8_t keycode = pgm_read_byte(++str); | ||
509 | while (isdigit(keycode)) { | ||
510 | ms *= 10; | ||
511 | ms += keycode - '0'; | ||
512 | keycode = pgm_read_byte(++str); | ||
513 | } | ||
514 | while (ms--) wait_ms(1); | ||
515 | } | ||
516 | } else { | ||
517 | send_char(ascii_code); | ||
518 | } | ||
519 | ++str; | ||
520 | // interval | ||
521 | { | ||
522 | uint8_t ms = interval; | ||
523 | while (ms--) wait_ms(1); | ||
524 | } | ||
525 | } | ||
526 | } | ||
527 | |||
528 | void send_char(char ascii_code) { | ||
529 | #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL) | ||
530 | if (ascii_code == '\a') { // BEL | ||
531 | PLAY_SONG(bell_song); | ||
532 | return; | ||
533 | } | ||
534 | #endif | ||
535 | |||
536 | uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]); | ||
537 | bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code); | ||
538 | bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code); | ||
539 | bool is_dead = PGM_LOADBIT(ascii_to_dead_lut, (uint8_t)ascii_code); | ||
540 | |||
541 | if (is_shifted) { | ||
542 | register_code(KC_LSFT); | ||
543 | } | ||
544 | if (is_altgred) { | ||
545 | register_code(KC_RALT); | ||
546 | } | ||
547 | tap_code(keycode); | ||
548 | if (is_altgred) { | ||
549 | unregister_code(KC_RALT); | ||
550 | } | ||
551 | if (is_shifted) { | ||
552 | unregister_code(KC_LSFT); | ||
553 | } | ||
554 | if (is_dead) { | ||
555 | tap_code(KC_SPACE); | ||
556 | } | ||
557 | } | ||
558 | |||
559 | void set_single_persistent_default_layer(uint8_t default_layer) { | 327 | void set_single_persistent_default_layer(uint8_t default_layer) { |
560 | #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS) | 328 | #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS) |
561 | PLAY_SONG(default_layer_songs[default_layer]); | 329 | PLAY_SONG(default_layer_songs[default_layer]); |
diff --git a/quantum/quantum.h b/quantum/quantum.h index 36a983d57..b1600dd72 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h | |||
@@ -61,7 +61,7 @@ | |||
61 | #include "action_util.h" | 61 | #include "action_util.h" |
62 | #include "action_tapping.h" | 62 | #include "action_tapping.h" |
63 | #include "print.h" | 63 | #include "print.h" |
64 | #include "send_string_keycodes.h" | 64 | #include "send_string.h" |
65 | #include "suspend.h" | 65 | #include "suspend.h" |
66 | #include <stddef.h> | 66 | #include <stddef.h> |
67 | #include <stdlib.h> | 67 | #include <stdlib.h> |
@@ -231,31 +231,6 @@ extern layer_state_t layer_state; | |||
231 | # define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) | 231 | # define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) |
232 | 232 | ||
233 | #endif | 233 | #endif |
234 | #define SEND_STRING(string) send_string_P(PSTR(string)) | ||
235 | #define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval) | ||
236 | |||
237 | // Look-Up Tables (LUTs) to convert ASCII character to keycode sequence. | ||
238 | extern const uint8_t ascii_to_keycode_lut[128]; | ||
239 | extern const uint8_t ascii_to_shift_lut[16]; | ||
240 | extern const uint8_t ascii_to_altgr_lut[16]; | ||
241 | extern const uint8_t ascii_to_dead_lut[16]; | ||
242 | // clang-format off | ||
243 | #define KCLUT_ENTRY(a, b, c, d, e, f, g, h) \ | ||
244 | ( ((a) ? 1 : 0) << 0 \ | ||
245 | | ((b) ? 1 : 0) << 1 \ | ||
246 | | ((c) ? 1 : 0) << 2 \ | ||
247 | | ((d) ? 1 : 0) << 3 \ | ||
248 | | ((e) ? 1 : 0) << 4 \ | ||
249 | | ((f) ? 1 : 0) << 5 \ | ||
250 | | ((g) ? 1 : 0) << 6 \ | ||
251 | | ((h) ? 1 : 0) << 7 ) | ||
252 | // clang-format on | ||
253 | |||
254 | void send_string(const char *str); | ||
255 | void send_string_with_delay(const char *str, uint8_t interval); | ||
256 | void send_string_P(const char *str); | ||
257 | void send_string_with_delay_P(const char *str, uint8_t interval); | ||
258 | void send_char(char ascii_code); | ||
259 | 234 | ||
260 | // For tri-layer | 235 | // For tri-layer |
261 | void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3); | 236 | void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3); |
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 | |||
137 | void send_string(const char *str) { send_string_with_delay(str, 0); } | ||
138 | |||
139 | void send_string_P(const char *str) { send_string_with_delay_P(str, 0); } | ||
140 | |||
141 | void 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 | |||
182 | void 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 | |||
223 | void 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 | ||
diff --git a/quantum/send_string.h b/quantum/send_string.h new file mode 100644 index 000000000..570522eb0 --- /dev/null +++ b/quantum/send_string.h | |||
@@ -0,0 +1,47 @@ | |||
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 <stdint.h> | ||
18 | |||
19 | #include "progmem.h" | ||
20 | #include "send_string_keycodes.h" | ||
21 | |||
22 | #define SEND_STRING(string) send_string_P(PSTR(string)) | ||
23 | #define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval) | ||
24 | |||
25 | // Look-Up Tables (LUTs) to convert ASCII character to keycode sequence. | ||
26 | extern const uint8_t ascii_to_keycode_lut[128]; | ||
27 | extern const uint8_t ascii_to_shift_lut[16]; | ||
28 | extern const uint8_t ascii_to_altgr_lut[16]; | ||
29 | extern const uint8_t ascii_to_dead_lut[16]; | ||
30 | |||
31 | // clang-format off | ||
32 | #define KCLUT_ENTRY(a, b, c, d, e, f, g, h) \ | ||
33 | ( ((a) ? 1 : 0) << 0 \ | ||
34 | | ((b) ? 1 : 0) << 1 \ | ||
35 | | ((c) ? 1 : 0) << 2 \ | ||
36 | | ((d) ? 1 : 0) << 3 \ | ||
37 | | ((e) ? 1 : 0) << 4 \ | ||
38 | | ((f) ? 1 : 0) << 5 \ | ||
39 | | ((g) ? 1 : 0) << 6 \ | ||
40 | | ((h) ? 1 : 0) << 7 ) | ||
41 | // clang-format on | ||
42 | |||
43 | void send_string(const char *str); | ||
44 | void send_string_with_delay(const char *str, uint8_t interval); | ||
45 | void send_string_P(const char *str); | ||
46 | void send_string_with_delay_P(const char *str, uint8_t interval); | ||
47 | void send_char(char ascii_code); | ||