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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. uint8_t Bootloader_KeyDetected = 0;
  41. // ----- Functions -----
  42. inline void macro_finishedWithBuffer(void)
  43. {
  44. /* BudKeypad
  45. // Boot loader sequence state handler
  46. switch ( KeyIndex_BufferUsed )
  47. {
  48. // The next bootloader key can now be pressed, if there were no keys processed
  49. case 0:
  50. Bootloader_NextPositionReady = 1;
  51. break;
  52. // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
  53. // This case purposely falls through
  54. case 1:
  55. if ( Bootloader_KeyDetected )
  56. break;
  57. default:
  58. Bootloader_ConditionState = 0;
  59. break;
  60. }
  61. Bootloader_KeyDetected = 0;
  62. */
  63. }
  64. void jumpToBootloader(void)
  65. {
  66. cli();
  67. // disable watchdog, if enabled
  68. // disable all peripherals
  69. UDCON = 1;
  70. USBCON = (1<<FRZCLK); // disable USB
  71. UCSR1B = 0;
  72. _delay_ms(5);
  73. #if defined(__AVR_AT90USB162__) // Teensy 1.0
  74. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  75. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  76. DDRB = 0; DDRC = 0; DDRD = 0;
  77. PORTB = 0; PORTC = 0; PORTD = 0;
  78. asm volatile("jmp 0x3E00");
  79. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  80. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  81. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  82. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  83. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  84. asm volatile("jmp 0x7E00");
  85. #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  86. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  87. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  88. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  89. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  90. asm volatile("jmp 0xFC00");
  91. #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  92. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  93. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  94. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  95. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  96. asm volatile("jmp 0x1FC00");
  97. #endif
  98. }
  99. // Given a sampling array, and the current number of detected keypress
  100. // Add as many keypresses from the sampling array to the USB key send array as possible.
  101. /*
  102. inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  103. {
  104. uint8_t Bootloader_KeyDetected = 0;
  105. uint8_t processed_keys = 0;
  106. // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
  107. for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
  108. {
  109. if ( keys[key] & (1 << 7) )
  110. {
  111. processed_keys++;
  112. // Display the detected scancode
  113. char tmpStr[4];
  114. int8ToStr( key, tmpStr );
  115. dPrintStrs( tmpStr, " " );
  116. // Is this a bootloader sequence key?
  117. if ( !Bootloader_KeyDetected
  118. && Bootloader_NextPositionReady
  119. && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  120. {
  121. Bootloader_KeyDetected = 1;
  122. Bootloader_NextPositionReady = 0;
  123. Bootloader_ConditionState++;
  124. }
  125. else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  126. {
  127. Bootloader_KeyDetected = 1;
  128. }
  129. // Determine if the key is a modifier
  130. uint8_t modFound = 0;
  131. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  132. // Modifier found
  133. if ( modifiers[mod] == key ) {
  134. USBKeys_Modifiers |= map[key];
  135. modFound = 1;
  136. break;
  137. }
  138. }
  139. // Modifier, already done this loop
  140. if ( modFound )
  141. continue;
  142. // Too many keys
  143. if ( USBKeys_Sent >= USBKeys_MaxSize )
  144. {
  145. info_print("USB Key limit reached");
  146. errorLED( 1 );
  147. break;
  148. }
  149. // Allow ignoring keys with 0's
  150. if ( map[key] != 0 )
  151. USBKeys_Array[USBKeys_Sent++] = map[key];
  152. }
  153. }
  154. // Boot loader sequence state handler
  155. switch ( processed_keys )
  156. {
  157. // The next bootloader key can now be pressed, if there were no keys processed
  158. case 0:
  159. Bootloader_NextPositionReady = 1;
  160. break;
  161. // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
  162. // This case purposely falls through
  163. case 1:
  164. if ( Bootloader_KeyDetected )
  165. break;
  166. default:
  167. Bootloader_ConditionState = 0;
  168. break;
  169. }
  170. // Add debug separator if keys sent via USB
  171. if ( USBKeys_Sent > 0 )
  172. print("\033[1;32m|\033[0m\n");
  173. }
  174. */
  175. // Scancode Macro Detection
  176. int scancodeMacro( uint8_t scanCode )
  177. {
  178. /*
  179. if ( scanCode == 0x7A )
  180. {
  181. scan_resetKeyboard();
  182. }
  183. else
  184. {
  185. scan_sendData( scanCode );
  186. _delay_ms( 200 );
  187. scan_sendData( 0x80 | scanCode );
  188. }
  189. return 1;
  190. */
  191. /* BudKeypad
  192. // Is this a bootloader sequence key?
  193. if ( !Bootloader_KeyDetected
  194. && Bootloader_NextPositionReady
  195. && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  196. {
  197. Bootloader_KeyDetected = 1;
  198. Bootloader_NextPositionReady = 0;
  199. Bootloader_ConditionState++;
  200. }
  201. else if ( Bootloader_ConditionState > 0 && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  202. {
  203. Bootloader_KeyDetected = 1;
  204. }
  205. */
  206. return 0;
  207. }
  208. uint8_t sendCode = 0;
  209. // USBCode Macro Detection
  210. int usbcodeMacro( uint8_t usbCode )
  211. {
  212. /*
  213. // Keyboard Input Test Macro
  214. switch ( usbCode )
  215. {
  216. case KEY_F1:
  217. sendCode--;
  218. scan_sendData( 0x90 );
  219. scan_sendData( sendCode );
  220. _delay_ms( 200 );
  221. break;
  222. case KEY_F2:
  223. scan_sendData( 0x90 );
  224. scan_sendData( sendCode );
  225. _delay_ms( 200 );
  226. break;
  227. case KEY_F3:
  228. sendCode++;
  229. scan_sendData( 0x90 );
  230. scan_sendData( sendCode );
  231. _delay_ms( 200 );
  232. break;
  233. default:
  234. return 0;
  235. }
  236. return 1;
  237. */
  238. return 0;
  239. }
  240. // Given a list of keypresses, translate into the USB key codes
  241. // The buffer is cleared after running
  242. // If the buffer doesn't fit into the USB send array, the extra keys are dropped
  243. void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  244. {
  245. // Loop through input buffer
  246. for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
  247. {
  248. // Get the keycode from the buffer
  249. uint8_t key = KeyIndex_Buffer[index];
  250. // Check key for special usages using the scancode
  251. // If non-zero return, ignore normal processing of the scancode
  252. if ( scancodeMacro( key ) )
  253. continue;
  254. // Check key for special usages using the usbcode
  255. // If non-zero return, ignore normal processing of the usbcode
  256. if ( usbcodeMacro( map[key] ) )
  257. continue;
  258. // Determine if the key is a modifier
  259. uint8_t modFound = 0;
  260. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  261. // Modifier found
  262. if ( modifiers[mod] == key ) {
  263. USBKeys_Modifiers |= map[key];
  264. modFound = 1;
  265. break;
  266. }
  267. }
  268. // Modifier, already done this loop
  269. if ( modFound )
  270. continue;
  271. // Too many keys
  272. if ( USBKeys_Sent >= USBKeys_MaxSize )
  273. {
  274. info_print("USB Key limit reached");
  275. errorLED( 1 );
  276. break;
  277. }
  278. // Allow ignoring keys with 0's
  279. if ( map[key] != 0 )
  280. {
  281. USBKeys_Array[USBKeys_Sent++] = map[key];
  282. }
  283. else
  284. {
  285. // Key was not mapped
  286. // TODO Add dead key map
  287. char tmpStr[6];
  288. hexToStr( key, tmpStr );
  289. erro_dPrint( "Key not mapped... - ", tmpStr );
  290. errorLED( 1 );
  291. }
  292. }
  293. // Signal Macro processor that all of the buffered keys have been processed
  294. macro_finishedWithBuffer();
  295. // Signal buffer that we've used it
  296. scan_finishedWithBuffer();
  297. }
  298. inline void process_macros(void)
  299. {
  300. // Online process macros once (if some were found), until the next USB send
  301. if ( USBKeys_Sent != 0 )
  302. return;
  303. // Query the input buffer for keypresses
  304. keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
  305. // Check for bootloader condition
  306. //if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
  307. // jumpToBootloader();
  308. }