process_printer.c 7.8 KB

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