You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
11 KiB

  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 *str) { send_string_with_delay(str, 0); }
  130. void send_string_P(const char *str) { send_string_with_delay_P(str, 0); }
  131. void send_string_with_delay(const char *str, uint8_t interval) {
  132. while (1) {
  133. char ascii_code = *str;
  134. if (!ascii_code) break;
  135. if (ascii_code == SS_QMK_PREFIX) {
  136. ascii_code = *(++str);
  137. if (ascii_code == SS_TAP_CODE) {
  138. // tap
  139. uint8_t keycode = *(++str);
  140. tap_code(keycode);
  141. } else if (ascii_code == SS_DOWN_CODE) {
  142. // down
  143. uint8_t keycode = *(++str);
  144. register_code(keycode);
  145. } else if (ascii_code == SS_UP_CODE) {
  146. // up
  147. uint8_t keycode = *(++str);
  148. unregister_code(keycode);
  149. } else if (ascii_code == SS_DELAY_CODE) {
  150. // delay
  151. int ms = 0;
  152. uint8_t keycode = *(++str);
  153. while (isdigit(keycode)) {
  154. ms *= 10;
  155. ms += keycode - '0';
  156. keycode = *(++str);
  157. }
  158. while (ms--) wait_ms(1);
  159. }
  160. } else {
  161. send_char(ascii_code);
  162. }
  163. ++str;
  164. // interval
  165. {
  166. uint8_t ms = interval;
  167. while (ms--) wait_ms(1);
  168. }
  169. }
  170. }
  171. void send_string_with_delay_P(const char *str, uint8_t interval) {
  172. while (1) {
  173. char ascii_code = pgm_read_byte(str);
  174. if (!ascii_code) break;
  175. if (ascii_code == SS_QMK_PREFIX) {
  176. ascii_code = pgm_read_byte(++str);
  177. if (ascii_code == SS_TAP_CODE) {
  178. // tap
  179. uint8_t keycode = pgm_read_byte(++str);
  180. tap_code(keycode);
  181. } else if (ascii_code == SS_DOWN_CODE) {
  182. // down
  183. uint8_t keycode = pgm_read_byte(++str);
  184. register_code(keycode);
  185. } else if (ascii_code == SS_UP_CODE) {
  186. // up
  187. uint8_t keycode = pgm_read_byte(++str);
  188. unregister_code(keycode);
  189. } else if (ascii_code == SS_DELAY_CODE) {
  190. // delay
  191. int ms = 0;
  192. uint8_t keycode = pgm_read_byte(++str);
  193. while (isdigit(keycode)) {
  194. ms *= 10;
  195. ms += keycode - '0';
  196. keycode = pgm_read_byte(++str);
  197. }
  198. while (ms--) wait_ms(1);
  199. }
  200. } else {
  201. send_char(ascii_code);
  202. }
  203. ++str;
  204. // interval
  205. {
  206. uint8_t ms = interval;
  207. while (ms--) wait_ms(1);
  208. }
  209. }
  210. }
  211. void send_char(char ascii_code) {
  212. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  213. if (ascii_code == '\a') { // BEL
  214. PLAY_SONG(bell_song);
  215. return;
  216. }
  217. #endif
  218. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  219. bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code);
  220. bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code);
  221. bool is_dead = PGM_LOADBIT(ascii_to_dead_lut, (uint8_t)ascii_code);
  222. if (is_shifted) {
  223. register_code(KC_LSFT);
  224. }
  225. if (is_altgred) {
  226. register_code(KC_RALT);
  227. }
  228. tap_code(keycode);
  229. if (is_altgred) {
  230. unregister_code(KC_RALT);
  231. }
  232. if (is_shifted) {
  233. unregister_code(KC_LSFT);
  234. }
  235. if (is_dead) {
  236. tap_code(KC_SPACE);
  237. }
  238. }
  239. void send_dword(uint32_t number) {
  240. send_word(number >> 16);
  241. send_word(number & 0xFFFFUL);
  242. }
  243. void send_word(uint16_t number) {
  244. send_byte(number >> 8);
  245. send_byte(number & 0xFF);
  246. }
  247. void send_byte(uint8_t number) {
  248. send_nibble(number >> 4);
  249. send_nibble(number & 0xF);
  250. }
  251. void send_nibble(uint8_t number) {
  252. switch (number & 0xF) {
  253. case 0 ... 9:
  254. send_char(number + '0');
  255. break;
  256. case 10 ... 15:
  257. send_char(number - 10 + 'a');
  258. break;
  259. }
  260. }
  261. void tap_random_base64(void) {
  262. #if defined(__AVR_ATmega32U4__)
  263. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  264. #else
  265. uint8_t key = rand() % 64;
  266. #endif
  267. switch (key) {
  268. case 0 ... 25:
  269. send_char(key + 'A');
  270. break;
  271. case 26 ... 51:
  272. send_char(key - 26 + 'a');
  273. break;
  274. case 52:
  275. send_char('0');
  276. break;
  277. case 53 ... 61:
  278. send_char(key - 53 + '1');
  279. break;
  280. case 62:
  281. send_char('+');
  282. break;
  283. case 63:
  284. send_char('/');
  285. break;
  286. }
  287. }