Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

macro.c 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /* Copyright (C) 2014-2016 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. // Keymaps
  25. #include "usb_hid.h"
  26. #include <generatedKeymap.h> // Generated using kll at compile time, in build directory
  27. // Connect Includes
  28. #if defined(ConnectEnabled_define)
  29. #include <connect_scan.h>
  30. #endif
  31. // Local Includes
  32. #include "trigger.h"
  33. #include "result.h"
  34. #include "macro.h"
  35. // ----- Function Declarations -----
  36. void cliFunc_capList ( char* args );
  37. void cliFunc_capSelect ( char* args );
  38. void cliFunc_keyHold ( char* args );
  39. void cliFunc_keyPress ( char* args );
  40. void cliFunc_keyRelease( char* args );
  41. void cliFunc_layerDebug( char* args );
  42. void cliFunc_layerList ( char* args );
  43. void cliFunc_layerState( char* args );
  44. void cliFunc_macroDebug( char* args );
  45. void cliFunc_macroList ( char* args );
  46. void cliFunc_macroProc ( char* args );
  47. void cliFunc_macroShow ( char* args );
  48. void cliFunc_macroStep ( char* args );
  49. // ----- Variables -----
  50. // Macro Module command dictionary
  51. CLIDict_Entry( capList, "Prints an indexed list of all non USB keycode capabilities." );
  52. CLIDict_Entry( capSelect, "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B" );
  53. CLIDict_Entry( keyHold, "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
  54. CLIDict_Entry( keyPress, "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
  55. CLIDict_Entry( keyRelease, "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
  56. CLIDict_Entry( layerDebug, "Layer debug mode. Shows layer stack and any changes." );
  57. CLIDict_Entry( layerList, "List available layers." );
  58. CLIDict_Entry( 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" );
  59. CLIDict_Entry( macroDebug, "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes." );
  60. CLIDict_Entry( macroList, "List the defined trigger and result macros." );
  61. CLIDict_Entry( macroProc, "Pause/Resume macro processing." );
  62. CLIDict_Entry( 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" );
  63. CLIDict_Entry( macroStep, "Do N macro processing steps. Defaults to 1." );
  64. CLIDict_Def( macroCLIDict, "Macro Module Commands" ) = {
  65. CLIDict_Item( capList ),
  66. CLIDict_Item( capSelect ),
  67. CLIDict_Item( keyHold ),
  68. CLIDict_Item( keyPress ),
  69. CLIDict_Item( keyRelease ),
  70. CLIDict_Item( layerDebug ),
  71. CLIDict_Item( layerList ),
  72. CLIDict_Item( layerState ),
  73. CLIDict_Item( macroDebug ),
  74. CLIDict_Item( macroList ),
  75. CLIDict_Item( macroProc ),
  76. CLIDict_Item( macroShow ),
  77. CLIDict_Item( macroStep ),
  78. { 0, 0, 0 } // Null entry for dictionary end
  79. };
  80. // Layer debug flag - If set, displays any changes to layers and the full layer stack on change
  81. uint8_t layerDebugMode = 0;
  82. // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
  83. uint8_t macroDebugMode = 0;
  84. // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
  85. uint8_t macroPauseMode = 0;
  86. // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
  87. uint16_t macroStepCounter = 0;
  88. // Key Trigger List Buffer and Layer Cache
  89. // The layer cache is set on press only, hold and release events refer to the value set on press
  90. TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
  91. var_uint_t macroTriggerListBufferSize = 0;
  92. var_uint_t macroTriggerListLayerCache[ MaxScanCode ];
  93. // Layer Index Stack
  94. // * When modifying layer state and the state is non-0x0, the stack must be adjusted
  95. index_uint_t macroLayerIndexStack[ LayerNum + 1 ] = { 0 };
  96. index_uint_t macroLayerIndexStackSize = 0;
  97. // TODO REMOVE when dependency no longer exists
  98. extern index_uint_t macroResultMacroPendingList[];
  99. extern index_uint_t macroResultMacroPendingListSize;
  100. extern index_uint_t macroTriggerMacroPendingList[];
  101. extern index_uint_t macroTriggerMacroPendingListSize;
  102. // Interconnect ScanCode Cache
  103. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  104. // TODO This can be shrunk by the size of the max node 0 ScanCode
  105. TriggerGuide macroInterconnectCache[ MaxScanCode ];
  106. uint8_t macroInterconnectCacheSize = 0;
  107. #endif
  108. // ----- Capabilities -----
  109. // Sets the given layer with the specified layerState
  110. void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t layerState )
  111. {
  112. // Ignore if layer does not exist or trying to manipulate layer 0/Default layer
  113. if ( layer >= LayerNum || layer == 0 )
  114. return;
  115. // Is layer in the LayerIndexStack?
  116. uint8_t inLayerIndexStack = 0;
  117. uint16_t stackItem = 0;
  118. while ( stackItem < macroLayerIndexStackSize )
  119. {
  120. // Flag if layer is already in the LayerIndexStack
  121. if ( macroLayerIndexStack[ stackItem ] == layer )
  122. {
  123. inLayerIndexStack = 1;
  124. break;
  125. }
  126. // Increment to next item
  127. stackItem++;
  128. }
  129. // Toggle Layer State Byte
  130. if ( LayerState[ layer ] & layerState )
  131. {
  132. // Unset
  133. LayerState[ layer ] &= ~layerState;
  134. }
  135. else
  136. {
  137. // Set
  138. LayerState[ layer ] |= layerState;
  139. }
  140. // If the layer was not in the LayerIndexStack add it
  141. if ( !inLayerIndexStack )
  142. {
  143. macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
  144. }
  145. // If the layer is in the LayerIndexStack and the state is 0x00, remove
  146. if ( LayerState[ layer ] == 0x00 && inLayerIndexStack )
  147. {
  148. // Remove the layer from the LayerIndexStack
  149. // Using the already positioned stackItem variable from the loop above
  150. while ( stackItem < macroLayerIndexStackSize )
  151. {
  152. macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
  153. stackItem++;
  154. }
  155. // Reduce LayerIndexStack size
  156. macroLayerIndexStackSize--;
  157. }
  158. // Layer Debug Mode
  159. if ( layerDebugMode )
  160. {
  161. dbug_msg("Layer ");
  162. // Iterate over each of the layers displaying the state as a hex value
  163. for ( index_uint_t index = 0; index < LayerNum; index++ )
  164. {
  165. printHex_op( LayerState[ index ], 0 );
  166. }
  167. // Always show the default layer (it's always 0)
  168. print(" 0");
  169. // Iterate over the layer stack starting from the bottom of the stack
  170. for ( index_uint_t index = macroLayerIndexStackSize; index > 0; index-- )
  171. {
  172. print(":");
  173. printHex_op( macroLayerIndexStack[ index - 1 ], 0 );
  174. }
  175. print( NL );
  176. }
  177. }
  178. // Modifies the specified Layer control byte
  179. // Argument #1: Layer Index -> uint16_t
  180. // Argument #2: Layer State -> uint8_t
  181. void Macro_layerState_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  182. {
  183. // Display capability name
  184. if ( stateType == 0xFF && state == 0xFF )
  185. {
  186. print("Macro_layerState(layerIndex,layerState)");
  187. return;
  188. }
  189. // Only use capability on press or release
  190. // TODO Analog
  191. // XXX This may cause issues, might be better to implement state table here to decide -HaaTa
  192. if ( stateType == 0x00 && state == 0x02 ) // Hold condition
  193. return;
  194. // Get layer index from arguments
  195. // Cast pointer to uint8_t to uint16_t then access that memory location
  196. uint16_t layer = *(uint16_t*)(&args[0]);
  197. // Get layer toggle byte
  198. uint8_t layerState = args[ sizeof(uint16_t) ];
  199. Macro_layerState( state, stateType, layer, layerState );
  200. }
  201. // Latches given layer
  202. // Argument #1: Layer Index -> uint16_t
  203. void Macro_layerLatch_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  204. {
  205. // Display capability name
  206. if ( stateType == 0xFF && state == 0xFF )
  207. {
  208. print("Macro_layerLatch(layerIndex)");
  209. return;
  210. }
  211. // Only use capability on press
  212. // TODO Analog
  213. if ( stateType == 0x00 && state != 0x03 ) // Only on release
  214. return;
  215. // Get layer index from arguments
  216. // Cast pointer to uint8_t to uint16_t then access that memory location
  217. uint16_t layer = *(uint16_t*)(&args[0]);
  218. Macro_layerState( state, stateType, layer, 0x02 );
  219. }
  220. // Locks given layer
  221. // Argument #1: Layer Index -> uint16_t
  222. void Macro_layerLock_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  223. {
  224. // Display capability name
  225. if ( stateType == 0xFF && state == 0xFF )
  226. {
  227. print("Macro_layerLock(layerIndex)");
  228. return;
  229. }
  230. // Only use capability on press
  231. // TODO Analog
  232. // XXX Could also be on release, but that's sorta dumb -HaaTa
  233. if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
  234. return;
  235. // Get layer index from arguments
  236. // Cast pointer to uint8_t to uint16_t then access that memory location
  237. uint16_t layer = *(uint16_t*)(&args[0]);
  238. Macro_layerState( state, stateType, layer, 0x04 );
  239. }
  240. // Shifts given layer
  241. // Argument #1: Layer Index -> uint16_t
  242. void Macro_layerShift_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  243. {
  244. // Display capability name
  245. if ( stateType == 0xFF && state == 0xFF )
  246. {
  247. print("Macro_layerShift(layerIndex)");
  248. return;
  249. }
  250. // Only use capability on press or release
  251. // TODO Analog
  252. if ( stateType == 0x00 && ( state == 0x00 || state == 0x02 ) ) // Only pass press or release conditions
  253. return;
  254. // Get layer index from arguments
  255. // Cast pointer to uint8_t to uint16_t then access that memory location
  256. uint16_t layer = *(uint16_t*)(&args[0]);
  257. // Only set the layer if it is disabled
  258. if ( LayerState[ layer ] != 0x00 && state == 0x01 )
  259. return;
  260. // Only unset the layer if it is enabled
  261. if ( LayerState[ layer ] == 0x00 && state == 0x03 )
  262. return;
  263. Macro_layerState( state, stateType, layer, 0x01 );
  264. }
  265. // Rotate layer to next/previous
  266. // Uses state variable to keep track of the current layer position
  267. // Layers are still evaluated using the layer stack
  268. uint16_t Macro_rotationLayer;
  269. void Macro_layerRotate_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  270. {
  271. // Display capability name
  272. if ( stateType == 0xFF && state == 0xFF )
  273. {
  274. print("Macro_layerRotate(previous)");
  275. return;
  276. }
  277. // Only use capability on press
  278. // TODO Analog
  279. // XXX Could also be on release, but that's sorta dumb -HaaTa
  280. if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
  281. return;
  282. // Unset previous rotation layer if not 0
  283. if ( Macro_rotationLayer != 0 )
  284. {
  285. Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
  286. }
  287. // Get direction of rotation, 0, next, non-zero previous
  288. uint8_t direction = *args;
  289. // Next
  290. if ( !direction )
  291. {
  292. Macro_rotationLayer++;
  293. // Invalid layer
  294. if ( Macro_rotationLayer >= LayerNum )
  295. Macro_rotationLayer = 0;
  296. }
  297. // Previous
  298. else
  299. {
  300. Macro_rotationLayer--;
  301. // Layer wrap
  302. if ( Macro_rotationLayer >= LayerNum )
  303. Macro_rotationLayer = LayerNum - 1;
  304. }
  305. // Toggle the computed layer rotation
  306. Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
  307. }
  308. // ----- Functions -----
  309. // Looks up the trigger list for the given scan code (from the active layer)
  310. // NOTE: Calling function must handle the NULL pointer case
  311. nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire )
  312. {
  313. uint8_t scanCode = guide->scanCode;
  314. // TODO Analog
  315. // If a normal key, and not pressed, do a layer cache lookup
  316. if ( guide->type == 0x00 && guide->state != 0x01 )
  317. {
  318. // Cached layer
  319. var_uint_t cachedLayer = macroTriggerListLayerCache[ scanCode ];
  320. // Lookup map, then layer
  321. nat_ptr_t **map = (nat_ptr_t**)LayerIndex[ cachedLayer ].triggerMap;
  322. const Layer *layer = &LayerIndex[ cachedLayer ];
  323. // Cache trigger list before attempting to expire latch
  324. nat_ptr_t *trigger_list = map[ scanCode - layer->first ];
  325. // Check if latch has been pressed for this layer
  326. uint8_t latch = LayerState[ cachedLayer ] & 0x02;
  327. if ( latch && latch_expire )
  328. {
  329. Macro_layerState( 0, 0, cachedLayer, 0x02 );
  330. #if defined(ConnectEnabled_define) && defined(LCDEnabled_define)
  331. // Evaluate the layerStack capability if available (LCD + Interconnect)
  332. extern void LCD_layerStack_capability( uint8_t state, uint8_t stateType, uint8_t *args );
  333. LCD_layerStack_capability( 0, 0, 0 );
  334. #endif
  335. }
  336. return trigger_list;
  337. }
  338. // If no trigger macro is defined at the given layer, fallthrough to the next layer
  339. for ( uint16_t layerIndex = macroLayerIndexStackSize; layerIndex != 0xFFFF; layerIndex-- )
  340. {
  341. // Lookup Layer
  342. const Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
  343. // Check if latch has been pressed for this layer
  344. // XXX Regardless of whether a key is found, the latch is removed on first lookup
  345. uint8_t latch = LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x02;
  346. if ( latch && latch_expire )
  347. {
  348. Macro_layerState( 0, 0, macroLayerIndexStack[ layerIndex ], 0x02 );
  349. }
  350. // Only use layer, if state is valid
  351. // XOR each of the state bits
  352. // If only two are enabled, do not use this state
  353. if ( (LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x01) ^ (latch>>1) ^ ((LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x04)>>2) )
  354. {
  355. // Lookup layer
  356. nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
  357. // Determine if layer has key defined
  358. // Make sure scanCode is between layer first and last scancodes
  359. if ( map != 0
  360. && scanCode <= layer->last
  361. && scanCode >= layer->first
  362. && *map[ scanCode - layer->first ] != 0 )
  363. {
  364. // Set the layer cache
  365. macroTriggerListLayerCache[ scanCode ] = macroLayerIndexStack[ layerIndex ];
  366. return map[ scanCode - layer->first ];
  367. }
  368. }
  369. }
  370. // Do lookup on default layer
  371. nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
  372. // Lookup default layer
  373. const Layer *layer = &LayerIndex[0];
  374. // Make sure scanCode is between layer first and last scancodes
  375. if ( map != 0
  376. && scanCode <= layer->last
  377. && scanCode >= layer->first
  378. && *map[ scanCode - layer->first ] != 0 )
  379. {
  380. // Set the layer cache to default map
  381. macroTriggerListLayerCache[ scanCode ] = 0;
  382. return map[ scanCode - layer->first ];
  383. }
  384. // Otherwise no defined Trigger Macro
  385. erro_msg("Scan Code has no defined Trigger Macro: ");
  386. printHex( scanCode );
  387. print( NL );
  388. return 0;
  389. }
  390. // Add an interconnect ScanCode
  391. // These are handled differently (less information is sent, hold/off states must be assumed)
  392. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  393. void Macro_pressReleaseAdd( void *trigger_ptr )
  394. {
  395. TriggerGuide *trigger = (TriggerGuide*)trigger_ptr;
  396. // Error checking
  397. uint8_t error = 0;
  398. switch ( trigger->type )
  399. {
  400. case 0x00: // Normal key
  401. switch ( trigger->state )
  402. {
  403. case 0x00:
  404. case 0x01:
  405. case 0x02:
  406. case 0x03:
  407. break;
  408. default:
  409. erro_msg("Invalid key state - ");
  410. error = 1;
  411. break;
  412. }
  413. break;
  414. // Invalid TriggerGuide type
  415. default:
  416. erro_msg("Invalid type - ");
  417. error = 1;
  418. break;
  419. }
  420. // Check if ScanCode is out of range
  421. if ( trigger->scanCode > MaxScanCode )
  422. {
  423. warn_msg("ScanCode is out of range/not defined - ");
  424. error = 1;
  425. }
  426. // Display TriggerGuide
  427. if ( error )
  428. {
  429. printHex( trigger->type );
  430. print(" ");
  431. printHex( trigger->state );
  432. print(" ");
  433. printHex( trigger->scanCode );
  434. print( NL );
  435. return;
  436. }
  437. // Add trigger to the Interconnect Cache
  438. // During each processing loop, a scancode may be re-added depending on it's state
  439. for ( var_uint_t c = 0; c < macroInterconnectCacheSize; c++ )
  440. {
  441. // Check if the same ScanCode
  442. if ( macroInterconnectCache[ c ].scanCode == trigger->scanCode )
  443. {
  444. // Update the state
  445. macroInterconnectCache[ c ].state = trigger->state;
  446. return;
  447. }
  448. }
  449. // If not in the list, add it
  450. macroInterconnectCache[ macroInterconnectCacheSize++ ] = *trigger;
  451. }
  452. #endif
  453. // Update the scancode key state
  454. // States:
  455. // * 0x00 - Off
  456. // * 0x01 - Pressed
  457. // * 0x02 - Held
  458. // * 0x03 - Released
  459. // * 0x04 - Unpressed (this is currently ignored)
  460. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  461. {
  462. #if defined(ConnectEnabled_define)
  463. // Only compile in if a Connect node module is available
  464. if ( !Connect_master )
  465. {
  466. // ScanCodes are only added if there was a state change (on/off)
  467. switch ( state )
  468. {
  469. case 0x00: // Off
  470. case 0x02: // Held
  471. return;
  472. }
  473. }
  474. #endif
  475. // Only add to macro trigger list if one of three states
  476. switch ( state )
  477. {
  478. case 0x01: // Pressed
  479. case 0x02: // Held
  480. case 0x03: // Released
  481. // Check if ScanCode is out of range
  482. if ( scanCode > MaxScanCode )
  483. {
  484. warn_msg("ScanCode is out of range/not defined: ");
  485. printHex( scanCode );
  486. print( NL );
  487. return;
  488. }
  489. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  490. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  491. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
  492. macroTriggerListBufferSize++;
  493. break;
  494. }
  495. }
  496. // Update the scancode analog state
  497. // States:
  498. // * 0x00 - Off
  499. // * 0x01 - Released
  500. // * 0x02-0xFF - Analog value (low to high)
  501. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  502. {
  503. // Only add to macro trigger list if non-off
  504. // TODO Handle change for interconnect
  505. if ( state != 0x00 )
  506. {
  507. // Check if ScanCode is out of range
  508. if ( scanCode > MaxScanCode )
  509. {
  510. warn_msg("ScanCode is out of range/not defined: ");
  511. printHex( scanCode );
  512. print( NL );
  513. return;
  514. }
  515. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  516. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  517. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
  518. macroTriggerListBufferSize++;
  519. }
  520. }
  521. // Update led state
  522. // States:
  523. // * 0x00 - Off
  524. // * 0x01 - On
  525. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  526. {
  527. // Only add to macro trigger list if non-off
  528. // TODO Handle change for interconnect
  529. if ( state != 0x00 )
  530. {
  531. // Check if LedCode is out of range
  532. // TODO
  533. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
  534. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  535. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
  536. macroTriggerListBufferSize++;
  537. }
  538. }
  539. // Append result macro to pending list, checking for duplicates
  540. // Do nothing if duplicate
  541. void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMacro )
  542. {
  543. // Lookup result macro index
  544. var_uint_t resultMacroIndex = triggerMacro->result;
  545. // Iterate through result macro pending list, making sure this macro hasn't been added yet
  546. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  547. {
  548. // If duplicate found, do nothing
  549. if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
  550. return;
  551. }
  552. // No duplicates found, add to pending list
  553. macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
  554. // Lookup scanCode of the last key in the last combo
  555. var_uint_t pos = 0;
  556. for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
  557. {
  558. pos += TriggerGuideSize * comboLength + 1;
  559. comboLength = triggerMacro->guide[ pos ];
  560. }
  561. uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
  562. // Lookup scanCode in buffer list for the current state and stateType
  563. for ( var_uint_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
  564. {
  565. if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
  566. {
  567. ResultMacroRecordList[ resultMacroIndex ].state = macroTriggerListBuffer[ keyIndex ].state;
  568. ResultMacroRecordList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
  569. }
  570. }
  571. // Reset the macro position
  572. ResultMacroRecordList[ resultMacroIndex ].pos = 0;
  573. }
  574. // Macro Procesing Loop
  575. // Called once per USB buffer send
  576. inline void Macro_process()
  577. {
  578. #if defined(ConnectEnabled_define)
  579. // Only compile in if a Connect node module is available
  580. // If this is a interconnect slave node, send all scancodes to master node
  581. if ( !Connect_master )
  582. {
  583. if ( macroTriggerListBufferSize > 0 )
  584. {
  585. Connect_send_ScanCode( Connect_id, macroTriggerListBuffer, macroTriggerListBufferSize );
  586. macroTriggerListBufferSize = 0;
  587. }
  588. return;
  589. }
  590. #endif
  591. // Only do one round of macro processing between Output Module timer sends
  592. if ( USBKeys_Sent != 0 )
  593. return;
  594. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  595. #if defined(ConnectEnabled_define)
  596. // Check if there are any ScanCodes in the interconnect cache to process
  597. if ( Connect_master && macroInterconnectCacheSize > 0 )
  598. #endif
  599. {
  600. // Iterate over all the cache ScanCodes
  601. uint8_t currentInterconnectCacheSize = macroInterconnectCacheSize;
  602. macroInterconnectCacheSize = 0;
  603. for ( uint8_t c = 0; c < currentInterconnectCacheSize; c++ )
  604. {
  605. // Add to the trigger list
  606. macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = macroInterconnectCache[ c ];
  607. // TODO Handle other TriggerGuide types (e.g. analog)
  608. switch ( macroInterconnectCache[ c ].type )
  609. {
  610. // Normal (Press/Hold/Release)
  611. case 0x00:
  612. // Decide what to do based on the current state
  613. switch ( macroInterconnectCache[ c ].state )
  614. {
  615. // Re-add to interconnect cache in hold state
  616. case 0x01: // Press
  617. //case 0x02: // Hold // XXX Why does this not work? -HaaTa
  618. macroInterconnectCache[ c ].state = 0x02;
  619. macroInterconnectCache[ macroInterconnectCacheSize++ ] = macroInterconnectCache[ c ];
  620. break;
  621. case 0x03: // Remove
  622. break;
  623. // Otherwise, do not re-add
  624. }
  625. }
  626. }
  627. }
  628. #endif
  629. // If the pause flag is set, only process if the step counter is non-zero
  630. if ( macroPauseMode )
  631. {
  632. if ( macroStepCounter == 0 )
  633. return;
  634. // Proceed, decrementing the step counter
  635. macroStepCounter--;
  636. dbug_print("Macro Step");
  637. }
  638. // Process Trigger Macros
  639. Trigger_process();
  640. // Process result macros
  641. Result_process();
  642. // Signal buffer that we've used it
  643. Scan_finishedWithMacro( macroTriggerListBufferSize );
  644. // Reset TriggerList buffer
  645. macroTriggerListBufferSize = 0;
  646. // If Macro debug mode is set, clear the USB Buffer
  647. if ( macroDebugMode )
  648. {
  649. USBKeys_Modifiers = 0;
  650. USBKeys_Sent = 0;
  651. }
  652. }
  653. inline void Macro_setup()
  654. {
  655. // Register Macro CLI dictionary
  656. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  657. // Disable Macro debug mode
  658. macroDebugMode = 0;
  659. // Disable Macro pause flag
  660. macroPauseMode = 0;
  661. // Set Macro step counter to zero
  662. macroStepCounter = 0;
  663. // Make sure macro trigger buffer is empty
  664. macroTriggerListBufferSize = 0;
  665. // Set the current rotated layer to 0
  666. Macro_rotationLayer = 0;
  667. // Setup Triggers
  668. Trigger_setup();
  669. // Setup Results
  670. Result_setup();
  671. }
  672. // ----- CLI Command Functions -----
  673. void cliFunc_capList( char* args )
  674. {
  675. print( NL );
  676. info_msg("Capabilities List ");
  677. printHex( CapabilitiesNum );
  678. // Iterate through all of the capabilities and display them
  679. for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
  680. {
  681. print( NL "\t" );
  682. printHex( cap );
  683. print(" - ");
  684. // Display/Lookup Capability Name (utilize debug mode of capability)
  685. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  686. capability( 0xFF, 0xFF, 0 );
  687. }
  688. }
  689. void cliFunc_capSelect( char* args )
  690. {
  691. // Parse code from argument
  692. char* curArgs;
  693. char* arg1Ptr;
  694. char* arg2Ptr = args;
  695. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  696. var_uint_t totalArgs = 2; // Always at least two args
  697. var_uint_t cap = 0;
  698. // Arguments used for keyboard capability function
  699. var_uint_t argSetCount = 0;
  700. uint8_t *argSet = (uint8_t*)args;
  701. // Process all args
  702. for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
  703. {
  704. curArgs = arg2Ptr;
  705. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  706. // Stop processing args if no more are found
  707. // Extra arguments are ignored
  708. if ( *arg1Ptr == '\0' )
  709. break;
  710. // For the first argument, choose the capability
  711. if ( c == 0 ) switch ( arg1Ptr[0] )
  712. {
  713. // Keyboard Capability
  714. case 'K':
  715. // Determine capability index
  716. cap = numToInt( &arg1Ptr[1] );
  717. // Lookup the number of args
  718. totalArgs += CapabilitiesList[ cap ].argCount;
  719. continue;
  720. }
  721. // Because allocating memory isn't doable, and the argument count is arbitrary
  722. // The argument pointer is repurposed as the argument list (much smaller anyways)
  723. argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
  724. // Once all the arguments are prepared, call the keyboard capability function
  725. if ( argSetCount == totalArgs )
  726. {
  727. // Indicate that the capability was called
  728. print( NL );
  729. info_msg("K");
  730. printInt8( cap );
  731. print(" - ");
  732. printHex( argSet[0] );
  733. print(" - ");
  734. printHex( argSet[1] );
  735. print(" - ");
  736. printHex( argSet[2] );
  737. print( "..." NL );
  738. // Make sure this isn't the reload capability
  739. // If it is, and the remote reflash define is not set, ignore
  740. if ( flashModeEnabled_define == 0 ) for ( uint32_t cap = 0; cap < CapabilitiesNum; cap++ )
  741. {
  742. if ( CapabilitiesList[ cap ].func == (const void*)Output_flashMode_capability )
  743. {
  744. print( NL );
  745. warn_print("flashModeEnabled not set, cancelling firmware reload...");
  746. info_msg("Set flashModeEnabled to 1 in your kll configuration.");
  747. return;
  748. }
  749. }
  750. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  751. capability( argSet[0], argSet[1], &argSet[2] );
  752. }
  753. }
  754. }
  755. void cliFunc_keyHold( char* args )
  756. {
  757. // Parse codes from arguments
  758. char* curArgs;
  759. char* arg1Ptr;
  760. char* arg2Ptr = args;
  761. // Process all args
  762. for ( ;; )
  763. {
  764. curArgs = arg2Ptr;
  765. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  766. // Stop processing args if no more are found
  767. if ( *arg1Ptr == '\0' )
  768. break;
  769. // Ignore non-Scancode numbers
  770. switch ( arg1Ptr[0] )
  771. {
  772. // Scancode
  773. case 'S':
  774. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
  775. break;
  776. }
  777. }
  778. }
  779. void cliFunc_keyPress( char* args )
  780. {
  781. // Parse codes from arguments
  782. char* curArgs;
  783. char* arg1Ptr;
  784. char* arg2Ptr = args;
  785. // Process all args
  786. for ( ;; )
  787. {
  788. curArgs = arg2Ptr;
  789. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  790. // Stop processing args if no more are found
  791. if ( *arg1Ptr == '\0' )
  792. break;
  793. // Ignore non-Scancode numbers
  794. switch ( arg1Ptr[0] )
  795. {
  796. // Scancode
  797. case 'S':
  798. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  799. break;
  800. }
  801. }
  802. }
  803. void cliFunc_keyRelease( char* args )
  804. {
  805. // Parse codes from arguments
  806. char* curArgs;
  807. char* arg1Ptr;
  808. char* arg2Ptr = args;
  809. // Process all args
  810. for ( ;; )
  811. {
  812. curArgs = arg2Ptr;
  813. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  814. // Stop processing args if no more are found
  815. if ( *arg1Ptr == '\0' )
  816. break;
  817. // Ignore non-Scancode numbers
  818. switch ( arg1Ptr[0] )
  819. {
  820. // Scancode
  821. case 'S':
  822. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  823. break;
  824. }
  825. }
  826. }
  827. void cliFunc_layerDebug( char *args )
  828. {
  829. // Toggle layer debug mode
  830. layerDebugMode = layerDebugMode ? 0 : 1;
  831. print( NL );
  832. info_msg("Layer Debug Mode: ");
  833. printInt8( layerDebugMode );
  834. }
  835. void cliFunc_layerList( char* args )
  836. {
  837. print( NL );
  838. info_msg("Layer List");
  839. // Iterate through all of the layers and display them
  840. for ( uint16_t layer = 0; layer < LayerNum; layer++ )
  841. {
  842. print( NL "\t" );
  843. printHex( layer );
  844. print(" - ");
  845. // Display layer name
  846. dPrint( (char*)LayerIndex[ layer ].name );
  847. // Default map
  848. if ( layer == 0 )
  849. print(" \033[1m(default)\033[0m");
  850. // Layer State
  851. print( NL "\t\t Layer State: " );
  852. printHex( LayerState[ layer ] );
  853. // First -> Last Indices
  854. print(" First -> Last Indices: ");
  855. printHex( LayerIndex[ layer ].first );
  856. print(" -> ");
  857. printHex( LayerIndex[ layer ].last );
  858. }
  859. }
  860. void cliFunc_layerState( char* args )
  861. {
  862. // Parse codes from arguments
  863. char* curArgs;
  864. char* arg1Ptr;
  865. char* arg2Ptr = args;
  866. uint8_t arg1 = 0;
  867. uint8_t arg2 = 0;
  868. // Process first two args
  869. for ( uint8_t c = 0; c < 2; c++ )
  870. {
  871. curArgs = arg2Ptr;
  872. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  873. // Stop processing args if no more are found
  874. if ( *arg1Ptr == '\0' )
  875. break;
  876. switch ( c )
  877. {
  878. // First argument (e.g. L1)
  879. case 0:
  880. if ( arg1Ptr[0] != 'L' )
  881. return;
  882. arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
  883. break;
  884. // Second argument (e.g. 4)
  885. case 1:
  886. arg2 = (uint8_t)numToInt( arg1Ptr );
  887. // Display operation (to indicate that it worked)
  888. print( NL );
  889. info_msg("Setting Layer L");
  890. printInt8( arg1 );
  891. print(" to - ");
  892. printHex( arg2 );
  893. // Set the layer state
  894. LayerState[ arg1 ] = arg2;
  895. break;
  896. }
  897. }
  898. }
  899. void cliFunc_macroDebug( char* args )
  900. {
  901. // Toggle macro debug mode
  902. macroDebugMode = macroDebugMode ? 0 : 1;
  903. print( NL );
  904. info_msg("Macro Debug Mode: ");
  905. printInt8( macroDebugMode );
  906. }
  907. void cliFunc_macroList( char* args )
  908. {
  909. // Show pending key events
  910. print( NL );
  911. info_msg("Pending Key Events: ");
  912. printInt16( (uint16_t)macroTriggerListBufferSize );
  913. print(" : ");
  914. for ( var_uint_t key = 0; key < macroTriggerListBufferSize; key++ )
  915. {
  916. printHex( macroTriggerListBuffer[ key ].scanCode );
  917. print(" ");
  918. }
  919. // Show pending trigger macros
  920. print( NL );
  921. info_msg("Pending Trigger Macros: ");
  922. printInt16( (uint16_t)macroTriggerMacroPendingListSize );
  923. print(" : ");
  924. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  925. {
  926. printHex( macroTriggerMacroPendingList[ macro ] );
  927. print(" ");
  928. }
  929. // Show pending result macros
  930. print( NL );
  931. info_msg("Pending Result Macros: ");
  932. printInt16( (uint16_t)macroResultMacroPendingListSize );
  933. print(" : ");
  934. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  935. {
  936. printHex( macroResultMacroPendingList[ macro ] );
  937. print(" ");
  938. }
  939. // Show available trigger macro indices
  940. print( NL );
  941. info_msg("Trigger Macros Range: T0 -> T");
  942. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  943. // Show available result macro indices
  944. print( NL );
  945. info_msg("Result Macros Range: R0 -> R");
  946. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  947. // Show Trigger to Result Macro Links
  948. print( NL );
  949. info_msg("Trigger : Result Macro Pairs");
  950. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  951. {
  952. print( NL );
  953. print("\tT");
  954. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  955. print(" : R");
  956. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  957. }
  958. }
  959. void cliFunc_macroProc( char* args )
  960. {
  961. // Toggle macro pause mode
  962. macroPauseMode = macroPauseMode ? 0 : 1;
  963. print( NL );
  964. info_msg("Macro Processing Mode: ");
  965. printInt8( macroPauseMode );
  966. }
  967. void macroDebugShowTrigger( var_uint_t index )
  968. {
  969. // Only proceed if the macro exists
  970. if ( index >= TriggerMacroNum )
  971. return;
  972. // Trigger Macro Show
  973. const TriggerMacro *macro = &TriggerMacroList[ index ];
  974. TriggerMacroRecord *record = &TriggerMacroRecordList[ index ];
  975. print( NL );
  976. info_msg("Trigger Macro Index: ");
  977. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  978. print( NL );
  979. // Read the comboLength for combo in the sequence (sequence of combos)
  980. var_uint_t pos = 0;
  981. uint8_t comboLength = macro->guide[ pos ];
  982. // Iterate through and interpret the guide
  983. while ( comboLength != 0 )
  984. {
  985. // Initial position of the combo
  986. var_uint_t comboPos = ++pos;
  987. // Iterate through the combo
  988. while ( pos < comboLength * TriggerGuideSize + comboPos )
  989. {
  990. // Assign TriggerGuide element (key type, state and scancode)
  991. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  992. // Display guide information about trigger key
  993. printHex( guide->scanCode );
  994. print("|");
  995. printHex( guide->type );
  996. print("|");
  997. printHex( guide->state );
  998. // Increment position
  999. pos += TriggerGuideSize;
  1000. // Only show combo separator if there are combos left in the sequence element
  1001. if ( pos < comboLength * TriggerGuideSize + comboPos )
  1002. print("+");
  1003. }
  1004. // Read the next comboLength
  1005. comboLength = macro->guide[ pos ];
  1006. // Only show sequence separator if there is another combo to process
  1007. if ( comboLength != 0 )
  1008. print(";");
  1009. }
  1010. // Display current position
  1011. print( NL "Position: " );
  1012. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1013. // Display result macro index
  1014. print( NL "Result Macro Index: " );
  1015. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  1016. // Display trigger macro state
  1017. print( NL "Trigger Macro State: " );
  1018. switch ( record->state )
  1019. {
  1020. case TriggerMacro_Press: print("Press"); break;
  1021. case TriggerMacro_Release: print("Release"); break;
  1022. case TriggerMacro_Waiting: print("Waiting"); break;
  1023. }
  1024. }
  1025. void macroDebugShowResult( var_uint_t index )
  1026. {
  1027. // Only proceed if the macro exists
  1028. if ( index >= ResultMacroNum )
  1029. return;
  1030. // Trigger Macro Show
  1031. const ResultMacro *macro = &ResultMacroList[ index ];
  1032. ResultMacroRecord *record = &ResultMacroRecordList[ index ];
  1033. print( NL );
  1034. info_msg("Result Macro Index: ");
  1035. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1036. print( NL );
  1037. // Read the comboLength for combo in the sequence (sequence of combos)
  1038. var_uint_t pos = 0;
  1039. uint8_t comboLength = macro->guide[ pos++ ];
  1040. // Iterate through and interpret the guide
  1041. while ( comboLength != 0 )
  1042. {
  1043. // Function Counter, used to keep track of the combos processed
  1044. var_uint_t funcCount = 0;
  1045. // Iterate through the combo
  1046. while ( funcCount < comboLength )
  1047. {
  1048. // Assign TriggerGuide element (key type, state and scancode)
  1049. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  1050. // Display Function Index
  1051. printHex( guide->index );
  1052. print("|");
  1053. // Display Function Ptr Address
  1054. printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
  1055. print("|");
  1056. // Display/Lookup Capability Name (utilize debug mode of capability)
  1057. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  1058. capability( 0xFF, 0xFF, 0 );
  1059. // Display Argument(s)
  1060. print("(");
  1061. for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  1062. {
  1063. // Arguments are only 8 bit values
  1064. printHex( (&guide->args)[ arg ] );
  1065. // Only show arg separator if there are args left
  1066. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  1067. print(",");
  1068. }
  1069. print(")");
  1070. // Increment position
  1071. pos += ResultGuideSize( guide );
  1072. // Increment function count
  1073. funcCount++;
  1074. // Only show combo separator if there are combos left in the sequence element
  1075. if ( funcCount < comboLength )
  1076. print("+");
  1077. }
  1078. // Read the next comboLength
  1079. comboLength = macro->guide[ pos++ ];
  1080. // Only show sequence separator if there is another combo to process
  1081. if ( comboLength != 0 )
  1082. print(";");
  1083. }
  1084. // Display current position
  1085. print( NL "Position: " );
  1086. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1087. // Display final trigger state/type
  1088. print( NL "Final Trigger State (State/Type): " );
  1089. printHex( record->state );
  1090. print("/");
  1091. printHex( record->stateType );
  1092. }
  1093. void cliFunc_macroShow( char* args )
  1094. {
  1095. // Parse codes from arguments
  1096. char* curArgs;
  1097. char* arg1Ptr;
  1098. char* arg2Ptr = args;
  1099. // Process all args
  1100. for ( ;; )
  1101. {
  1102. curArgs = arg2Ptr;
  1103. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1104. // Stop processing args if no more are found
  1105. if ( *arg1Ptr == '\0' )
  1106. break;
  1107. // Ignore invalid codes
  1108. switch ( arg1Ptr[0] )
  1109. {
  1110. // Indexed Trigger Macro
  1111. case 'T':
  1112. macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
  1113. break;
  1114. // Indexed Result Macro
  1115. case 'R':
  1116. macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
  1117. break;
  1118. }
  1119. }
  1120. }
  1121. void cliFunc_macroStep( char* args )
  1122. {
  1123. // Parse number from argument
  1124. // NOTE: Only first argument is used
  1125. char* arg1Ptr;
  1126. char* arg2Ptr;
  1127. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  1128. // Default to 1, if no argument given
  1129. var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
  1130. if ( count == 0 )
  1131. count = 1;
  1132. // Set the macro step counter, negative int's are cast to uint
  1133. macroStepCounter = count;
  1134. }