Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright (C) 2011-2013 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. // ----- Includes -----
  22. // Compiler Includes
  23. #include <Lib/MainLib.h>
  24. // Project Includes
  25. #include <macro.h>
  26. #include <scan_loop.h>
  27. #include <usb_com.h>
  28. #include <led.h>
  29. #include <print.h>
  30. // ----- Defines -----
  31. // Verified Keypress Defines
  32. #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
  33. // ----- Macros -----
  34. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  35. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  36. #endif
  37. // ----- Variables -----
  38. // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
  39. uint16_t sendKeypressCounter = 0;
  40. // Flag generated by the timer interrupt
  41. volatile uint8_t sendKeypresses = 0;
  42. // ----- Functions -----
  43. // Initial Pin Setup, make sure they are sane
  44. inline void pinSetup(void)
  45. {
  46. // AVR
  47. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  48. // For each pin, 0=input, 1=output
  49. #if defined(__AVR_AT90USB1286__)
  50. DDRA = 0x00;
  51. #endif
  52. DDRB = 0x00;
  53. DDRC = 0x00;
  54. DDRD = 0x00;
  55. DDRE = 0x00;
  56. DDRF = 0x00;
  57. // Setting pins to either high or pull-up resistor
  58. #if defined(__AVR_AT90USB1286__)
  59. PORTA = 0x00;
  60. #endif
  61. PORTB = 0x00;
  62. PORTC = 0x00;
  63. PORTD = 0x00;
  64. PORTE = 0x00;
  65. PORTF = 0x00;
  66. // ARM
  67. #elif defined(_mk20dx128_)
  68. // TODO
  69. #endif
  70. }
  71. inline void usbTimerSetup(void)
  72. {
  73. // AVR
  74. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  75. // Setup with 16 MHz clock
  76. CPU_PRESCALE( 0 );
  77. // Setup ISR Timer for flagging a kepress send to USB
  78. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  79. TCCR0A = 0x00;
  80. TCCR0B = 0x03;
  81. TIMSK0 = (1 << TOIE0);
  82. // ARM
  83. #elif defined(_mk20dx128_)
  84. // TODO
  85. #endif
  86. }
  87. int main(void)
  88. {
  89. // Configuring Pins
  90. pinSetup();
  91. init_errorLED();
  92. // Setup USB Module
  93. usb_setup();
  94. // Setup ISR Timer for flagging a kepress send to USB
  95. usbTimerSetup();
  96. // Main Detection Loop
  97. uint8_t ledTimer = 15; // Enable LED for a short time
  98. while ( 1 )
  99. {
  100. // Setup the scanning module
  101. scan_setup();
  102. while ( 1 )
  103. {
  104. // Acquire Key Indices
  105. // Loop continuously until scan_loop returns 0
  106. cli();
  107. while ( scan_loop() );
  108. sei();
  109. // Run Macros over Key Indices and convert to USB Keys
  110. process_macros();
  111. // Send keypresses over USB if the ISR has signalled that it's time
  112. if ( !sendKeypresses )
  113. continue;
  114. // Send USB Data
  115. usb_send();
  116. // Clear sendKeypresses Flag
  117. sendKeypresses = 0;
  118. // Indicate Error, if valid
  119. errorLED( ledTimer );
  120. if ( ledTimer > 0 )
  121. ledTimer--;
  122. }
  123. // Loop should never get here (indicate error)
  124. ledTimer = 255;
  125. // HID Debug Error message
  126. erro_print("Detection loop error, this is very bad...bug report!");
  127. }
  128. }
  129. // ----- Interrupts -----
  130. // AVR - USB Keyboard Data Send Counter Interrupt
  131. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  132. ISR( TIMER0_OVF_vect )
  133. {
  134. sendKeypressCounter++;
  135. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  136. sendKeypressCounter = 0;
  137. sendKeypresses = 1;
  138. }
  139. }
  140. // ARM - USB Keyboard Data Send Counter Interrupt
  141. #elif defined(_mk20dx128_)
  142. // TODO
  143. #endif