Kiibohd Controller
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

macro.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /*
  192. // BudKeypad
  193. // Is this a bootloader sequence key?
  194. if ( !Bootloader_KeyDetected
  195. && Bootloader_NextPositionReady
  196. && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  197. {
  198. Bootloader_KeyDetected = 1;
  199. Bootloader_NextPositionReady = 0;
  200. Bootloader_ConditionState++;
  201. erro_dPrint("detect");
  202. }
  203. else if ( Bootloader_ConditionState > 0 && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  204. {
  205. Bootloader_KeyDetected = 0;
  206. Bootloader_NextPositionReady = 1;
  207. erro_dPrint("detect-again!");
  208. }
  209. // Cancel sequence
  210. else
  211. {
  212. Bootloader_KeyDetected = 0;
  213. Bootloader_NextPositionReady = 1;
  214. Bootloader_ConditionState = 0;
  215. erro_dPrint("Arg");
  216. }
  217. */
  218. return 0;
  219. }
  220. uint8_t sendCode = 0;
  221. // USBCode Macro Detection
  222. int usbcodeMacro( uint8_t usbCode )
  223. {
  224. // Keyboard Input Test Macro
  225. /*
  226. switch ( usbCode )
  227. {
  228. case KEY_F1:
  229. sendCode--;
  230. //scan_sendData( 0x90 );
  231. scan_sendData( sendCode );
  232. _delay_ms( 200 );
  233. break;
  234. case KEY_F2:
  235. //scan_sendData( 0x90 );
  236. scan_sendData( sendCode );
  237. _delay_ms( 200 );
  238. break;
  239. case KEY_F3:
  240. sendCode++;
  241. //scan_sendData( 0x90 );
  242. scan_sendData( sendCode );
  243. _delay_ms( 200 );
  244. break;
  245. case KEY_F4:
  246. sendCode += 0x10;
  247. //scan_sendData( 0x90 );
  248. scan_sendData( sendCode );
  249. _delay_ms( 200 );
  250. break;
  251. case KEY_F5:
  252. // Set 9th bit to 0
  253. UCSR1B &= ~(1 << 0);
  254. _delay_ms( 200 );
  255. break;
  256. case KEY_F6:
  257. // Set 9th bit to 1
  258. UCSR1B |= (1 << 0);
  259. _delay_ms( 200 );
  260. break;
  261. case KEY_F11:
  262. // Set click code
  263. KeyIndex_Add_InputSignal = sendCode;
  264. _delay_ms( 200 );
  265. break;
  266. default:
  267. return 0;
  268. }
  269. return 1;
  270. */
  271. return 0;
  272. }
  273. // Given a list of keypresses, translate into the USB key codes
  274. // The buffer is cleared after running
  275. // If the buffer doesn't fit into the USB send array, the extra keys are dropped
  276. void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  277. {
  278. // Loop through input buffer
  279. for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
  280. {
  281. // Get the keycode from the buffer
  282. uint8_t key = KeyIndex_Buffer[index];
  283. // Check key for special usages using the scancode
  284. // If non-zero return, ignore normal processing of the scancode
  285. if ( scancodeMacro( key ) )
  286. continue;
  287. // Check key for special usages using the usbcode
  288. // If non-zero return, ignore normal processing of the usbcode
  289. if ( usbcodeMacro( map[key] ) )
  290. continue;
  291. // Determine if the key is a modifier
  292. uint8_t modFound = 0;
  293. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  294. // Modifier found
  295. if ( modifiers[mod] == key ) {
  296. USBKeys_Modifiers |= map[key];
  297. modFound = 1;
  298. break;
  299. }
  300. }
  301. // Modifier, already done this loop
  302. if ( modFound )
  303. continue;
  304. // Too many keys
  305. if ( USBKeys_Sent >= USBKeys_MaxSize )
  306. {
  307. info_print("USB Key limit reached");
  308. errorLED( 1 );
  309. break;
  310. }
  311. // Allow ignoring keys with 0's
  312. if ( map[key] != 0 )
  313. {
  314. USBKeys_Array[USBKeys_Sent++] = map[key];
  315. }
  316. else
  317. {
  318. // Key was not mapped
  319. // TODO Add dead key map
  320. char tmpStr[6];
  321. hexToStr( key, tmpStr );
  322. erro_dPrint( "Key not mapped... - ", tmpStr );
  323. errorLED( 1 );
  324. }
  325. }
  326. // Signal Macro processor that all of the buffered keys have been processed
  327. macro_finishedWithBuffer();
  328. // Signal buffer that we've used it
  329. scan_finishedWithBuffer();
  330. }
  331. inline void process_macros(void)
  332. {
  333. // Online process macros once (if some were found), until the next USB send
  334. if ( USBKeys_Sent != 0 )
  335. return;
  336. // Query the input buffer for keypresses
  337. keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
  338. // Check for bootloader condition
  339. if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
  340. jumpToBootloader();
  341. }