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.

macro.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  79. {
  80. uint8_t Bootloader_KeyDetected = 0;
  81. uint8_t processed_keys = 0;
  82. // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
  83. for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
  84. {
  85. if ( keys[key] & (1 << 7) )
  86. {
  87. processed_keys++;
  88. // Display the detected scancode
  89. char tmpStr[4];
  90. int8ToStr( key, tmpStr );
  91. dPrintStrs( tmpStr, " " );
  92. // Is this a bootloader sequence key?
  93. if ( !Bootloader_KeyDetected
  94. && Bootloader_NextPositionReady
  95. && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  96. {
  97. Bootloader_KeyDetected = 1;
  98. Bootloader_NextPositionReady = 0;
  99. Bootloader_ConditionState++;
  100. }
  101. else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  102. {
  103. Bootloader_KeyDetected = 1;
  104. }
  105. // Determine if the key is a modifier
  106. uint8_t modFound = 0;
  107. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  108. // Modifier found
  109. if ( modifiers[mod] == key ) {
  110. USBKeys_Modifiers |= map[key];
  111. modFound = 1;
  112. break;
  113. }
  114. }
  115. // Modifier, already done this loop
  116. if ( modFound )
  117. continue;
  118. // Too many keys
  119. if ( USBKeys_Sent >= USBKeys_MaxSize )
  120. {
  121. info_print("USB Key limit reached");
  122. errorLED( 1 );
  123. break;
  124. }
  125. // Allow ignoring keys with 0's
  126. if ( map[key] != 0 )
  127. USBKeys_Array[USBKeys_Sent++] = map[key];
  128. }
  129. }
  130. // Boot loader sequence state handler
  131. switch ( processed_keys )
  132. {
  133. // The next bootloader key can now be pressed, if there were no keys processed
  134. case 0:
  135. Bootloader_NextPositionReady = 1;
  136. break;
  137. // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
  138. // This case purposely falls through
  139. case 1:
  140. if ( Bootloader_KeyDetected )
  141. break;
  142. default:
  143. Bootloader_ConditionState = 0;
  144. break;
  145. }
  146. // Add debug separator if keys sent via USB
  147. if ( USBKeys_Sent > 0 )
  148. print("\033[1;32m|\033[0m\n");
  149. }
  150. inline void process_macros(void)
  151. {
  152. // Online process macros once (if some were found), until the next USB send
  153. if ( USBKeys_Sent != 0 )
  154. return;
  155. // Debounce Sampling Array to USB Data Array
  156. keyPressDetection( KeyIndex_Array, KeyIndex_Size, MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
  157. // Check for bootloader condition
  158. if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
  159. jumpToBootloader();
  160. }