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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <util/delay.h>
  24. #include <avr/interrupt.h>
  25. // Project Includes
  26. #include <led.h>
  27. #include <print.h>
  28. #include <scan_loop.h>
  29. #include <usb_com.h>
  30. // Keymaps
  31. #include <keymap.h>
  32. #include <usb_keys.h>
  33. // Local Includes
  34. #include "macro.h"
  35. // ----- Variables -----
  36. // Keeps track of the sequence used to reflash the teensy in software
  37. static uint8_t Bootloader_ConditionSequence[] = {1,16,6,11};
  38. uint8_t Bootloader_ConditionState = 0;
  39. uint8_t Bootloader_NextPositionReady = 1;
  40. // ----- Functions -----
  41. void jumpToBootloader(void)
  42. {
  43. cli();
  44. // disable watchdog, if enabled
  45. // disable all peripherals
  46. UDCON = 1;
  47. USBCON = (1<<FRZCLK); // disable USB
  48. UCSR1B = 0;
  49. _delay_ms(5);
  50. #if defined(__AVR_AT90USB162__) // Teensy 1.0
  51. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  52. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  53. DDRB = 0; DDRC = 0; DDRD = 0;
  54. PORTB = 0; PORTC = 0; PORTD = 0;
  55. asm volatile("jmp 0x3E00");
  56. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  57. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  58. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  59. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  60. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  61. asm volatile("jmp 0x7E00");
  62. #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  63. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  64. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  65. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  66. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  67. asm volatile("jmp 0xFC00");
  68. #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  69. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  70. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  71. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  72. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  73. asm volatile("jmp 0x1FC00");
  74. #endif
  75. }
  76. // Given a sampling array, and the current number of detected keypress
  77. // Add as many keypresses from the sampling array to the USB key send array as possible.
  78. /*
  79. inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  80. {
  81. uint8_t Bootloader_KeyDetected = 0;
  82. uint8_t processed_keys = 0;
  83. // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
  84. for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
  85. {
  86. if ( keys[key] & (1 << 7) )
  87. {
  88. processed_keys++;
  89. // Display the detected scancode
  90. char tmpStr[4];
  91. int8ToStr( key, tmpStr );
  92. dPrintStrs( tmpStr, " " );
  93. // Is this a bootloader sequence key?
  94. if ( !Bootloader_KeyDetected
  95. && Bootloader_NextPositionReady
  96. && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  97. {
  98. Bootloader_KeyDetected = 1;
  99. Bootloader_NextPositionReady = 0;
  100. Bootloader_ConditionState++;
  101. }
  102. else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  103. {
  104. Bootloader_KeyDetected = 1;
  105. }
  106. // Determine if the key is a modifier
  107. uint8_t modFound = 0;
  108. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  109. // Modifier found
  110. if ( modifiers[mod] == key ) {
  111. USBKeys_Modifiers |= map[key];
  112. modFound = 1;
  113. break;
  114. }
  115. }
  116. // Modifier, already done this loop
  117. if ( modFound )
  118. continue;
  119. // Too many keys
  120. if ( USBKeys_Sent >= USBKeys_MaxSize )
  121. {
  122. info_print("USB Key limit reached");
  123. errorLED( 1 );
  124. break;
  125. }
  126. // Allow ignoring keys with 0's
  127. if ( map[key] != 0 )
  128. USBKeys_Array[USBKeys_Sent++] = map[key];
  129. }
  130. }
  131. // Boot loader sequence state handler
  132. switch ( processed_keys )
  133. {
  134. // The next bootloader key can now be pressed, if there were no keys processed
  135. case 0:
  136. Bootloader_NextPositionReady = 1;
  137. break;
  138. // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
  139. // This case purposely falls through
  140. case 1:
  141. if ( Bootloader_KeyDetected )
  142. break;
  143. default:
  144. Bootloader_ConditionState = 0;
  145. break;
  146. }
  147. // Add debug separator if keys sent via USB
  148. if ( USBKeys_Sent > 0 )
  149. print("\033[1;32m|\033[0m\n");
  150. }
  151. */
  152. // Given a list of keypresses, translate into the USB key codes
  153. // The buffer is cleared after running
  154. // If the buffer doesn't fit into the USB send array, the extra keys are dropped
  155. void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  156. {
  157. // Loop through input buffer
  158. for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
  159. {
  160. // Get the keycode from the buffer
  161. uint8_t key = KeyIndex_Buffer[index];
  162. // Determine if the key is a modifier
  163. uint8_t modFound = 0;
  164. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  165. // Modifier found
  166. if ( modifiers[mod] == key ) {
  167. USBKeys_Modifiers |= map[key];
  168. modFound = 1;
  169. break;
  170. }
  171. }
  172. // Modifier, already done this loop
  173. if ( modFound )
  174. continue;
  175. // Too many keys
  176. if ( USBKeys_Sent >= USBKeys_MaxSize )
  177. {
  178. info_print("USB Key limit reached");
  179. errorLED( 1 );
  180. break;
  181. }
  182. // Allow ignoring keys with 0's
  183. if ( map[key] != 0 )
  184. {
  185. USBKeys_Array[USBKeys_Sent++] = map[key];
  186. }
  187. else
  188. {
  189. // Key was not mapped
  190. // TODO Add dead key map
  191. char tmpStr[6];
  192. hexToStr( key, tmpStr );
  193. erro_dPrint( "Key not mapped... - ", tmpStr );
  194. errorLED( 1 );
  195. }
  196. }
  197. // Signal buffer that we've used it
  198. scan_finishedWithBuffer();
  199. }
  200. inline void process_macros(void)
  201. {
  202. // Online process macros once (if some were found), until the next USB send
  203. if ( USBKeys_Sent != 0 )
  204. return;
  205. // Query the input buffer for keypresses
  206. keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
  207. // Check for bootloader condition
  208. //if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
  209. // jumpToBootloader();
  210. }