Kiibohd Controller
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

macro.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright (C) 2014 by Jacob Alexander
  2. *
  3. * This file is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This file is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this file. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // ----- Includes -----
  17. // Compiler Includes
  18. #include <Lib/MacroLib.h>
  19. // Project Includes
  20. #include <cli.h>
  21. #include <led.h>
  22. #include <print.h>
  23. #include <scan_loop.h>
  24. #include <output_com.h>
  25. // Keymaps
  26. #include "usb_hid.h"
  27. #include <defaultMap.h>
  28. #include "generatedKeymap.h" // TODO Use actual generated version
  29. // Local Includes
  30. #include "macro.h"
  31. // ----- Function Declarations -----
  32. void cliFunc_capList ( char* args );
  33. void cliFunc_capSelect ( char* args );
  34. void cliFunc_lookComb ( char* args );
  35. void cliFunc_lookDefault( char* args );
  36. void cliFunc_lookPartial( char* args );
  37. void cliFunc_macroDebug ( char* args );
  38. // ----- Variables -----
  39. // Macro Module command dictionary
  40. char* macroCLIDictName = "Macro Module Commands (Not all commands fully work yet...)";
  41. CLIDictItem macroCLIDict[] = {
  42. { "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
  43. { "capSelect", "Triggers the specified capability." NL "\t\t\033[35mU10\033[0m USB Code 0x0A, \033[35mK11\033[0m Keyboard Capability 0x0B, \033[35mS12\033[0m Scancode 0x0C", cliFunc_capSelect },
  44. { "lookComb", "Do a lookup on the Combined map." NL "\t\t\033[35mS10\033[0m Scancode 0x0A, \033[35mU11\033[0m USB Code 0x0B", cliFunc_lookComb },
  45. { "lookDefault", "Do a lookup on the Default map." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_lookDefault },
  46. { "lookPartial", "Do a lookup on the layered Partial maps." NL "\t\t\033[35mS10\033[0m Scancode 0x0A, \033[35mU11\033[0m USB Code 0x0B", cliFunc_lookPartial },
  47. { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
  48. { 0, 0, 0 } // Null entry for dictionary end
  49. };
  50. // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
  51. uint8_t macroDebugMode = 0;
  52. // Key Trigger List Buffer
  53. // * Item 1: scan code
  54. // * Item 2: state
  55. // ...
  56. uint8_t macroTriggerListBuffer[0xFF * 2] = { 0 }; // Each key has a state to be cached (this can be decreased to save RAM)
  57. uint8_t macroTriggerListBufferSize = 0;
  58. // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
  59. // Possibly could be calculated by the KLL compiler
  60. TriggerMacro *triggerMacroPendingList[30];
  61. // ----- Functions -----
  62. // Looks up the trigger list for the given scan code (from the active layer)
  63. unsigned int *Macro_layerLookup( uint8_t scanCode )
  64. {
  65. // TODO - No layer fallthrough lookup
  66. return default_scanMap[ scanCode ];
  67. }
  68. // Update the scancode key state
  69. // States:
  70. // * 0x00 - Reserved
  71. // * 0x01 - Pressed
  72. // * 0x02 - Held
  73. // * 0x03 - Released
  74. // * 0x04 - Unpressed (this is currently ignored)
  75. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  76. {
  77. // Only add to macro trigger list if one of three states
  78. switch ( state )
  79. {
  80. case 0x01: // Pressed
  81. case 0x02: // Held
  82. case 0x03: // Released
  83. macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = scanCode;
  84. macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = state;
  85. break;
  86. }
  87. }
  88. // Update the scancode analog state
  89. // States:
  90. // * 0x00 - Reserved
  91. // * 0x01 - Released
  92. // * 0x02-0xFF - Analog value (low to high)
  93. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  94. {
  95. // TODO
  96. }
  97. // Update led state
  98. // States:
  99. // * 0x00 - Reserved
  100. // * 0x01 - On
  101. // * 0x02 - Off
  102. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  103. {
  104. // TODO
  105. }
  106. // Evaluate/Update the TriggerMacro
  107. void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
  108. {
  109. // Which combo in the sequence is being evaluated
  110. unsigned int comboPos = triggerMacro->pos;
  111. // If combo length is more than 1, cancel trigger macro if an incorrect key is found
  112. uint8_t comboLength = triggerMacro->guide[ comboPos ];
  113. // Iterate over list of keys currently pressed
  114. for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed += 2 )
  115. {
  116. // Compare with keys in combo
  117. for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
  118. {
  119. // Lookup key in combo
  120. uint8_t guideKey = triggerMacro->guide[ comboPos + comboKey + 2 ]; // TODO Only Press/Hold/Release atm
  121. // Sequence Case
  122. if ( comboLength == 1 )
  123. {
  124. // If key matches and only 1 key pressed, increment the TriggerMacro combo position
  125. if ( guideKey == macroTriggerListBuffer[ keyPressed ] && macroTriggerListBufferSize == 1 )
  126. {
  127. triggerMacro->pos += comboLength * 2 + 1;
  128. // TODO check if TriggerMacro is finished, register ResultMacro
  129. return;
  130. }
  131. // If key does not match or more than 1 key pressed, reset the TriggerMacro combo position
  132. triggerMacro->pos = 0;
  133. return;
  134. }
  135. // Combo Case
  136. else
  137. {
  138. // TODO
  139. }
  140. }
  141. }
  142. }
  143. inline void Macro_bufferAdd( uint8_t byte )
  144. {
  145. // Make sure we haven't overflowed the key buffer
  146. // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
  147. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
  148. {
  149. uint8_t key = DefaultMap_Lookup[byte];
  150. for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
  151. {
  152. // Key already in the buffer
  153. if ( KeyIndex_Buffer[c] == key )
  154. return;
  155. }
  156. // Add to the buffer
  157. KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
  158. }
  159. }
  160. inline void Macro_bufferRemove( uint8_t byte )
  161. {
  162. uint8_t key = DefaultMap_Lookup[byte];
  163. // Check for the released key, and shift the other keys lower on the buffer
  164. for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
  165. {
  166. // Key to release found
  167. if ( KeyIndex_Buffer[c] == key )
  168. {
  169. // Shift keys from c position
  170. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  171. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  172. // Decrement Buffer
  173. KeyIndex_BufferUsed--;
  174. return;
  175. }
  176. }
  177. // Error case (no key to release)
  178. erro_msg("Could not find key to release: ");
  179. printHex( key );
  180. }
  181. inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
  182. {
  183. }
  184. inline void Macro_process()
  185. {
  186. // Only do one round of macro processing between Output Module timer sends
  187. if ( USBKeys_Sent != 0 )
  188. return;
  189. // Loop through macro trigger buffer
  190. for ( uint8_t index = 0; index < macroTriggerListBufferSize; index += 2 )
  191. {
  192. // Get scanCode, first item of macroTriggerListBuffer pairs
  193. uint8_t scanCode = macroTriggerListBuffer[ index ];
  194. // Lookup trigger list for this key
  195. unsigned int *triggerList = Macro_layerLookup( scanCode );
  196. // The first element is the length of the trigger list
  197. unsigned int triggerListSize = triggerList[0];
  198. // Loop through the trigger list
  199. for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
  200. {
  201. // Lookup TriggerMacro
  202. TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
  203. // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
  204. uint8_t state = macroTriggerListBuffer[ index + 1 ];
  205. // Evaluate Macro
  206. Macro_evalTriggerMacro( triggerMacro );
  207. }
  208. }
  209. /* TODO
  210. // Loop through input buffer
  211. for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
  212. {
  213. //print(" KEYS: ");
  214. //printInt8( KeyIndex_BufferUsed );
  215. // Get the keycode from the buffer
  216. uint8_t key = KeyIndex_Buffer[index];
  217. // Set the modifier bit if this key is a modifier
  218. if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
  219. {
  220. USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
  221. // Modifier processed, move on to the next key
  222. continue;
  223. }
  224. // Too many keys
  225. if ( USBKeys_Sent >= USBKeys_MaxSize )
  226. {
  227. warn_msg("USB Key limit reached");
  228. errorLED( 1 );
  229. break;
  230. }
  231. // Allow ignoring keys with 0's
  232. if ( key != 0 )
  233. {
  234. USBKeys_Array[USBKeys_Sent++] = key;
  235. }
  236. else
  237. {
  238. // Key was not mapped
  239. erro_msg( "Key not mapped... - " );
  240. printHex( key );
  241. errorLED( 1 );
  242. }
  243. }
  244. */
  245. // Signal buffer that we've used it
  246. Scan_finishedWithBuffer( KeyIndex_BufferUsed );
  247. // If Macro debug mode is set, clear the USB Buffer
  248. if ( macroDebugMode )
  249. {
  250. USBKeys_Modifiers = 0;
  251. USBKeys_Sent = 0;
  252. }
  253. }
  254. inline void Macro_setup()
  255. {
  256. // Register Macro CLI dictionary
  257. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  258. // Disable Macro debug mode
  259. macroDebugMode = 0;
  260. // Make sure macro trigger buffer is empty
  261. macroTriggerListBufferSize = 0;
  262. }
  263. // ----- CLI Command Functions -----
  264. void cliFunc_capList( char* args )
  265. {
  266. // TODO
  267. }
  268. void cliFunc_capSelect( char* args )
  269. {
  270. // Parse code from argument
  271. // NOTE: Only first argument is used
  272. char* arg1Ptr;
  273. char* arg2Ptr;
  274. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  275. // Depending on the first character, the lookup changes
  276. switch ( arg1Ptr[0] )
  277. {
  278. // Keyboard Capability
  279. case 'K':
  280. // TODO
  281. break;
  282. // Scancode
  283. case 'S':
  284. // Add to the USB Buffer using the DefaultMap lookup
  285. Macro_bufferAdd( decToInt( &arg1Ptr[1] ) );
  286. break;
  287. // USB Code
  288. case 'U':
  289. // Just add the key to the USB Buffer
  290. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
  291. {
  292. KeyIndex_Buffer[KeyIndex_BufferUsed++] = decToInt( &arg1Ptr[1] );
  293. }
  294. break;
  295. }
  296. }
  297. void cliFunc_lookComb( char* args )
  298. {
  299. // Parse code from argument
  300. // NOTE: Only first argument is used
  301. char* arg1Ptr;
  302. char* arg2Ptr;
  303. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  304. // Depending on the first character, the lookup changes
  305. switch ( arg1Ptr[0] )
  306. {
  307. // Scancode
  308. case 'S':
  309. // TODO
  310. break;
  311. // USB Code
  312. case 'U':
  313. // TODO
  314. break;
  315. }
  316. }
  317. void cliFunc_lookDefault( char* args )
  318. {
  319. // Parse code from argument
  320. // NOTE: Only first argument is used
  321. char* arg1Ptr;
  322. char* arg2Ptr;
  323. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  324. // Depending on the first character, the lookup changes
  325. switch ( arg1Ptr[0] )
  326. {
  327. // Scancode
  328. case 'S':
  329. print( NL );
  330. printInt8( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
  331. print(" ");
  332. printHex( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
  333. break;
  334. }
  335. }
  336. void cliFunc_lookPartial( char* args )
  337. {
  338. // Parse code from argument
  339. // NOTE: Only first argument is used
  340. char* arg1Ptr;
  341. char* arg2Ptr;
  342. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  343. // Depending on the first character, the lookup changes
  344. switch ( arg1Ptr[0] )
  345. {
  346. // Scancode
  347. case 'S':
  348. // TODO
  349. break;
  350. // USB Code
  351. case 'U':
  352. // TODO
  353. break;
  354. }
  355. }
  356. void cliFunc_macroDebug( char* args )
  357. {
  358. // Toggle macro debug mode
  359. macroDebugMode = macroDebugMode ? 0 : 1;
  360. print( NL );
  361. info_msg("Macro Debug Mode: ");
  362. printInt8( macroDebugMode );
  363. }