Kiibohd Controller
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

macro.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Copyright (C) 2011-2013 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. uint8_t Bootloader_KeyDetected = 0;
  40. // ----- Functions -----
  41. inline void macro_finishedWithBuffer( uint8_t sentKeys )
  42. {
  43. /* BudKeypad
  44. // Boot loader sequence state handler
  45. switch ( KeyIndex_BufferUsed )
  46. {
  47. // The next bootloader key can now be pressed, if there were no keys processed
  48. case 0:
  49. Bootloader_NextPositionReady = 1;
  50. break;
  51. // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
  52. // This case purposely falls through
  53. case 1:
  54. if ( Bootloader_KeyDetected )
  55. break;
  56. default:
  57. Bootloader_ConditionState = 0;
  58. break;
  59. }
  60. Bootloader_KeyDetected = 0;
  61. */
  62. }
  63. void jumpToBootloader(void)
  64. {
  65. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  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. #endif
  99. }
  100. // Given a sampling array, and the current number of detected keypress
  101. // Add as many keypresses from the sampling array to the USB key send array as possible.
  102. /*
  103. inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  104. {
  105. uint8_t Bootloader_KeyDetected = 0;
  106. uint8_t processed_keys = 0;
  107. // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
  108. for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
  109. {
  110. if ( keys[key] & (1 << 7) )
  111. {
  112. processed_keys++;
  113. // Display the detected scancode
  114. char tmpStr[4];
  115. int8ToStr( key, tmpStr );
  116. dPrintStrs( tmpStr, " " );
  117. // Is this a bootloader sequence key?
  118. if ( !Bootloader_KeyDetected
  119. && Bootloader_NextPositionReady
  120. && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  121. {
  122. Bootloader_KeyDetected = 1;
  123. Bootloader_NextPositionReady = 0;
  124. Bootloader_ConditionState++;
  125. }
  126. else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  127. {
  128. Bootloader_KeyDetected = 1;
  129. }
  130. // Determine if the key is a modifier
  131. uint8_t modFound = 0;
  132. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  133. // Modifier found
  134. if ( modifiers[mod] == key ) {
  135. USBKeys_Modifiers |= map[key];
  136. modFound = 1;
  137. break;
  138. }
  139. }
  140. // Modifier, already done this loop
  141. if ( modFound )
  142. continue;
  143. // Too many keys
  144. if ( USBKeys_Sent >= USBKeys_MaxSize )
  145. {
  146. info_print("USB Key limit reached");
  147. errorLED( 1 );
  148. break;
  149. }
  150. // Allow ignoring keys with 0's
  151. if ( map[key] != 0 )
  152. USBKeys_Array[USBKeys_Sent++] = map[key];
  153. }
  154. }
  155. // Boot loader sequence state handler
  156. switch ( processed_keys )
  157. {
  158. // The next bootloader key can now be pressed, if there were no keys processed
  159. case 0:
  160. Bootloader_NextPositionReady = 1;
  161. break;
  162. // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
  163. // This case purposely falls through
  164. case 1:
  165. if ( Bootloader_KeyDetected )
  166. break;
  167. default:
  168. Bootloader_ConditionState = 0;
  169. break;
  170. }
  171. // Add debug separator if keys sent via USB
  172. if ( USBKeys_Sent > 0 )
  173. print("\033[1;32m|\033[0m\n");
  174. }
  175. */
  176. // Scancode Macro Detection
  177. int scancodeMacro( uint8_t scanCode )
  178. {
  179. /*
  180. if ( scanCode == 0x7A )
  181. {
  182. scan_resetKeyboard();
  183. }
  184. else
  185. {
  186. scan_sendData( scanCode );
  187. _delay_ms( 200 );
  188. scan_sendData( 0x80 | scanCode );
  189. }
  190. return 1;
  191. */
  192. /*
  193. // BudKeypad
  194. // Is this a bootloader sequence key?
  195. if ( !Bootloader_KeyDetected
  196. && Bootloader_NextPositionReady
  197. && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState] )
  198. {
  199. Bootloader_KeyDetected = 1;
  200. Bootloader_NextPositionReady = 0;
  201. Bootloader_ConditionState++;
  202. erro_dPrint("detect");
  203. }
  204. else if ( Bootloader_ConditionState > 0 && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
  205. {
  206. Bootloader_KeyDetected = 0;
  207. Bootloader_NextPositionReady = 1;
  208. erro_dPrint("detect-again!");
  209. }
  210. // Cancel sequence
  211. else
  212. {
  213. Bootloader_KeyDetected = 0;
  214. Bootloader_NextPositionReady = 1;
  215. Bootloader_ConditionState = 0;
  216. erro_dPrint("Arg");
  217. }
  218. */
  219. return 0;
  220. }
  221. uint8_t sendCode = 0;
  222. // USBCode Macro Detection
  223. int usbcodeMacro( uint8_t usbCode )
  224. {
  225. // Keyboard Input Test Macro
  226. /*
  227. switch ( usbCode )
  228. {
  229. case KEY_F1:
  230. sendCode--;
  231. //scan_sendData( 0x90 );
  232. scan_sendData( sendCode );
  233. _delay_ms( 200 );
  234. break;
  235. case KEY_F2:
  236. //scan_sendData( 0x90 );
  237. scan_sendData( sendCode );
  238. _delay_ms( 200 );
  239. break;
  240. case KEY_F3:
  241. sendCode++;
  242. //scan_sendData( 0x90 );
  243. scan_sendData( sendCode );
  244. _delay_ms( 200 );
  245. break;
  246. case KEY_F4:
  247. sendCode += 0x10;
  248. //scan_sendData( 0x90 );
  249. scan_sendData( sendCode );
  250. _delay_ms( 200 );
  251. break;
  252. case KEY_F5:
  253. // Set 9th bit to 0
  254. UCSR1B &= ~(1 << 0);
  255. _delay_ms( 200 );
  256. break;
  257. case KEY_F6:
  258. // Set 9th bit to 1
  259. UCSR1B |= (1 << 0);
  260. _delay_ms( 200 );
  261. break;
  262. case KEY_F11:
  263. // Set click code
  264. KeyIndex_Add_InputSignal = sendCode;
  265. _delay_ms( 200 );
  266. break;
  267. default:
  268. return 0;
  269. }
  270. return 1;
  271. */
  272. return 0;
  273. }
  274. // Given a list of keypresses, translate into the USB key codes
  275. // The buffer is cleared after running
  276. // If the buffer doesn't fit into the USB send array, the extra keys are dropped
  277. void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
  278. {
  279. // Loop through input buffer
  280. for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
  281. {
  282. // Get the keycode from the buffer
  283. uint8_t key = KeyIndex_Buffer[index];
  284. // Check key for special usages using the scancode
  285. // If non-zero return, ignore normal processing of the scancode
  286. if ( scancodeMacro( key ) )
  287. continue;
  288. // Check key for special usages using the usbcode
  289. // If non-zero return, ignore normal processing of the usbcode
  290. if ( usbcodeMacro( map[key] ) )
  291. continue;
  292. // Determine if the key is a modifier
  293. uint8_t modFound = 0;
  294. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  295. // Modifier found
  296. if ( modifiers[mod] == key ) {
  297. USBKeys_Modifiers |= map[key];
  298. modFound = 1;
  299. break;
  300. }
  301. }
  302. // Modifier, already done this loop
  303. if ( modFound )
  304. continue;
  305. // Too many keys
  306. if ( USBKeys_Sent >= USBKeys_MaxSize )
  307. {
  308. info_print("USB Key limit reached");
  309. errorLED( 1 );
  310. break;
  311. }
  312. // Allow ignoring keys with 0's
  313. if ( map[key] != 0 )
  314. {
  315. USBKeys_Array[USBKeys_Sent++] = map[key];
  316. }
  317. else
  318. {
  319. // Key was not mapped
  320. // TODO Add dead key map
  321. char tmpStr[6];
  322. hexToStr( key, tmpStr );
  323. erro_dPrint( "Key not mapped... - ", tmpStr );
  324. errorLED( 1 );
  325. }
  326. }
  327. // Signal Macro processor that all of the buffered keys have been processed
  328. macro_finishedWithBuffer( KeyIndex_BufferUsed );
  329. // Signal buffer that we've used it
  330. scan_finishedWithBuffer( KeyIndex_BufferUsed );
  331. }
  332. inline void process_macros(void)
  333. {
  334. // Online process macros once (if some were found), until the next USB send
  335. if ( USBKeys_Sent != 0 )
  336. return;
  337. // Query the input buffer for keypresses
  338. keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
  339. // Check for bootloader condition
  340. if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
  341. jumpToBootloader();
  342. }