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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /*
  11. This example should run on most AVRs with only little changes. No special
  12. hardware resources except INT0 are used. You may have to change usbconfig.h for
  13. different I/O pins for USB. Please note that USB D+ must be the INT0 pin, or
  14. at least be connected to INT0 as well.
  15. We use VID/PID 0x046D/0xC00E which is taken from a Logitech mouse. Don't
  16. publish any hardware using these IDs! This is for demonstration only!
  17. */
  18. #include <stdint.h>
  19. #include <avr/io.h>
  20. #include <avr/wdt.h>
  21. #include <avr/interrupt.h> /* for sei() */
  22. #include <util/delay.h> /* for _delay_ms() */
  23. #include <avr/pgmspace.h> /* required by usbdrv.h */
  24. #include "usbdrv.h"
  25. #include "usart_print.h" /* This is also an example for using debug macros */
  26. #include "ps2.h"
  27. #include "usb_keycodes.h"
  28. #include "matrix_skel.h"
  29. #include "keymap_skel.h"
  30. #include "layer.h"
  31. #include "print.h"
  32. #include "debug.h"
  33. #include "sendchar.h"
  34. #include "keyboard.h"
  35. #include "timer.h"
  36. /* ------------------------------------------------------------------------- */
  37. /* ----------------------------- USB interface ----------------------------- */
  38. /* ------------------------------------------------------------------------- */
  39. int main(void)
  40. {
  41. uchar i;
  42. print_enable = true;
  43. debug_enable = true;
  44. timer_init();
  45. matrix_init();
  46. wdt_enable(WDTO_1S);
  47. /* Even if you don't use the watchdog, turn it off here. On newer devices,
  48. * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
  49. */
  50. /* RESET status: all port bits are inputs without pull-up.
  51. * That's the way we need D+ and D-. Therefore we don't need any
  52. * additional hardware initialization.
  53. */
  54. odDebugInit();
  55. DBG1(0x00, 0, 0); /* debug output: main starts */
  56. usbInit();
  57. usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
  58. i = 0;
  59. while(--i){ /* fake USB disconnect for > 250 ms */
  60. wdt_reset();
  61. _delay_ms(1);
  62. }
  63. usbDeviceConnect();
  64. sei();
  65. uint8_t fn_bits = 0;
  66. while (1) { /* main event loop */
  67. DBG1(0x02, 0, 0); /* debug output: main loop iterates */
  68. wdt_reset();
  69. usbPoll();
  70. /*
  71. static uint8_t code = 0;
  72. code = ps2_host_recv();
  73. if (code) {
  74. odDebug(0x05, &code, 1);
  75. }
  76. */
  77. matrix_scan();
  78. if (matrix_is_modified()) {
  79. //matrix_print(); // too heavy on USART
  80. fn_bits = 0;
  81. report_swap();
  82. report_clear();
  83. for (int row = 0; row < matrix_rows(); row++) {
  84. for (int col = 0; col < matrix_cols(); col++) {
  85. if (!matrix_is_on(row, col)) continue;
  86. uint8_t code = layer_get_keycode(row, col);
  87. if (code == KB_NO) {
  88. // do nothing
  89. }
  90. else if (IS_MOD(code)) {
  91. report_add_mod(MOD_BIT(code));
  92. }
  93. else if (IS_KEY(code)) {
  94. report_add_key(code);
  95. }
  96. else if (IS_FN(code)) {
  97. fn_bits |= FN_BIT(code);
  98. }
  99. else {
  100. debug("ignore keycode: "); debug_hex(code); debug("\n");
  101. }
  102. }
  103. }
  104. }
  105. layer_switching(fn_bits);
  106. if (matrix_is_modified()) {
  107. report_send();
  108. }
  109. }
  110. }