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.

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