Kiibohd Controller
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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_keyPress ( char* args );
  35. void cliFunc_keyRelease( char* args );
  36. void cliFunc_layerList ( char* args );
  37. void cliFunc_layerState( char* args );
  38. void cliFunc_macroDebug( char* args );
  39. void cliFunc_macroList ( char* args );
  40. void cliFunc_macroProc ( char* args );
  41. void cliFunc_macroShow ( char* args );
  42. void cliFunc_macroStep ( char* args );
  43. // ----- Variables -----
  44. // Macro Module command dictionary
  45. char* macroCLIDictName = "Macro Module Commands";
  46. CLIDictItem macroCLIDict[] = {
  47. { "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
  48. { "capSelect", "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
  49. { "keyPress", "Send key-presses to the macro module. Held until released. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
  50. { "keyRelease", "Release a key-press from the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
  51. { "layerList", "List available layers.", cliFunc_layerList },
  52. { "layerState", "Modify specified indexed layer state <layer> <state byte>." NL "\t\t\033[35mL2\033[0m Indexed Layer 0x02" NL "\t\t0 Off, 1 Shift, 2 Latch, 4 Lock States", cliFunc_layerState },
  53. { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
  54. { "macroList", "List the defined trigger and result macros.", cliFunc_macroList },
  55. { "macroProc", "Pause/Resume macro processing.", cliFunc_macroProc },
  56. { "macroShow", "Show the macro corresponding to the given index." NL "\t\t\033[35mT16\033[0m Indexed Trigger Macro 0x10, \033[35mR12\033[0m Indexed Result Macro 0x0C", cliFunc_macroShow },
  57. { "macroStep", "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
  58. { 0, 0, 0 } // Null entry for dictionary end
  59. };
  60. // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
  61. uint8_t macroDebugMode = 0;
  62. // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
  63. uint8_t macroPauseMode = 0;
  64. // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
  65. unsigned int macroStepCounter = 0;
  66. // Key Trigger List Buffer
  67. TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
  68. uint8_t macroTriggerListBufferSize = 0;
  69. // Pending Trigger Macro Index List
  70. // * Any trigger macros that need processing from a previous macro processing loop
  71. // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
  72. // Possibly could be calculated by the KLL compiler
  73. // XXX It may be possible to calculate the worst case using the KLL compiler
  74. unsigned int macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
  75. unsigned int macroTriggerMacroPendingListSize = 0;
  76. // Layer Index Stack
  77. // * When modifying layer state and the state is non-0x0, the stack must be adjusted
  78. unsigned int macroLayerIndexStack[ LayerNum ] = { 0 };
  79. unsigned int macroLayerIndexStackSize = 0;
  80. // Pending Result Macro Index List
  81. // * Any result macro that needs processing from a previous macro processing loop
  82. unsigned int macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
  83. unsigned int macroResultMacroPendingListSize = 0;
  84. // ----- Functions -----
  85. // Looks up the trigger list for the given scan code (from the active layer)
  86. // NOTE: Calling function must handle the NULL pointer case
  87. unsigned int *Macro_layerLookup( uint8_t scanCode )
  88. {
  89. // If no trigger macro is defined at the given layer, fallthrough to the next layer
  90. for ( unsigned int layer = 0; layer < macroLayerIndexStackSize; layer++ )
  91. {
  92. // Lookup layer
  93. unsigned int **map = LayerIndex[ macroLayerIndexStack[ layer ] ].triggerMap;
  94. // Determine if layer has key defined
  95. if ( map != 0 && *map[ scanCode ] != 0 )
  96. return map[ scanCode ];
  97. }
  98. // Do lookup on default layer
  99. unsigned int **map = LayerIndex[0].triggerMap;
  100. // Determine if layer has key defined
  101. if ( map == 0 && *map[ scanCode ] == 0 )
  102. {
  103. erro_msg("Scan Code has no defined Trigger Macro: ");
  104. printHex( scanCode );
  105. return 0;
  106. }
  107. // Return lookup result
  108. return map[ scanCode ];
  109. }
  110. // Update the scancode key state
  111. // States:
  112. // * 0x00 - Off
  113. // * 0x01 - Pressed
  114. // * 0x02 - Held
  115. // * 0x03 - Released
  116. // * 0x04 - Unpressed (this is currently ignored)
  117. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  118. {
  119. // Only add to macro trigger list if one of three states
  120. switch ( state )
  121. {
  122. case 0x01: // Pressed
  123. case 0x02: // Held
  124. case 0x03: // Released
  125. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  126. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  127. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
  128. macroTriggerListBufferSize++;
  129. break;
  130. }
  131. }
  132. // Update the scancode analog state
  133. // States:
  134. // * 0x00 - Off
  135. // * 0x01 - Released
  136. // * 0x02-0xFF - Analog value (low to high)
  137. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  138. {
  139. // Only add to macro trigger list if non-off
  140. if ( state != 0x00 )
  141. {
  142. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  143. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  144. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
  145. macroTriggerListBufferSize++;
  146. }
  147. }
  148. // Update led state
  149. // States:
  150. // * 0x00 - Off
  151. // * 0x01 - On
  152. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  153. {
  154. // Only add to macro trigger list if non-off
  155. if ( state != 0x00 )
  156. {
  157. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
  158. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  159. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
  160. macroTriggerListBufferSize++;
  161. }
  162. }
  163. // Evaluate/Update TriggerMacro
  164. void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
  165. {
  166. // Which combo in the sequence is being evaluated
  167. unsigned int comboPos = triggerMacro->pos;
  168. // If combo length is more than 1, cancel trigger macro if an incorrect key is found
  169. uint8_t comboLength = triggerMacro->guide[ comboPos ];
  170. // Iterate over list of keys currently pressed
  171. for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed++ )
  172. {
  173. // Compare with keys in combo
  174. for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
  175. {
  176. // Lookup key in combo
  177. uint8_t guideKey = triggerMacro->guide[ comboPos + comboKey + 2 ]; // TODO Only Press/Hold/Release atm
  178. // Sequence Case
  179. if ( comboLength == 1 )
  180. {
  181. // If key matches and only 1 key pressed, increment the TriggerMacro combo position
  182. if ( guideKey == macroTriggerListBuffer[ keyPressed ].scanCode && macroTriggerListBufferSize == 1 )
  183. {
  184. triggerMacro->pos += comboLength * 2 + 1;
  185. // TODO check if TriggerMacro is finished, register ResultMacro
  186. return;
  187. }
  188. // If key does not match or more than 1 key pressed, reset the TriggerMacro combo position
  189. triggerMacro->pos = 0;
  190. return;
  191. }
  192. // Combo Case
  193. else
  194. {
  195. // TODO
  196. }
  197. }
  198. }
  199. }
  200. // Evaluate/Update ResultMacro
  201. void Macro_evalResultMacro( ResultMacro *resultMacro )
  202. {
  203. // TODO
  204. }
  205. // Macro Procesing Loop
  206. // Called once per USB buffer send
  207. inline void Macro_process()
  208. {
  209. // Only do one round of macro processing between Output Module timer sends
  210. if ( USBKeys_Sent != 0 )
  211. return;
  212. // If the pause flag is set, only process if the step counter is non-zero
  213. if ( macroPauseMode && macroStepCounter == 0 )
  214. {
  215. return;
  216. }
  217. // Proceed, decrementing the step counter
  218. else
  219. {
  220. macroStepCounter--;
  221. }
  222. // Loop through macro trigger buffer
  223. for ( uint8_t index = 0; index < macroTriggerListBufferSize; index++ )
  224. {
  225. // Get scanCode, first item of macroTriggerListBuffer pairs
  226. uint8_t scanCode = macroTriggerListBuffer[ index ].scanCode;
  227. // Lookup trigger list for this key
  228. unsigned int *triggerList = Macro_layerLookup( scanCode );
  229. // Skip, if no trigger list
  230. if ( triggerList == 0 )
  231. continue;
  232. // The first element is the length of the trigger list
  233. unsigned int triggerListSize = triggerList[0];
  234. // Loop through the trigger list
  235. for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
  236. {
  237. // Lookup TriggerMacro
  238. TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
  239. // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
  240. uint8_t state = macroTriggerListBuffer[ index ].state;
  241. // Evaluate Macro
  242. Macro_evalTriggerMacro( triggerMacro );
  243. }
  244. }
  245. /* TODO
  246. // Loop through input buffer
  247. for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
  248. {
  249. //print(" KEYS: ");
  250. //printInt8( KeyIndex_BufferUsed );
  251. // Get the keycode from the buffer
  252. uint8_t key = KeyIndex_Buffer[index];
  253. // Set the modifier bit if this key is a modifier
  254. if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
  255. {
  256. USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
  257. // Modifier processed, move on to the next key
  258. continue;
  259. }
  260. // Too many keys
  261. if ( USBKeys_Sent >= USBKeys_MaxSize )
  262. {
  263. warn_msg("USB Key limit reached");
  264. errorLED( 1 );
  265. break;
  266. }
  267. // Allow ignoring keys with 0's
  268. if ( key != 0 )
  269. {
  270. USBKeys_Array[USBKeys_Sent++] = key;
  271. }
  272. else
  273. {
  274. // Key was not mapped
  275. erro_msg( "Key not mapped... - " );
  276. printHex( key );
  277. errorLED( 1 );
  278. }
  279. }
  280. */
  281. // Signal buffer that we've used it TODO
  282. Scan_finishedWithMacro( 0 );
  283. //Scan_finishedWithBuffer( KeyIndex_BufferUsed );
  284. // If Macro debug mode is set, clear the USB Buffer
  285. if ( macroDebugMode )
  286. {
  287. USBKeys_Modifiers = 0;
  288. USBKeys_Sent = 0;
  289. }
  290. }
  291. inline void Macro_setup()
  292. {
  293. // Register Macro CLI dictionary
  294. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  295. // Disable Macro debug mode
  296. macroDebugMode = 0;
  297. // Disable Macro pause flag
  298. macroPauseMode = 0;
  299. // Set Macro step counter to zero
  300. macroStepCounter = 0;
  301. // Make sure macro trigger buffer is empty
  302. macroTriggerListBufferSize = 0;
  303. }
  304. // ----- CLI Command Functions -----
  305. void cliFunc_capList( char* args )
  306. {
  307. print( NL );
  308. info_msg("Capabilities List");
  309. // Iterate through all of the capabilities and display them
  310. for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
  311. {
  312. print( NL "\t" );
  313. printHex( cap );
  314. print(" - ");
  315. // Display/Lookup Capability Name (utilize debug mode of capability)
  316. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  317. capability( 0xFF, 0xFF, 0 );
  318. }
  319. }
  320. void cliFunc_capSelect( char* args )
  321. {
  322. // Parse code from argument
  323. char* curArgs;
  324. char* arg1Ptr;
  325. char* arg2Ptr = args;
  326. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  327. unsigned int totalArgs = 2; // Always at least two args
  328. unsigned int cap = 0;
  329. // Arguments used for keyboard capability function
  330. unsigned int argSetCount = 0;
  331. uint8_t *argSet = (uint8_t*)args;
  332. // Process all args
  333. for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
  334. {
  335. curArgs = arg2Ptr;
  336. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  337. // Stop processing args if no more are found
  338. // Extra arguments are ignored
  339. if ( *arg1Ptr == '\0' )
  340. break;
  341. // For the first argument, choose the capability
  342. if ( c == 0 ) switch ( arg1Ptr[0] )
  343. {
  344. // Keyboard Capability
  345. case 'K':
  346. // Determine capability index
  347. cap = decToInt( &arg1Ptr[1] );
  348. // Lookup the number of args
  349. totalArgs += CapabilitiesList[ cap ].argCount;
  350. continue;
  351. }
  352. // Because allocating memory isn't doable, and the argument count is arbitrary
  353. // The argument pointer is repurposed as the argument list (much smaller anyways)
  354. argSet[ argSetCount++ ] = (uint8_t)decToInt( arg1Ptr );
  355. // Once all the arguments are prepared, call the keyboard capability function
  356. if ( argSetCount == totalArgs )
  357. {
  358. // Indicate that the capability was called
  359. print( NL );
  360. info_msg("K");
  361. printInt8( cap );
  362. print(" - ");
  363. printHex( argSet[0] );
  364. print(" - ");
  365. printHex( argSet[1] );
  366. print(" - ");
  367. printHex( argSet[2] );
  368. print( "..." NL );
  369. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  370. capability( argSet[0], argSet[1], &argSet[2] );
  371. }
  372. }
  373. }
  374. void cliFunc_keyPress( char* args )
  375. {
  376. // Parse codes from arguments
  377. char* curArgs;
  378. char* arg1Ptr;
  379. char* arg2Ptr = args;
  380. // Process all args
  381. for ( ;; )
  382. {
  383. curArgs = arg2Ptr;
  384. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  385. // Stop processing args if no more are found
  386. if ( *arg1Ptr == '\0' )
  387. break;
  388. // Ignore non-Scancode numbers
  389. switch ( arg1Ptr[0] )
  390. {
  391. // Scancode
  392. case 'S':
  393. Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  394. break;
  395. }
  396. }
  397. }
  398. void cliFunc_keyRelease( char* args )
  399. {
  400. // Parse codes from arguments
  401. char* curArgs;
  402. char* arg1Ptr;
  403. char* arg2Ptr = args;
  404. // Process all args
  405. for ( ;; )
  406. {
  407. curArgs = arg2Ptr;
  408. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  409. // Stop processing args if no more are found
  410. if ( *arg1Ptr == '\0' )
  411. break;
  412. // Ignore non-Scancode numbers
  413. switch ( arg1Ptr[0] )
  414. {
  415. // Scancode
  416. case 'S':
  417. Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  418. break;
  419. }
  420. }
  421. }
  422. void cliFunc_layerList( char* args )
  423. {
  424. print( NL );
  425. info_msg("Layer List");
  426. // Iterate through all of the layers and display them
  427. for ( unsigned int layer = 0; layer < LayerNum; layer++ )
  428. {
  429. print( NL "\t" );
  430. printHex( layer );
  431. print(" - ");
  432. // Display layer name
  433. dPrint( LayerIndex[ layer ].name );
  434. // Default map
  435. if ( layer == 0 )
  436. print(" \033[1m(default)\033[0m");
  437. // Layer State
  438. print( NL "\t\t Layer State: " );
  439. printHex( LayerIndex[ layer ].state );
  440. // Max Index
  441. print(" Max Index: ");
  442. printHex( LayerIndex[ layer ].max );
  443. }
  444. }
  445. void cliFunc_layerState( char* args )
  446. {
  447. // Parse codes from arguments
  448. char* curArgs;
  449. char* arg1Ptr;
  450. char* arg2Ptr = args;
  451. uint8_t arg1 = 0;
  452. uint8_t arg2 = 0;
  453. // Process first two args
  454. for ( uint8_t c = 0; c < 2; c++ )
  455. {
  456. curArgs = arg2Ptr;
  457. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  458. // Stop processing args if no more are found
  459. if ( *arg1Ptr == '\0' )
  460. break;
  461. switch ( c )
  462. {
  463. // First argument (e.g. L1)
  464. case 0:
  465. if ( arg1Ptr[0] != 'L' )
  466. return;
  467. arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
  468. break;
  469. // Second argument (e.g. 4)
  470. case 1:
  471. arg2 = (uint8_t)decToInt( arg1Ptr );
  472. // Display operation (to indicate that it worked)
  473. print( NL );
  474. info_msg("Setting Layer L");
  475. printInt8( arg1 );
  476. print(" to - ");
  477. printHex( arg2 );
  478. // Set the layer state
  479. LayerIndex[ arg1 ].state = arg2;
  480. break;
  481. }
  482. }
  483. }
  484. void cliFunc_macroDebug( char* args )
  485. {
  486. // Toggle macro debug mode
  487. macroDebugMode = macroDebugMode ? 0 : 1;
  488. print( NL );
  489. info_msg("Macro Debug Mode: ");
  490. printInt8( macroDebugMode );
  491. }
  492. void cliFunc_macroList( char* args )
  493. {
  494. // Show available trigger macro indices
  495. print( NL );
  496. info_msg("Trigger Macros Range: T0 -> T");
  497. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  498. // Show available result macro indices
  499. print( NL );
  500. info_msg("Result Macros Range: R0 -> R");
  501. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  502. // Show Trigger to Result Macro Links
  503. print( NL );
  504. info_msg("Trigger : Result Macro Pairs");
  505. for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
  506. {
  507. print( NL );
  508. print("\tT");
  509. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  510. print(" : R");
  511. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  512. }
  513. }
  514. void cliFunc_macroProc( char* args )
  515. {
  516. // Toggle macro pause mode
  517. macroPauseMode = macroPauseMode ? 0 : 1;
  518. print( NL );
  519. info_msg("Macro Processing Mode: ");
  520. printInt8( macroPauseMode );
  521. }
  522. void macroDebugShowTrigger( unsigned int index )
  523. {
  524. // Only proceed if the macro exists
  525. if ( index >= TriggerMacroNum )
  526. return;
  527. // Trigger Macro Show
  528. TriggerMacro *macro = &TriggerMacroList[ index ];
  529. print( NL );
  530. info_msg("Trigger Macro Index: ");
  531. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  532. print( NL );
  533. // Read the comboLength for combo in the sequence (sequence of combos)
  534. unsigned int pos = 0;
  535. uint8_t comboLength = macro->guide[ pos ];
  536. // Iterate through and interpret the guide
  537. while ( comboLength != 0 )
  538. {
  539. // Initial position of the combo
  540. unsigned int comboPos = ++pos;
  541. // Iterate through the combo
  542. while ( pos < comboLength * TriggerGuideSize + comboPos )
  543. {
  544. // Assign TriggerGuide element (key type, state and scancode)
  545. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  546. // Display guide information about trigger key
  547. printHex( guide->scanCode );
  548. print("|");
  549. printHex( guide->type );
  550. print("|");
  551. printHex( guide->state );
  552. // Increment position
  553. pos += TriggerGuideSize;
  554. // Only show combo separator if there are combos left in the sequence element
  555. if ( pos < comboLength * TriggerGuideSize + comboPos )
  556. print("+");
  557. }
  558. // Read the next comboLength
  559. comboLength = macro->guide[ pos ];
  560. // Only show sequence separator if there is another combo to process
  561. if ( comboLength != 0 )
  562. print(";");
  563. }
  564. // Display current position
  565. print( NL "Position: " );
  566. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  567. // Display result macro index
  568. print( NL "Result Macro Index: " );
  569. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  570. }
  571. void macroDebugShowResult( unsigned int index )
  572. {
  573. // Only proceed if the macro exists
  574. if ( index >= ResultMacroNum )
  575. return;
  576. // Trigger Macro Show
  577. ResultMacro *macro = &ResultMacroList[ index ];
  578. print( NL );
  579. info_msg("Result Macro Index: ");
  580. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  581. print( NL );
  582. // Read the comboLength for combo in the sequence (sequence of combos)
  583. unsigned int pos = 0;
  584. uint8_t comboLength = macro->guide[ pos++ ];
  585. // Iterate through and interpret the guide
  586. while ( comboLength != 0 )
  587. {
  588. // Function Counter, used to keep track of the combos processed
  589. unsigned int funcCount = 0;
  590. // Iterate through the combo
  591. while ( funcCount < comboLength )
  592. {
  593. // Assign TriggerGuide element (key type, state and scancode)
  594. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  595. // Display Function Index
  596. printHex( guide->index );
  597. print("|");
  598. // Display Function Ptr Address
  599. printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
  600. print("|");
  601. // Display/Lookup Capability Name (utilize debug mode of capability)
  602. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  603. capability( 0xFF, 0xFF, 0 );
  604. // Display Argument(s)
  605. print("(");
  606. for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  607. {
  608. // Arguments are only 8 bit values
  609. printHex( (&guide->args)[ arg ] );
  610. // Only show arg separator if there are args left
  611. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  612. print(",");
  613. }
  614. print(")");
  615. // Increment position
  616. pos += ResultGuideSize( guide );
  617. // Increment function count
  618. funcCount++;
  619. // Only show combo separator if there are combos left in the sequence element
  620. if ( funcCount < comboLength )
  621. print("+");
  622. }
  623. // Read the next comboLength
  624. comboLength = macro->guide[ pos++ ];
  625. // Only show sequence separator if there is another combo to process
  626. if ( comboLength != 0 )
  627. print(";");
  628. }
  629. // Display current position
  630. print( NL "Position: " );
  631. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  632. // Display final trigger state/type
  633. print( NL "Final Trigger State (State/Type): " );
  634. printHex( macro->state );
  635. print("/");
  636. printHex( macro->stateType );
  637. }
  638. void cliFunc_macroShow( char* args )
  639. {
  640. // Parse codes from arguments
  641. char* curArgs;
  642. char* arg1Ptr;
  643. char* arg2Ptr = args;
  644. // Process all args
  645. for ( ;; )
  646. {
  647. curArgs = arg2Ptr;
  648. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  649. // Stop processing args if no more are found
  650. if ( *arg1Ptr == '\0' )
  651. break;
  652. // Ignore invalid codes
  653. switch ( arg1Ptr[0] )
  654. {
  655. // Indexed Trigger Macro
  656. case 'T':
  657. macroDebugShowTrigger( decToInt( &arg1Ptr[1] ) );
  658. break;
  659. // Indexed Result Macro
  660. case 'R':
  661. macroDebugShowResult( decToInt( &arg1Ptr[1] ) );
  662. break;
  663. }
  664. }
  665. }
  666. void cliFunc_macroStep( char* args )
  667. {
  668. // Parse number from argument
  669. // NOTE: Only first argument is used
  670. char* arg1Ptr;
  671. char* arg2Ptr;
  672. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  673. // Set the macro step counter, negative int's are cast to uint
  674. macroStepCounter = (unsigned int)decToInt( arg1Ptr );
  675. }