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.

main.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Name: main.c
  2. * Project: hid-mouse, a very simple HID example
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2008-04-07
  5. * Tabsize: 4
  6. * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: main.c 790 2010-05-30 21:00:26Z cs $
  9. */
  10. #include <stdint.h>
  11. #include <avr/io.h>
  12. #include <avr/wdt.h>
  13. #include <avr/interrupt.h> /* for sei() */
  14. #include <avr/pgmspace.h> /* required by usbdrv.h */
  15. #include <util/delay.h> /* for _delay_ms() */
  16. #include "usbdrv.h"
  17. #include "usart_print.h" /* This is also an example for using debug macros */
  18. #include "usb_keycodes.h"
  19. #include "matrix_skel.h"
  20. #include "keymap_skel.h"
  21. #include "mousekey.h"
  22. #include "keyboard.h"
  23. #include "layer.h"
  24. #include "print.h"
  25. #include "debug.h"
  26. #include "sendchar.h"
  27. #include "host.h"
  28. #include "host_vusb.h"
  29. #include "timer.h"
  30. int main(void)
  31. {
  32. wdt_enable(WDTO_1S);
  33. /* Even if you don't use the watchdog, turn it off here. On newer devices,
  34. * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
  35. */
  36. /* RESET status: all port bits are inputs without pull-up.
  37. * That's the way we need D+ and D-. Therefore we don't need any
  38. * additional hardware initialization.
  39. */
  40. odDebugInit();
  41. usbInit();
  42. print_enable = true;
  43. //debug_enable = true;
  44. timer_init();
  45. matrix_init();
  46. /* enforce re-enumeration, do this while interrupts are disabled! */
  47. usbDeviceDisconnect();
  48. uint8_t i = 0;
  49. while(--i){ /* fake USB disconnect for > 250 ms */
  50. wdt_reset();
  51. _delay_ms(1);
  52. }
  53. usbDeviceConnect();
  54. sei();
  55. uint8_t fn_bits = 0;
  56. while (1) { /* main event loop */
  57. wdt_reset();
  58. usbPoll();
  59. host_vusb_keyboard_send();
  60. matrix_scan();
  61. fn_bits = 0;
  62. keyboard_swap_report();
  63. keyboard_clear_report();
  64. mousekey_clear_report();
  65. for (int row = 0; row < matrix_rows(); row++) {
  66. for (int col = 0; col < matrix_cols(); col++) {
  67. if (!matrix_is_on(row, col)) continue;
  68. uint8_t code = layer_get_keycode(row, col);
  69. if (code == KB_NO) {
  70. // do nothing
  71. }
  72. else if (IS_MOD(code)) {
  73. keyboard_add_mod(MOD_BIT(code));
  74. }
  75. else if (IS_KEY(code)) {
  76. keyboard_add_key(code);
  77. }
  78. else if (IS_FN(code)) {
  79. fn_bits |= FN_BIT(code);
  80. }
  81. else if (IS_MOUSEKEY(code)) {
  82. mousekey_decode(code);
  83. }
  84. else {
  85. debug("ignore keycode: "); debug_hex(code); debug("\n");
  86. }
  87. }
  88. }
  89. layer_switching(fn_bits);
  90. if (matrix_is_modified()) {
  91. keyboard_send();
  92. }
  93. mousekey_send();
  94. }
  95. }