Keyboard firmwares for Atmel AVR and Cortex-M
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.

key_process.c 13KB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include <stdbool.h>
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <util/delay.h>
  5. #include "print.h"
  6. #include "debug.h"
  7. #include "timer.h"
  8. #include "util.h"
  9. #include "jump_bootloader.h"
  10. #include "usb_keyboard.h"
  11. #include "usb_keycodes.h"
  12. #include "usb.h"
  13. #include "layer.h"
  14. #include "matrix_skel.h"
  15. #include "keymap_skel.h"
  16. #include "key_process.h"
  17. #ifdef MOUSEKEY_ENABLE
  18. # include "mousekey.h"
  19. #endif
  20. #ifdef PS2_MOUSE_ENABLE
  21. # include "ps2_mouse.h"
  22. #endif
  23. #ifdef USB_EXTRA_ENABLE
  24. # include "usb_extra.h"
  25. #endif
  26. #ifdef USB_MOUSE_ENABLE
  27. # include "usb_mouse.h"
  28. #endif
  29. // TODO: refactoring
  30. void proc_matrix(void) {
  31. bool modified = false;
  32. uint8_t fn_bits = 0;
  33. matrix_scan();
  34. modified = matrix_is_modified();
  35. if (modified) {
  36. if (debug_matrix) matrix_print();
  37. #ifdef DEBUG_LED
  38. // LED flash for debug
  39. DEBUG_LED_CONFIG;
  40. DEBUG_LED_ON;
  41. #endif
  42. }
  43. if (matrix_has_ghost()) {
  44. // should send error?
  45. debug("matrix has ghost!!\n");
  46. return;
  47. }
  48. usb_keyboard_swap_report();
  49. usb_keyboard_clear_report();
  50. for (int row = 0; row < matrix_rows(); row++) {
  51. for (int col = 0; col < matrix_cols(); col++) {
  52. if (!matrix_is_on(row, col)) continue;
  53. uint8_t code = layer_get_keycode(row, col);
  54. if (code == KB_NO) {
  55. // do nothing
  56. } else if (IS_MOD(code)) {
  57. usb_keyboard_add_mod(code);
  58. } else if (IS_FN(code)) {
  59. fn_bits |= FN_BIT(code);
  60. }
  61. #ifdef MOUSEKEY_ENABLE
  62. else if (IS_MOUSEKEY(code)) {
  63. mousekey_decode(code);
  64. }
  65. #endif
  66. #ifdef USB_EXTRA_ENABLE
  67. // audio control & system control
  68. else if (code == KB_MUTE) {
  69. usb_extra_audio_send(AUDIO_MUTE);
  70. usb_extra_audio_send(0);
  71. _delay_ms(500);
  72. } else if (code == KB_VOLU) {
  73. usb_extra_audio_send(AUDIO_VOL_UP);
  74. usb_extra_audio_send(0);
  75. _delay_ms(200);
  76. } else if (code == KB_VOLD) {
  77. usb_extra_audio_send(AUDIO_VOL_DOWN);
  78. usb_extra_audio_send(0);
  79. _delay_ms(200);
  80. } else if (code == KB_PWR) {
  81. if (suspend && remote_wakeup) {
  82. usb_remote_wakeup();
  83. } else {
  84. usb_extra_system_send(SYSTEM_POWER_DOWN);
  85. }
  86. _delay_ms(1000);
  87. }
  88. #endif
  89. // normal key
  90. else if (IS_KEY(code)) {
  91. usb_keyboard_add_key(code);
  92. } else {
  93. debug("ignore keycode: "); debug_hex(code); debug("\n");
  94. }
  95. }
  96. }
  97. if (modified) {
  98. #ifdef DEBUG_LED
  99. // LED flash for debug
  100. DEBUG_LED_CONFIG;
  101. DEBUG_LED_OFF;
  102. #endif
  103. }
  104. layer_switching(fn_bits);
  105. // TODO: clean code
  106. // special mode for control, develop and debug
  107. if (keymap_is_special_mode(fn_bits)) {
  108. switch (usb_keyboard_get_key()) {
  109. case KB_H: // help
  110. usb_keyboard_clear_report();
  111. usb_keyboard_send();
  112. print_enable = true;
  113. print("b: jump to bootloader\n");
  114. print("d: toggle debug enable\n");
  115. print("x: toggle matrix debug\n");
  116. print("k: toggle keyboard debug\n");
  117. print("m: toggle mouse debug\n");
  118. print("p: toggle print enable\n");
  119. print("v: print version\n");
  120. print("t: print timer count\n");
  121. print("s: print status\n");
  122. print("`: toggle protcol(boot/report)\n");
  123. #ifdef USB_NKRO_ENABLE
  124. print("n: toggle USB_NKRO\n");
  125. #endif
  126. print("Backspace: clear matrix\n");
  127. print("ESC: power down/wake up\n");
  128. print("0: switch to Layer0 \n");
  129. print("1: switch to Layer1 \n");
  130. print("2: switch to Layer2 \n");
  131. print("3: switch to Layer3 \n");
  132. print("4: switch to Layer4 \n");
  133. #ifdef PS2_MOUSE_ENABLE
  134. print("[: ps2_mouse_init \n");
  135. print("]: ps2_mouse_read \n");
  136. print("\: ps2_mouse: on/off toggle \n");
  137. #endif
  138. _delay_ms(500);
  139. print_enable = false;
  140. break;
  141. case KB_BSPC:
  142. usb_keyboard_clear_report();
  143. usb_keyboard_send();
  144. matrix_init();
  145. print("clear matrix\n");
  146. _delay_ms(500);
  147. break;
  148. case KB_0:
  149. usb_keyboard_clear_report();
  150. usb_keyboard_send();
  151. print("current_layer: "); phex(current_layer); print("\n");
  152. print("default_layer: "); phex(default_layer); print("\n");
  153. current_layer = 0;
  154. default_layer = 0;
  155. print("switch to Layer0 \n");
  156. _delay_ms(500);
  157. break;
  158. case KB_1:
  159. usb_keyboard_clear_report();
  160. usb_keyboard_send();
  161. print("current_layer: "); phex(current_layer); print("\n");
  162. print("default_layer: "); phex(default_layer); print("\n");
  163. current_layer = 1;
  164. default_layer = 1;
  165. print("switch to Layer1 \n");
  166. _delay_ms(500);
  167. break;
  168. case KB_2:
  169. usb_keyboard_clear_report();
  170. usb_keyboard_send();
  171. print("current_layer: "); phex(current_layer); print("\n");
  172. print("default_layer: "); phex(default_layer); print("\n");
  173. current_layer = 2;
  174. default_layer = 2;
  175. print("switch to Layer2 \n");
  176. _delay_ms(500);
  177. break;
  178. case KB_3:
  179. usb_keyboard_clear_report();
  180. usb_keyboard_send();
  181. print("current_layer: "); phex(current_layer); print("\n");
  182. print("default_layer: "); phex(default_layer); print("\n");
  183. current_layer = 3;
  184. default_layer = 3;
  185. print("switch to Layer3 \n");
  186. _delay_ms(500);
  187. break;
  188. case KB_4:
  189. usb_keyboard_clear_report();
  190. usb_keyboard_send();
  191. print("current_layer: "); phex(current_layer); print("\n");
  192. print("default_layer: "); phex(default_layer); print("\n");
  193. current_layer = 4;
  194. default_layer = 4;
  195. print("switch to Layer4 \n");
  196. _delay_ms(500);
  197. break;
  198. #ifdef PS2_MOUSE_ENABLE
  199. case KB_LBRC:
  200. usb_keyboard_clear_report();
  201. usb_keyboard_send();
  202. print_enable = true;
  203. print("ps2_mouse_init...\n");
  204. _delay_ms(500);
  205. ps2_mouse_init();
  206. break;
  207. case KB_RBRC:
  208. usb_keyboard_clear_report();
  209. usb_keyboard_send();
  210. print_enable = true;
  211. print("ps2_mouse_read[btn x y]: ");
  212. _delay_ms(100);
  213. ps2_mouse_read();
  214. phex(ps2_mouse_btn); print(" ");
  215. phex(ps2_mouse_x); print(" ");
  216. phex(ps2_mouse_y); print("\n");
  217. print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
  218. break;
  219. case KB_BSLS:
  220. ps2_mouse_enable = !ps2_mouse_enable;
  221. print("ps2_mouse: ");
  222. if (ps2_mouse_enable)
  223. print("on");
  224. else
  225. print("off");
  226. print("\n");
  227. _delay_ms(500);
  228. break;
  229. #endif
  230. case KB_B: // bootloader
  231. usb_keyboard_clear_report();
  232. usb_keyboard_send();
  233. print_enable = true;
  234. print("jump to bootloader...\n");
  235. _delay_ms(1000);
  236. jump_bootloader(); // not return
  237. break;
  238. case KB_D: // debug all toggle
  239. usb_keyboard_clear_report();
  240. usb_keyboard_send();
  241. debug_enable = !debug_enable;
  242. if (debug_enable) {
  243. print_enable = true;
  244. print("debug enabled.\n");
  245. //debug_matrix = true;
  246. //debug_keyboard = true;
  247. //debug_mouse = true;
  248. } else {
  249. print("debug disabled.\n");
  250. print_enable = false;
  251. //debug_matrix = false;
  252. //debug_keyboard = false;
  253. //debug_mouse = false;
  254. }
  255. _delay_ms(1000);
  256. break;
  257. case KB_X: // debug matrix toggle
  258. usb_keyboard_clear_report();
  259. usb_keyboard_send();
  260. debug_matrix = !debug_matrix;
  261. if (debug_matrix)
  262. print("debug matrix enabled.\n");
  263. else
  264. print("debug matrix disabled.\n");
  265. _delay_ms(1000);
  266. break;
  267. case KB_K: // debug keyboard toggle
  268. usb_keyboard_clear_report();
  269. usb_keyboard_send();
  270. debug_keyboard = !debug_keyboard;
  271. if (debug_keyboard)
  272. print("debug keyboard enabled.\n");
  273. else
  274. print("debug keyboard disabled.\n");
  275. _delay_ms(1000);
  276. break;
  277. case KB_M: // debug mouse toggle
  278. usb_keyboard_clear_report();
  279. usb_keyboard_send();
  280. debug_mouse = !debug_mouse;
  281. if (debug_mouse)
  282. print("debug mouse enabled.\n");
  283. else
  284. print("debug mouse disabled.\n");
  285. _delay_ms(1000);
  286. break;
  287. case KB_V: // print version & information
  288. usb_keyboard_clear_report();
  289. usb_keyboard_send();
  290. print_enable = true;
  291. print(STR(DESCRIPTION) "\n");
  292. _delay_ms(1000);
  293. break;
  294. case KB_T: // print timer
  295. usb_keyboard_clear_report();
  296. usb_keyboard_send();
  297. print_enable = true;
  298. print("timer: "); phex16(timer_count); print("\n");
  299. _delay_ms(500);
  300. break;
  301. case KB_P: // print toggle
  302. usb_keyboard_clear_report();
  303. usb_keyboard_send();
  304. if (print_enable) {
  305. print("print disabled.\n");
  306. print_enable = false;
  307. } else {
  308. print_enable = true;
  309. print("print enabled.\n");
  310. }
  311. _delay_ms(1000);
  312. break;
  313. case KB_S:
  314. usb_keyboard_clear_report();
  315. usb_keyboard_send();
  316. print("UDCON: "); phex(UDCON); print("\n");
  317. print("UDIEN: "); phex(UDIEN); print("\n");
  318. print("UDINT: "); phex(UDINT); print("\n");
  319. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  320. print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
  321. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  322. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  323. #ifdef USB_MOUSE_ENABLE
  324. print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
  325. #endif
  326. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  327. _delay_ms(500);
  328. break;
  329. case KB_GRV:
  330. usb_keyboard_clear_report();
  331. usb_keyboard_send();
  332. usb_keyboard_protocol = !usb_keyboard_protocol;
  333. print("keyboard protcol: ");
  334. if (usb_keyboard_protocol) print("report"); else print("boot");
  335. print("\n");
  336. #ifdef USB_MOUSE_ENABLE
  337. usb_mouse_protocol = !usb_mouse_protocol;
  338. print("mouse protcol: ");
  339. if (usb_mouse_protocol) print("report"); else print("boot");
  340. print("\n");
  341. #endif
  342. _delay_ms(1000);
  343. break;
  344. #ifdef USB_NKRO_ENABLE
  345. case KB_N:
  346. usb_keyboard_clear_report();
  347. usb_keyboard_send();
  348. usb_keyboard_nkro = !usb_keyboard_nkro;
  349. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  350. _delay_ms(1000);
  351. break;
  352. #endif
  353. #ifdef USB_EXTRA_ENABLE
  354. case KB_ESC:
  355. usb_keyboard_clear_report();
  356. usb_keyboard_send();
  357. if (suspend && remote_wakeup) {
  358. usb_remote_wakeup();
  359. } else {
  360. usb_extra_system_send(SYSTEM_POWER_DOWN);
  361. }
  362. _delay_ms(1000);
  363. break;
  364. #endif
  365. }
  366. }
  367. if (modified) {
  368. usb_keyboard_send();
  369. }
  370. #ifdef MOUSEKEY_ENABLE
  371. mousekey_usb_send();
  372. #endif
  373. #ifdef PS2_MOUSE_ENABLE
  374. if (ps2_mouse_read() == 0)
  375. ps2_mouse_usb_send();
  376. #endif
  377. }