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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <avr/io.h>
  22. #include <avr/pgmspace.h>
  23. #include <avr/interrupt.h>
  24. #include <util/delay.h>
  25. //#include "usb_keys.h"
  26. #include "scan_loop.h"
  27. //#include "layouts.h"
  28. //#include "usb_keyboard.h"
  29. // TEMP INCLUDES
  30. #include "usb_keyboard_debug.h"
  31. #include "print.h"
  32. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  33. // Verified Keypress Defines
  34. #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
  35. // Error LED Control
  36. void errorLED( uint8_t on )
  37. {
  38. // Error LED On
  39. if ( on ) {
  40. PORTD |= (1<<6);
  41. }
  42. // Error LED Off
  43. else {
  44. PORTD &= ~(1<<6);
  45. }
  46. }
  47. // Initial Pin Setup
  48. // If the matrix is properly set, this function does not need to be changed
  49. inline void pinSetup(void)
  50. {
  51. // For each pin, 0=input, 1=output
  52. DDRA = 0x00;
  53. DDRB = 0x00;
  54. DDRC = 0x00;
  55. DDRD = 0x40; // LED Setup
  56. DDRE = 0x00;
  57. DDRF = 0x00;
  58. // Setting pins to either high or pull-up resistor
  59. PORTA = 0x00;
  60. PORTB = 0x00;
  61. PORTC = 0x00;
  62. PORTD = 0x40; // LED Enable
  63. PORTE = 0x00;
  64. PORTF = 0x00;
  65. }
  66. int main( void )
  67. {
  68. // Setup with 16 MHz clock
  69. CPU_PRESCALE( 0 );
  70. // Configuring Pins
  71. pinSetup();
  72. // Initialize the USB, and then wait for the host to set configuration.
  73. // If the Teensy is powered without a PC connected to the USB port,
  74. // this will wait forever.
  75. usb_init();
  76. while ( !usb_configured() ) /* wait */ ;
  77. // Wait an extra second for the PC's operating system to load drivers
  78. // and do whatever it does to actually be ready for input
  79. _delay_ms(1000);
  80. // Setup ISR Timer for flagging a kepress send to USB
  81. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  82. TCCR0A = 0x00;
  83. TCCR0B = 0x03;
  84. TIMSK0 = (1 << TOIE0);
  85. uint16_t led = 0;
  86. // Main Detection Loop
  87. while ( 1 ) {
  88. //scan_loop();
  89. // Loop should never get here (indicate error)
  90. errorLED( 1 );
  91. // HID Debug Error message
  92. erro_print("Detection loop error, this is very bad...bug report!");
  93. }
  94. }
  95. // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
  96. uint16_t sendKeypressCounter = 0;
  97. ISR( TIMER0_OVF_vect )
  98. {
  99. sendKeypressCounter++;
  100. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  101. sendKeypressCounter = 0;
  102. sendKeypresses = 1;
  103. }
  104. }