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.

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