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.

270 lines
5.3 KiB

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