process_printer_bb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "process_printer.h"
  2. #include "action_util.h"
  3. bool printing_enabled = false;
  4. uint8_t character_shift = 0;
  5. #define SERIAL_PIN_DDR DDRD
  6. #define SERIAL_PIN_PORT PORTD
  7. #define SERIAL_PIN_MASK _BV(PD3)
  8. #define SERIAL_DELAY 52
  9. inline static
  10. void serial_delay(void) {
  11. _delay_us(SERIAL_DELAY);
  12. }
  13. inline static
  14. void serial_high(void) {
  15. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  16. }
  17. inline static
  18. void serial_low(void) {
  19. SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
  20. }
  21. inline static
  22. void serial_output(void) {
  23. SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
  24. }
  25. void enabled_printing() {
  26. printing_enabled = true;
  27. serial_output();
  28. serial_high();
  29. }
  30. void disable_printing() {
  31. printing_enabled = false;
  32. }
  33. uint8_t shifted_numbers[10] = {0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, 0x26, 0x2A, 0x28, 0x29};
  34. // uint8_t keycode_to_ascii[0xFF][2];
  35. // keycode_to_ascii[KC_MINS] = {0x2D, 0x5F};
  36. void print_char(char c) {
  37. uint8_t b = 8;
  38. serial_output();
  39. while( b-- ) {
  40. if(c & (1 << b)) {
  41. serial_high();
  42. } else {
  43. serial_low();
  44. }
  45. serial_delay();
  46. }
  47. }
  48. void print_string(char c[]) {
  49. for(uint8_t i = 0; i < strlen(c); i++)
  50. print_char(c[i]);
  51. }
  52. bool process_printer(uint16_t keycode, keyrecord_t *record) {
  53. if (keycode == PRINT_ON) {
  54. enabled_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. }