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.

main.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (C) 2011 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. // AVR Includes
  23. #include <avr/io.h>
  24. #include <avr/interrupt.h>
  25. // Project Includes
  26. #include <macro.h>
  27. #include <scan_loop.h>
  28. #include <usb_com.h>
  29. #include <led.h>
  30. #include <print.h>
  31. // ----- Defines -----
  32. // Verified Keypress Defines
  33. #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
  34. // ----- Macros -----
  35. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  36. // ----- Variables -----
  37. // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
  38. uint16_t sendKeypressCounter = 0;
  39. // Flag generated by the timer interrupt
  40. volatile uint8_t sendKeypresses = 0;
  41. // ----- Functions -----
  42. // Initial Pin Setup, make sure they are sane
  43. inline void pinSetup(void)
  44. {
  45. // For each pin, 0=input, 1=output
  46. #if defined(__AVR_AT90USB1286__)
  47. DDRA = 0x00;
  48. #endif
  49. DDRB = 0x00;
  50. DDRC = 0x00;
  51. DDRD = 0x00;
  52. DDRE = 0x00;
  53. DDRF = 0x00;
  54. // Setting pins to either high or pull-up resistor
  55. #if defined(__AVR_AT90USB1286__)
  56. PORTA = 0x00;
  57. #endif
  58. PORTB = 0x00;
  59. PORTC = 0x00;
  60. PORTD = 0x00;
  61. PORTE = 0x00;
  62. PORTF = 0x00;
  63. }
  64. int main(void)
  65. {
  66. // Setup with 16 MHz clock
  67. CPU_PRESCALE( 0 );
  68. // Configuring Pins
  69. pinSetup();
  70. init_errorLED();
  71. // Setup USB Module
  72. usb_setup();
  73. // Setup ISR Timer for flagging a kepress send to USB
  74. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  75. TCCR0A = 0x00;
  76. TCCR0B = 0x03;
  77. TIMSK0 = (1 << TOIE0);
  78. // Main Detection Loop
  79. uint8_t ledTimer = 15; // Enable LED for a short time
  80. while ( 1 )
  81. {
  82. // Setup the scanning module
  83. cli();
  84. scan_setup();
  85. sei();
  86. while ( 1 )
  87. {
  88. // Acquire Key Indices
  89. // Loop continuously until scan_loop returns 0
  90. cli();
  91. while ( scan_loop() );
  92. sei();
  93. // Run Macros over Key Indices and convert to USB Keys
  94. process_macros();
  95. // Send keypresses over USB if the ISR has signalled that it's time
  96. if ( !sendKeypresses )
  97. continue;
  98. // Send USB Data
  99. usb_send();
  100. // Clear sendKeypresses Flag
  101. sendKeypresses = 0;
  102. // Indicate Error, if valid
  103. errorLED( ledTimer );
  104. if ( ledTimer > 0 )
  105. ledTimer--;
  106. }
  107. // Loop should never get here (indicate error)
  108. ledTimer = 255;
  109. // HID Debug Error message
  110. erro_print("Detection loop error, this is very bad...bug report!");
  111. }
  112. }
  113. // USB Keyboard Data Send Counter Interrupt
  114. ISR( TIMER0_OVF_vect )
  115. {
  116. sendKeypressCounter++;
  117. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  118. sendKeypressCounter = 0;
  119. sendKeypresses = 1;
  120. }
  121. }