send_string.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include <ctype.h>
  17. #include "quantum.h"
  18. #include "send_string.h"
  19. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  20. # include "audio.h"
  21. # ifndef BELL_SOUND
  22. # define BELL_SOUND TERMINAL_SOUND
  23. # endif
  24. float bell_song[][2] = SONG(BELL_SOUND);
  25. #endif
  26. // clang-format off
  27. /* Bit-Packed look-up table to convert an ASCII character to whether
  28. * [Shift] needs to be sent with the keycode.
  29. */
  30. __attribute__((weak)) const uint8_t ascii_to_shift_lut[16] PROGMEM = {
  31. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  32. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  33. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  34. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  35. KCLUT_ENTRY(0, 1, 1, 1, 1, 1, 1, 0),
  36. KCLUT_ENTRY(1, 1, 1, 1, 0, 0, 0, 0),
  37. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  38. KCLUT_ENTRY(0, 0, 1, 0, 1, 0, 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, 1, 1, 1, 1, 1),
  42. KCLUT_ENTRY(1, 1, 1, 0, 0, 0, 1, 1),
  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, 0, 0, 0, 0, 0),
  46. KCLUT_ENTRY(0, 0, 0, 1, 1, 1, 1, 0)
  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. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  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. };
  69. /* Bit-Packed look-up table to convert an ASCII character to whether
  70. * [Space] needs to be sent after the keycode
  71. */
  72. __attribute__((weak)) const uint8_t ascii_to_dead_lut[16] PROGMEM = {
  73. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  74. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  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. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  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. };
  90. /* Look-up table to convert an ASCII character to a keycode.
  91. */
  92. __attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = {
  93. // NUL SOH STX ETX EOT ENQ ACK BEL
  94. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  95. // BS TAB LF VT FF CR SO SI
  96. KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  97. // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
  98. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  99. // CAN EM SUB ESC FS GS RS US
  100. XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  101. // ! " # $ % & '
  102. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  103. // ( ) * + , - . /
  104. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  105. // 0 1 2 3 4 5 6 7
  106. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  107. // 8 9 : ; < = > ?
  108. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  109. // @ A B C D E F G
  110. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  111. // H I J K L M N O
  112. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  113. // P Q R S T U V W
  114. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  115. // X Y Z [ \ ] ^ _
  116. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  117. // ` a b c d e f g
  118. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  119. // h i j k l m n o
  120. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  121. // p q r s t u v w
  122. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  123. // x y z { | } ~ DEL
  124. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  125. };
  126. // clang-format on
  127. // Note: we bit-pack in "reverse" order to optimize loading
  128. #define PGM_LOADBIT(mem, pos) ((pgm_read_byte(&((mem)[(pos) / 8])) >> ((pos) % 8)) & 0x01)
  129. void send_string(const char *string) {
  130. send_string_with_delay(string, 0);
  131. }
  132. void send_string_with_delay(const char *string, uint8_t interval) {
  133. while (1) {
  134. char ascii_code = *string;
  135. if (!ascii_code) break;
  136. if (ascii_code == SS_QMK_PREFIX) {
  137. ascii_code = *(++string);
  138. if (ascii_code == SS_TAP_CODE) {
  139. // tap
  140. uint8_t keycode = *(++string);
  141. tap_code(keycode);
  142. } else if (ascii_code == SS_DOWN_CODE) {
  143. // down
  144. uint8_t keycode = *(++string);
  145. register_code(keycode);
  146. } else if (ascii_code == SS_UP_CODE) {
  147. // up
  148. uint8_t keycode = *(++string);
  149. unregister_code(keycode);
  150. } else if (ascii_code == SS_DELAY_CODE) {
  151. // delay
  152. int ms = 0;
  153. uint8_t keycode = *(++string);
  154. while (isdigit(keycode)) {
  155. ms *= 10;
  156. ms += keycode - '0';
  157. keycode = *(++string);
  158. }
  159. while (ms--)
  160. wait_ms(1);
  161. }
  162. } else {
  163. send_char(ascii_code);
  164. }
  165. ++string;
  166. // interval
  167. {
  168. uint8_t ms = interval;
  169. while (ms--)
  170. wait_ms(1);
  171. }
  172. }
  173. }
  174. void send_char(char ascii_code) {
  175. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  176. if (ascii_code == '\a') { // BEL
  177. PLAY_SONG(bell_song);
  178. return;
  179. }
  180. #endif
  181. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  182. bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code);
  183. bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code);
  184. bool is_dead = PGM_LOADBIT(ascii_to_dead_lut, (uint8_t)ascii_code);
  185. if (is_shifted) {
  186. register_code(KC_LEFT_SHIFT);
  187. }
  188. if (is_altgred) {
  189. register_code(KC_RIGHT_ALT);
  190. }
  191. tap_code(keycode);
  192. if (is_altgred) {
  193. unregister_code(KC_RIGHT_ALT);
  194. }
  195. if (is_shifted) {
  196. unregister_code(KC_LEFT_SHIFT);
  197. }
  198. if (is_dead) {
  199. tap_code(KC_SPACE);
  200. }
  201. }
  202. void send_dword(uint32_t number) {
  203. send_word(number >> 16);
  204. send_word(number & 0xFFFFUL);
  205. }
  206. void send_word(uint16_t number) {
  207. send_byte(number >> 8);
  208. send_byte(number & 0xFF);
  209. }
  210. void send_byte(uint8_t number) {
  211. send_nibble(number >> 4);
  212. send_nibble(number & 0xF);
  213. }
  214. void send_nibble(uint8_t number) {
  215. switch (number & 0xF) {
  216. case 0 ... 9:
  217. send_char(number + '0');
  218. break;
  219. case 10 ... 15:
  220. send_char(number - 10 + 'a');
  221. break;
  222. }
  223. }
  224. void tap_random_base64(void) {
  225. #if defined(__AVR_ATmega32U4__)
  226. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  227. #else
  228. uint8_t key = rand() % 64;
  229. #endif
  230. switch (key) {
  231. case 0 ... 25:
  232. send_char(key + 'A');
  233. break;
  234. case 26 ... 51:
  235. send_char(key - 26 + 'a');
  236. break;
  237. case 52:
  238. send_char('0');
  239. break;
  240. case 53 ... 61:
  241. send_char(key - 53 + '1');
  242. break;
  243. case 62:
  244. send_char('+');
  245. break;
  246. case 63:
  247. send_char('/');
  248. break;
  249. }
  250. }
  251. #if defined(__AVR__)
  252. void send_string_P(const char *string) {
  253. send_string_with_delay_P(string, 0);
  254. }
  255. void send_string_with_delay_P(const char *string, uint8_t interval) {
  256. while (1) {
  257. char ascii_code = pgm_read_byte(string);
  258. if (!ascii_code) break;
  259. if (ascii_code == SS_QMK_PREFIX) {
  260. ascii_code = pgm_read_byte(++string);
  261. if (ascii_code == SS_TAP_CODE) {
  262. // tap
  263. uint8_t keycode = pgm_read_byte(++string);
  264. tap_code(keycode);
  265. } else if (ascii_code == SS_DOWN_CODE) {
  266. // down
  267. uint8_t keycode = pgm_read_byte(++string);
  268. register_code(keycode);
  269. } else if (ascii_code == SS_UP_CODE) {
  270. // up
  271. uint8_t keycode = pgm_read_byte(++string);
  272. unregister_code(keycode);
  273. } else if (ascii_code == SS_DELAY_CODE) {
  274. // delay
  275. int ms = 0;
  276. uint8_t keycode = pgm_read_byte(++string);
  277. while (isdigit(keycode)) {
  278. ms *= 10;
  279. ms += keycode - '0';
  280. keycode = pgm_read_byte(++string);
  281. }
  282. while (ms--)
  283. wait_ms(1);
  284. }
  285. } else {
  286. send_char(ascii_code);
  287. }
  288. ++string;
  289. // interval
  290. {
  291. uint8_t ms = interval;
  292. while (ms--)
  293. wait_ms(1);
  294. }
  295. }
  296. }
  297. #endif