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.

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