process_printer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright 2016 Jack Humbert
  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 "process_printer.h"
  17. #include "action_util.h"
  18. #include "uart.h"
  19. bool printing_enabled = false;
  20. uint8_t character_shift = 0;
  21. void enable_printing(void) {
  22. printing_enabled = true;
  23. uart_init(19200);
  24. }
  25. void disable_printing(void) { printing_enabled = false; }
  26. uint8_t shifted_numbers[10] = {0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, 0x26, 0x2A, 0x28, 0x29};
  27. // uint8_t keycode_to_ascii[0xFF][2];
  28. // keycode_to_ascii[KC_MINUS] = {0x2D, 0x5F};
  29. void print_char(char c) {
  30. USB_Disable();
  31. uart_write(c);
  32. USB_Init();
  33. }
  34. void print_string(char c[]) {
  35. for (uint8_t i = 0; i < strlen(c); i++) print_char(c[i]);
  36. }
  37. void print_box_string(const char text[]) {
  38. size_t len = strlen(text);
  39. char out[len * 3 + 8];
  40. out[0] = 0xDA;
  41. for (uint8_t i = 0; i < len; i++) {
  42. out[i + 1] = 0xC4;
  43. }
  44. out[len + 1] = 0xBF;
  45. out[len + 2] = '\n';
  46. out[len + 3] = 0xB3;
  47. for (uint8_t i = 0; i < len; i++) {
  48. out[len + 4 + i] = text[i];
  49. }
  50. out[len * 2 + 4] = 0xB3;
  51. out[len * 2 + 5] = '\n';
  52. out[len * 2 + 6] = 0xC0;
  53. for (uint8_t i = 0; i < len; i++) {
  54. out[len * 2 + 7 + i] = 0xC4;
  55. }
  56. out[len * 3 + 7] = 0xD9;
  57. out[len * 3 + 8] = '\n';
  58. print_string(out);
  59. }
  60. bool process_printer(uint16_t keycode, keyrecord_t *record) {
  61. if (keycode == PRINT_ON) {
  62. enable_printing();
  63. return false;
  64. }
  65. if (keycode == PRINT_OFF) {
  66. disable_printing();
  67. return false;
  68. }
  69. if (printing_enabled) {
  70. switch (keycode) {
  71. case KC_EXLM ... KC_RPRN:
  72. case KC_UNDS:
  73. case KC_PLUS:
  74. case KC_LCBR:
  75. case KC_RCBR:
  76. case KC_PIPE:
  77. case KC_TILD:
  78. keycode &= 0xFF;
  79. case KC_LEFT_SHIFT:
  80. case KC_RIGHT_SHIFT:
  81. if (record->event.pressed) {
  82. character_shift++;
  83. } else {
  84. character_shift--;
  85. }
  86. return false;
  87. break;
  88. }
  89. switch (keycode) {
  90. case KC_F1:
  91. if (record->event.pressed) {
  92. print_box_string("This is a line of text!");
  93. }
  94. return false;
  95. case KC_ESCAPE:
  96. if (record->event.pressed) {
  97. print_char(0x1B);
  98. }
  99. return false;
  100. break;
  101. case KC_SPACE:
  102. if (record->event.pressed) {
  103. print_char(0x20);
  104. }
  105. return false;
  106. break;
  107. case KC_A ... KC_Z:
  108. if (record->event.pressed) {
  109. if (character_shift) {
  110. print_char(0x41 + (keycode - KC_A));
  111. } else {
  112. print_char(0x61 + (keycode - KC_A));
  113. }
  114. }
  115. return false;
  116. break;
  117. case KC_1 ... KC_0:
  118. if (record->event.pressed) {
  119. if (character_shift) {
  120. print_char(shifted_numbers[keycode - KC_1]);
  121. } else {
  122. print_char(0x30 + ((keycode - KC_1 + 1) % 10));
  123. }
  124. }
  125. return false;
  126. break;
  127. case KC_ENTER:
  128. if (record->event.pressed) {
  129. if (character_shift) {
  130. print_char(0x0C);
  131. } else {
  132. print_char(0x0A);
  133. }
  134. }
  135. return false;
  136. break;
  137. case KC_BACKSPACE:
  138. if (record->event.pressed) {
  139. if (character_shift) {
  140. print_char(0x18);
  141. } else {
  142. print_char(0x1A);
  143. }
  144. }
  145. return false;
  146. break;
  147. case KC_DOT:
  148. if (record->event.pressed) {
  149. if (character_shift) {
  150. print_char(0x3E);
  151. } else {
  152. print_char(0x2E);
  153. }
  154. }
  155. return false;
  156. break;
  157. case KC_COMMA:
  158. if (record->event.pressed) {
  159. if (character_shift) {
  160. print_char(0x3C);
  161. } else {
  162. print_char(0x2C);
  163. }
  164. }
  165. return false;
  166. break;
  167. case KC_SLASH:
  168. if (record->event.pressed) {
  169. if (character_shift) {
  170. print_char(0x3F);
  171. } else {
  172. print_char(0x2F);
  173. }
  174. }
  175. return false;
  176. break;
  177. case KC_QUOTE:
  178. if (record->event.pressed) {
  179. if (character_shift) {
  180. print_char(0x22);
  181. } else {
  182. print_char(0x27);
  183. }
  184. }
  185. return false;
  186. break;
  187. case KC_GRAVE:
  188. if (record->event.pressed) {
  189. if (character_shift) {
  190. print_char(0x7E);
  191. } else {
  192. print_char(0x60);
  193. }
  194. }
  195. return false;
  196. break;
  197. case KC_MINUS:
  198. if (record->event.pressed) {
  199. if (character_shift) {
  200. print_char(0x5F);
  201. } else {
  202. print_char(0x2D);
  203. }
  204. }
  205. return false;
  206. break;
  207. case KC_EQUAL:
  208. if (record->event.pressed) {
  209. if (character_shift) {
  210. print_char(0x2B);
  211. } else {
  212. print_char(0x3D);
  213. }
  214. }
  215. return false;
  216. break;
  217. case KC_LEFT_BRACKET:
  218. if (record->event.pressed) {
  219. if (character_shift) {
  220. print_char(0x7B);
  221. } else {
  222. print_char(0x5B);
  223. }
  224. }
  225. return false;
  226. break;
  227. case KC_RIGHT_BRACKET:
  228. if (record->event.pressed) {
  229. if (character_shift) {
  230. print_char(0x7D);
  231. } else {
  232. print_char(0x5D);
  233. }
  234. }
  235. return false;
  236. break;
  237. case KC_BACKSLASH:
  238. if (record->event.pressed) {
  239. if (character_shift) {
  240. print_char(0x7C);
  241. } else {
  242. print_char(0x5C);
  243. }
  244. }
  245. return false;
  246. break;
  247. }
  248. }
  249. return true;
  250. }