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.

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