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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // Compiler Includes
  23. #include <Lib/MacroLib.h>
  24. // Project Includes
  25. #include <led.h>
  26. #include <print.h>
  27. #include <scan_loop.h>
  28. #include <usb_com.h>
  29. // Keymaps
  30. #include <keymap.h>
  31. #include <usb_keys.h>
  32. // Local Includes
  33. #include "macro.h"
  34. // ----- Variables -----
  35. // Keeps track of the sequence used to reflash the teensy in software
  36. static uint8_t Bootloader_ConditionSequence[] = {1,16,6,11};
  37. uint8_t Bootloader_ConditionState = 0;
  38. uint8_t Bootloader_NextPositionReady = 1;
  39. // ----- Functions -----
  40. void jumpToBootloader(void)
  41. {
  42. cli();
  43. // disable watchdog, if enabled
  44. // disable all peripherals
  45. UDCON = 1;
  46. USBCON = (1<<FRZCLK); // disable USB
  47. UCSR1B = 0;
  48. _delay_ms(5);
  49. #if defined(__AVR_AT90USB162__) // Teensy 1.0
  50. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  51. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  52. DDRB = 0; DDRC = 0; DDRD = 0;
  53. PORTB = 0; PORTC = 0; PORTD = 0;
  54. asm volatile("jmp 0x3E00");
  55. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  56. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  57. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  58. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  59. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  60. asm volatile("jmp 0x7E00");
  61. #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  62. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  63. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  64. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  65. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  66. asm volatile("jmp 0xFC00");
  67. #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  68. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  69. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  70. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  71. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  72. asm volatile("jmp 0x1FC00");
  73. #endif
  74. }
  75. // Given a sampling array, and the current number of detected keypress
  76. // Add as many keypresses from the sampling array to the USB key send array as possible.
  77. inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  78. {
  79. uint8_t Bootloader_KeyDetected = 0;
  80. uint8_t processed_keys = 0;
  81. // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
  82. for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
  83. {
  84. if ( keys[key] & (1 << 7) )
  85. {
  86. processed_keys++;
  87. // Display the detected scancode
  88. char tmpStr[4];
  89. int8ToStr( key, tmpStr );
  90. dPrintStrs( tmpStr, " " );
  91. // Is this a bootloader sequence key?
  92. if ( !Bootloader_KeyDetected
  93. && Bootloader_NextPositionReady
  94. && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  95. {
  96. Bootloader_KeyDetected = 1;
  97. Bootloader_NextPositionReady = 0;
  98. Bootloader_ConditionState++;
  99. }
  100. else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  101. {
  102. Bootloader_KeyDetected = 1;
  103. }
  104. // Determine if the key is a modifier
  105. uint8_t modFound = 0;
  106. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  107. // Modifier found
  108. if ( modifiers[mod] == key ) {
  109. USBKeys_Modifiers |= map[key];
  110. modFound = 1;
  111. break;
  112. }
  113. }
  114. // Modifier, already done this loop
  115. if ( modFound )
  116. continue;
  117. // Too many keys
  118. if ( USBKeys_Sent >= USBKeys_MaxSize )
  119. {
  120. info_print("USB Key limit reached");
  121. errorLED( 1 );
  122. break;
  123. }
  124. // Allow ignoring keys with 0's
  125. if ( map[key] != 0 )
  126. USBKeys_Array[USBKeys_Sent++] = map[key];
  127. }
  128. }
  129. // Boot loader sequence state handler
  130. switch ( processed_keys )
  131. {
  132. // The next bootloader key can now be pressed, if there were no keys processed
  133. case 0:
  134. Bootloader_NextPositionReady = 1;
  135. break;
  136. // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
  137. // This case purposely falls through
  138. case 1:
  139. if ( Bootloader_KeyDetected )
  140. break;
  141. default:
  142. Bootloader_ConditionState = 0;
  143. break;
  144. }
  145. // Add debug separator if keys sent via USB
  146. if ( USBKeys_Sent > 0 )
  147. print("\033[1;32m|\033[0m\n");
  148. }
  149. inline void process_macros(void)
  150. {
  151. // Only process macros once (if some were found), until the next USB send
  152. if ( USBKeys_Sent != 0 )
  153. return;
  154. // Debounce Sampling Array to USB Data Array
  155. keyPressDetection( KeyIndex_Array, KeyIndex_Size, MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
  156. // Check for bootloader condition
  157. if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
  158. jumpToBootloader();
  159. }