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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // Scancode Macro Detection
  153. int scancodeMacro( uint8_t scanCode )
  154. {
  155. /*
  156. if ( scanCode == 0x7A )
  157. {
  158. scan_resetKeyboard();
  159. }
  160. else
  161. {
  162. scan_sendData( scanCode );
  163. _delay_ms( 200 );
  164. scan_sendData( 0x80 | scanCode );
  165. }
  166. return 1;
  167. */
  168. return 0;
  169. }
  170. uint8_t sendCode = 0;
  171. // USBCode Macro Detection
  172. int usbcodeMacro( uint8_t usbCode )
  173. {
  174. /*
  175. // Keyboard Input Test Macro
  176. switch ( usbCode )
  177. {
  178. case KEY_F1:
  179. sendCode--;
  180. scan_sendData( 0x90 );
  181. scan_sendData( sendCode );
  182. _delay_ms( 200 );
  183. break;
  184. case KEY_F2:
  185. scan_sendData( 0x90 );
  186. scan_sendData( sendCode );
  187. _delay_ms( 200 );
  188. break;
  189. case KEY_F3:
  190. sendCode++;
  191. scan_sendData( 0x90 );
  192. scan_sendData( sendCode );
  193. _delay_ms( 200 );
  194. break;
  195. default:
  196. return 0;
  197. }
  198. return 1;
  199. */
  200. return 0;
  201. }
  202. // Given a list of keypresses, translate into the USB key codes
  203. // The buffer is cleared after running
  204. // If the buffer doesn't fit into the USB send array, the extra keys are dropped
  205. void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  206. {
  207. // Loop through input buffer
  208. for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
  209. {
  210. // Get the keycode from the buffer
  211. uint8_t key = KeyIndex_Buffer[index];
  212. // Check key for special usages using the scancode
  213. // If non-zero return, ignore normal processing of the scancode
  214. if ( scancodeMacro( key ) )
  215. continue;
  216. // Check key for special usages using the usbcode
  217. // If non-zero return, ignore normal processing of the usbcode
  218. if ( usbcodeMacro( map[key] ) )
  219. continue;
  220. // Determine if the key is a modifier
  221. uint8_t modFound = 0;
  222. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  223. // Modifier found
  224. if ( modifiers[mod] == key ) {
  225. USBKeys_Modifiers |= map[key];
  226. modFound = 1;
  227. break;
  228. }
  229. }
  230. // Modifier, already done this loop
  231. if ( modFound )
  232. continue;
  233. // Too many keys
  234. if ( USBKeys_Sent >= USBKeys_MaxSize )
  235. {
  236. info_print("USB Key limit reached");
  237. errorLED( 1 );
  238. break;
  239. }
  240. // Allow ignoring keys with 0's
  241. if ( map[key] != 0 )
  242. {
  243. USBKeys_Array[USBKeys_Sent++] = map[key];
  244. }
  245. else
  246. {
  247. // Key was not mapped
  248. // TODO Add dead key map
  249. char tmpStr[6];
  250. hexToStr( key, tmpStr );
  251. erro_dPrint( "Key not mapped... - ", tmpStr );
  252. errorLED( 1 );
  253. }
  254. }
  255. // Signal buffer that we've used it
  256. scan_finishedWithBuffer();
  257. }
  258. inline void process_macros(void)
  259. {
  260. // Online process macros once (if some were found), until the next USB send
  261. if ( USBKeys_Sent != 0 )
  262. return;
  263. // Query the input buffer for keypresses
  264. keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
  265. // Check for bootloader condition
  266. //if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
  267. // jumpToBootloader();
  268. }