Kiibohd Controller
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 the 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 += 2 )
  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. /*
  201. inline void Macro_bufferAdd( uint8_t byte )
  202. {
  203. // Make sure we haven't overflowed the key buffer
  204. // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
  205. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
  206. {
  207. uint8_t key = DefaultMap_Lookup[byte];
  208. for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
  209. {
  210. // Key already in the buffer
  211. if ( KeyIndex_Buffer[c] == key )
  212. return;
  213. }
  214. // Add to the buffer
  215. KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
  216. }
  217. }
  218. inline void Macro_bufferRemove( uint8_t byte )
  219. {
  220. uint8_t key = DefaultMap_Lookup[byte];
  221. // Check for the released key, and shift the other keys lower on the buffer
  222. for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
  223. {
  224. // Key to release found
  225. if ( KeyIndex_Buffer[c] == key )
  226. {
  227. // Shift keys from c position
  228. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  229. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  230. // Decrement Buffer
  231. KeyIndex_BufferUsed--;
  232. return;
  233. }
  234. }
  235. // Error case (no key to release)
  236. erro_msg("Could not find key to release: ");
  237. printHex( key );
  238. }
  239. */
  240. inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
  241. {
  242. }
  243. inline void Macro_process()
  244. {
  245. // Only do one round of macro processing between Output Module timer sends
  246. if ( USBKeys_Sent != 0 )
  247. return;
  248. // If the pause flag is set, only process if the step counter is non-zero
  249. if ( macroPauseMode && macroStepCounter == 0 )
  250. {
  251. return;
  252. }
  253. // Proceed, decrementing the step counter
  254. else
  255. {
  256. macroStepCounter--;
  257. }
  258. // Loop through macro trigger buffer
  259. for ( uint8_t index = 0; index < macroTriggerListBufferSize; index++ )
  260. {
  261. // Get scanCode, first item of macroTriggerListBuffer pairs
  262. uint8_t scanCode = macroTriggerListBuffer[ index ].scanCode;
  263. // Lookup trigger list for this key
  264. unsigned int *triggerList = Macro_layerLookup( scanCode );
  265. // The first element is the length of the trigger list
  266. unsigned int triggerListSize = triggerList[0];
  267. // Loop through the trigger list
  268. for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
  269. {
  270. // Lookup TriggerMacro
  271. TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
  272. // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
  273. uint8_t state = macroTriggerListBuffer[ index ].state;
  274. // Evaluate Macro
  275. Macro_evalTriggerMacro( triggerMacro );
  276. }
  277. }
  278. /* TODO
  279. // Loop through input buffer
  280. for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
  281. {
  282. //print(" KEYS: ");
  283. //printInt8( KeyIndex_BufferUsed );
  284. // Get the keycode from the buffer
  285. uint8_t key = KeyIndex_Buffer[index];
  286. // Set the modifier bit if this key is a modifier
  287. if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
  288. {
  289. USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
  290. // Modifier processed, move on to the next key
  291. continue;
  292. }
  293. // Too many keys
  294. if ( USBKeys_Sent >= USBKeys_MaxSize )
  295. {
  296. warn_msg("USB Key limit reached");
  297. errorLED( 1 );
  298. break;
  299. }
  300. // Allow ignoring keys with 0's
  301. if ( key != 0 )
  302. {
  303. USBKeys_Array[USBKeys_Sent++] = key;
  304. }
  305. else
  306. {
  307. // Key was not mapped
  308. erro_msg( "Key not mapped... - " );
  309. printHex( key );
  310. errorLED( 1 );
  311. }
  312. }
  313. */
  314. // Signal buffer that we've used it
  315. Scan_finishedWithBuffer( KeyIndex_BufferUsed );
  316. // If Macro debug mode is set, clear the USB Buffer
  317. if ( macroDebugMode )
  318. {
  319. USBKeys_Modifiers = 0;
  320. USBKeys_Sent = 0;
  321. }
  322. }
  323. inline void Macro_setup()
  324. {
  325. // Register Macro CLI dictionary
  326. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  327. // Disable Macro debug mode
  328. macroDebugMode = 0;
  329. // Disable Macro pause flag
  330. macroPauseMode = 0;
  331. // Set Macro step counter to zero
  332. macroStepCounter = 0;
  333. // Make sure macro trigger buffer is empty
  334. macroTriggerListBufferSize = 0;
  335. }
  336. // ----- CLI Command Functions -----
  337. void cliFunc_capList( char* args )
  338. {
  339. print( NL );
  340. info_msg("Capabilities List");
  341. // Iterate through all of the capabilities and display them
  342. for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
  343. {
  344. print( NL "\t" );
  345. printHex( cap );
  346. print(" - ");
  347. // Display/Lookup Capability Name (utilize debug mode of capability)
  348. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  349. capability( 0xFF, 0xFF, 0 );
  350. }
  351. }
  352. void cliFunc_capSelect( char* args )
  353. {
  354. // Parse code from argument
  355. char* curArgs;
  356. char* arg1Ptr;
  357. char* arg2Ptr = args;
  358. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  359. unsigned int totalArgs = 2; // Always at least two args
  360. unsigned int cap = 0;
  361. // Arguments used for keyboard capability function
  362. unsigned int argSetCount = 0;
  363. uint8_t *argSet = (uint8_t*)args;
  364. // Process all args
  365. for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
  366. {
  367. curArgs = arg2Ptr;
  368. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  369. // Stop processing args if no more are found
  370. // Extra arguments are ignored
  371. if ( *arg1Ptr == '\0' )
  372. break;
  373. // For the first argument, choose the capability
  374. if ( c == 0 ) switch ( arg1Ptr[0] )
  375. {
  376. // Keyboard Capability
  377. case 'K':
  378. // Determine capability index
  379. cap = decToInt( &arg1Ptr[1] );
  380. // Lookup the number of args
  381. totalArgs += CapabilitiesList[ cap ].argCount;
  382. continue;
  383. }
  384. // Because allocating memory isn't doable, and the argument count is arbitrary
  385. // The argument pointer is repurposed as the argument list (much smaller anyways)
  386. argSet[ argSetCount++ ] = (uint8_t)decToInt( arg1Ptr );
  387. // Once all the arguments are prepared, call the keyboard capability function
  388. if ( argSetCount == totalArgs )
  389. {
  390. // Indicate that the capability was called
  391. print( NL );
  392. info_msg("K");
  393. printInt8( cap );
  394. print(" - ");
  395. printHex( argSet[0] );
  396. print(" - ");
  397. printHex( argSet[1] );
  398. print(" - ");
  399. printHex( argSet[2] );
  400. print( "..." NL );
  401. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  402. capability( argSet[0], argSet[1], &argSet[2] );
  403. }
  404. }
  405. }
  406. void cliFunc_keyPress( char* args )
  407. {
  408. // Parse codes from arguments
  409. char* curArgs;
  410. char* arg1Ptr;
  411. char* arg2Ptr = args;
  412. // Process all args
  413. for ( ;; )
  414. {
  415. curArgs = arg2Ptr;
  416. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  417. // Stop processing args if no more are found
  418. if ( *arg1Ptr == '\0' )
  419. break;
  420. // Ignore non-Scancode numbers
  421. switch ( arg1Ptr[0] )
  422. {
  423. // Scancode
  424. case 'S':
  425. Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  426. break;
  427. }
  428. }
  429. }
  430. void cliFunc_keyRelease( char* args )
  431. {
  432. // Parse codes from arguments
  433. char* curArgs;
  434. char* arg1Ptr;
  435. char* arg2Ptr = args;
  436. // Process all args
  437. for ( ;; )
  438. {
  439. curArgs = arg2Ptr;
  440. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  441. // Stop processing args if no more are found
  442. if ( *arg1Ptr == '\0' )
  443. break;
  444. // Ignore non-Scancode numbers
  445. switch ( arg1Ptr[0] )
  446. {
  447. // Scancode
  448. case 'S':
  449. Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  450. break;
  451. }
  452. }
  453. }
  454. void cliFunc_layerList( char* args )
  455. {
  456. print( NL );
  457. info_msg("Layer List");
  458. // Iterate through all of the layers and display them
  459. for ( unsigned int layer = 0; layer < LayerNum; layer++ )
  460. {
  461. print( NL "\t" );
  462. printHex( layer );
  463. print(" - ");
  464. // Display layer name
  465. dPrint( LayerIndex[ layer ].name );
  466. // Default map
  467. if ( layer == 0 )
  468. print(" \033[1m(default)\033[0m");
  469. // Layer State
  470. print( NL "\t\t Layer State: " );
  471. printHex( LayerIndex[ layer ].state );
  472. // Max Index
  473. print(" Max Index: ");
  474. printHex( LayerIndex[ layer ].max );
  475. }
  476. }
  477. void cliFunc_layerState( char* args )
  478. {
  479. // Parse codes from arguments
  480. char* curArgs;
  481. char* arg1Ptr;
  482. char* arg2Ptr = args;
  483. uint8_t arg1 = 0;
  484. uint8_t arg2 = 0;
  485. // Process first two args
  486. for ( uint8_t c = 0; c < 2; c++ )
  487. {
  488. curArgs = arg2Ptr;
  489. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  490. // Stop processing args if no more are found
  491. if ( *arg1Ptr == '\0' )
  492. break;
  493. switch ( c )
  494. {
  495. // First argument (e.g. L1)
  496. case 0:
  497. if ( arg1Ptr[0] != 'L' )
  498. return;
  499. arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
  500. break;
  501. // Second argument (e.g. 4)
  502. case 1:
  503. arg2 = (uint8_t)decToInt( arg1Ptr );
  504. // Display operation (to indicate that it worked)
  505. print( NL );
  506. info_msg("Setting Layer L");
  507. printInt8( arg1 );
  508. print(" to - ");
  509. printHex( arg2 );
  510. // Set the layer state
  511. LayerIndex[ arg1 ].state = arg2;
  512. break;
  513. }
  514. }
  515. }
  516. void cliFunc_macroDebug( char* args )
  517. {
  518. // Toggle macro debug mode
  519. macroDebugMode = macroDebugMode ? 0 : 1;
  520. print( NL );
  521. info_msg("Macro Debug Mode: ");
  522. printInt8( macroDebugMode );
  523. }
  524. void cliFunc_macroList( char* args )
  525. {
  526. // Show available trigger macro indices
  527. print( NL );
  528. info_msg("Trigger Macros Range: T0 -> T");
  529. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  530. // Show available result macro indices
  531. print( NL );
  532. info_msg("Result Macros Range: R0 -> R");
  533. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  534. // Show Trigger to Result Macro Links
  535. print( NL );
  536. info_msg("Trigger : Result Macro Pairs");
  537. for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
  538. {
  539. print( NL );
  540. print("\tT");
  541. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  542. print(" : R");
  543. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  544. }
  545. }
  546. void cliFunc_macroProc( char* args )
  547. {
  548. // Toggle macro pause mode
  549. macroPauseMode = macroPauseMode ? 0 : 1;
  550. print( NL );
  551. info_msg("Macro Processing Mode: ");
  552. printInt8( macroPauseMode );
  553. }
  554. void macroDebugShowTrigger( unsigned int index )
  555. {
  556. // Only proceed if the macro exists
  557. if ( index >= TriggerMacroNum )
  558. return;
  559. // Trigger Macro Show
  560. TriggerMacro *macro = &TriggerMacroList[ index ];
  561. print( NL );
  562. info_msg("Trigger Macro Index: ");
  563. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  564. print( NL );
  565. // Read the comboLength for combo in the sequence (sequence of combos)
  566. unsigned int pos = 0;
  567. uint8_t comboLength = macro->guide[ pos ];
  568. // Iterate through and interpret the guide
  569. while ( comboLength != 0 )
  570. {
  571. // Initial position of the combo
  572. unsigned int comboPos = ++pos;
  573. // Iterate through the combo
  574. while ( pos < comboLength * TriggerGuideSize + comboPos )
  575. {
  576. // Assign TriggerGuide element (key type, state and scancode)
  577. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  578. // Display guide information about trigger key
  579. printHex( guide->scanCode );
  580. print("|");
  581. printHex( guide->type );
  582. print("|");
  583. printHex( guide->state );
  584. // Increment position
  585. pos += TriggerGuideSize;
  586. // Only show combo separator if there are combos left in the sequence element
  587. if ( pos < comboLength * TriggerGuideSize + comboPos )
  588. print("+");
  589. }
  590. // Read the next comboLength
  591. comboLength = macro->guide[ pos ];
  592. // Only show sequence separator if there is another combo to process
  593. if ( comboLength != 0 )
  594. print(";");
  595. }
  596. // Display current position
  597. print( NL "Position: " );
  598. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  599. // Display result macro index
  600. print( NL "Result Macro Index: " );
  601. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  602. }
  603. void macroDebugShowResult( unsigned int index )
  604. {
  605. // Only proceed if the macro exists
  606. if ( index >= ResultMacroNum )
  607. return;
  608. // Trigger Macro Show
  609. ResultMacro *macro = &ResultMacroList[ index ];
  610. print( NL );
  611. info_msg("Result Macro Index: ");
  612. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  613. print( NL );
  614. // Read the comboLength for combo in the sequence (sequence of combos)
  615. unsigned int pos = 0;
  616. uint8_t comboLength = macro->guide[ pos++ ];
  617. // Iterate through and interpret the guide
  618. while ( comboLength != 0 )
  619. {
  620. // Function Counter, used to keep track of the combos processed
  621. unsigned int funcCount = 0;
  622. // Iterate through the combo
  623. while ( funcCount < comboLength )
  624. {
  625. // Assign TriggerGuide element (key type, state and scancode)
  626. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  627. // Display Function Index
  628. printHex( guide->index );
  629. print("|");
  630. // Display Function Ptr Address
  631. printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
  632. print("|");
  633. // Display/Lookup Capability Name (utilize debug mode of capability)
  634. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  635. capability( 0xFF, 0xFF, 0 );
  636. // Display Argument(s)
  637. print("(");
  638. for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  639. {
  640. // Arguments are only 8 bit values
  641. printHex( (&guide->args)[ arg ] );
  642. // Only show arg separator if there are args left
  643. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  644. print(",");
  645. }
  646. print(")");
  647. // Increment position
  648. pos += ResultGuideSize( guide );
  649. // Increment function count
  650. funcCount++;
  651. // Only show combo separator if there are combos left in the sequence element
  652. if ( funcCount < comboLength )
  653. print("+");
  654. }
  655. // Read the next comboLength
  656. comboLength = macro->guide[ pos++ ];
  657. // Only show sequence separator if there is another combo to process
  658. if ( comboLength != 0 )
  659. print(";");
  660. }
  661. // Display current position
  662. print( NL "Position: " );
  663. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  664. // Display final trigger state/type
  665. print( NL "Final Trigger State (State/Type): " );
  666. printHex( macro->state );
  667. print("/");
  668. printHex( macro->stateType );
  669. }
  670. void cliFunc_macroShow( char* args )
  671. {
  672. // Parse codes from arguments
  673. char* curArgs;
  674. char* arg1Ptr;
  675. char* arg2Ptr = args;
  676. // Process all args
  677. for ( ;; )
  678. {
  679. curArgs = arg2Ptr;
  680. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  681. // Stop processing args if no more are found
  682. if ( *arg1Ptr == '\0' )
  683. break;
  684. // Ignore invalid codes
  685. switch ( arg1Ptr[0] )
  686. {
  687. // Indexed Trigger Macro
  688. case 'T':
  689. macroDebugShowTrigger( decToInt( &arg1Ptr[1] ) );
  690. break;
  691. // Indexed Result Macro
  692. case 'R':
  693. macroDebugShowResult( decToInt( &arg1Ptr[1] ) );
  694. break;
  695. }
  696. }
  697. }
  698. void cliFunc_macroStep( char* args )
  699. {
  700. // Parse number from argument
  701. // NOTE: Only first argument is used
  702. char* arg1Ptr;
  703. char* arg2Ptr;
  704. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  705. // Set the macro step counter, negative int's are cast to uint
  706. macroStepCounter = (unsigned int)decToInt( arg1Ptr );
  707. }