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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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. Macro_layerState( state, stateType, layer, 0x01 );
  258. }
  259. // Rotate layer to next/previous
  260. // Uses state variable to keep track of the current layer position
  261. // Layers are still evaluated using the layer stack
  262. uint16_t Macro_rotationLayer;
  263. void Macro_layerRotate_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  264. {
  265. // Display capability name
  266. if ( stateType == 0xFF && state == 0xFF )
  267. {
  268. print("Macro_layerRotate(previous)");
  269. return;
  270. }
  271. // Only use capability on press
  272. // TODO Analog
  273. // XXX Could also be on release, but that's sorta dumb -HaaTa
  274. if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
  275. return;
  276. // Unset previous rotation layer if not 0
  277. if ( Macro_rotationLayer != 0 )
  278. {
  279. Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
  280. }
  281. // Get direction of rotation, 0, next, non-zero previous
  282. uint8_t direction = *args;
  283. // Next
  284. if ( !direction )
  285. {
  286. Macro_rotationLayer++;
  287. // Invalid layer
  288. if ( Macro_rotationLayer >= LayerNum )
  289. Macro_rotationLayer = 0;
  290. }
  291. // Previous
  292. else
  293. {
  294. Macro_rotationLayer--;
  295. // Layer wrap
  296. if ( Macro_rotationLayer >= LayerNum )
  297. Macro_rotationLayer = LayerNum - 1;
  298. }
  299. // Toggle the computed layer rotation
  300. Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
  301. }
  302. // ----- Functions -----
  303. // Looks up the trigger list for the given scan code (from the active layer)
  304. // NOTE: Calling function must handle the NULL pointer case
  305. nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire )
  306. {
  307. uint8_t scanCode = guide->scanCode;
  308. // TODO Analog
  309. // If a normal key, and not pressed, do a layer cache lookup
  310. if ( guide->type == 0x00 && guide->state != 0x01 )
  311. {
  312. // Cached layer
  313. var_uint_t cachedLayer = macroTriggerListLayerCache[ scanCode ];
  314. // Lookup map, then layer
  315. nat_ptr_t **map = (nat_ptr_t**)LayerIndex[ cachedLayer ].triggerMap;
  316. const Layer *layer = &LayerIndex[ cachedLayer ];
  317. // Cache trigger list before attempting to expire latch
  318. nat_ptr_t *trigger_list = map[ scanCode - layer->first ];
  319. // Check if latch has been pressed for this layer
  320. uint8_t latch = LayerState[ cachedLayer ] & 0x02;
  321. if ( latch && latch_expire )
  322. {
  323. Macro_layerState( 0, 0, cachedLayer, 0x02 );
  324. #if defined(ConnectEnabled_define) && defined(LCDEnabled_define)
  325. // Evaluate the layerStack capability if available (LCD + Interconnect)
  326. extern void LCD_layerStack_capability( uint8_t state, uint8_t stateType, uint8_t *args );
  327. LCD_layerStack_capability( 0, 0, 0 );
  328. #endif
  329. }
  330. return trigger_list;
  331. }
  332. // If no trigger macro is defined at the given layer, fallthrough to the next layer
  333. for ( uint16_t layerIndex = macroLayerIndexStackSize; layerIndex != 0xFFFF; layerIndex-- )
  334. {
  335. // Lookup Layer
  336. const Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
  337. // Check if latch has been pressed for this layer
  338. // XXX Regardless of whether a key is found, the latch is removed on first lookup
  339. uint8_t latch = LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x02;
  340. if ( latch && latch_expire )
  341. {
  342. Macro_layerState( 0, 0, macroLayerIndexStack[ layerIndex ], 0x02 );
  343. }
  344. // Only use layer, if state is valid
  345. // XOR each of the state bits
  346. // If only two are enabled, do not use this state
  347. if ( (LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x01) ^ (latch>>1) ^ ((LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x04)>>2) )
  348. {
  349. // Lookup layer
  350. nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
  351. // Determine if layer has key defined
  352. // Make sure scanCode is between layer first and last scancodes
  353. if ( map != 0
  354. && scanCode <= layer->last
  355. && scanCode >= layer->first
  356. && *map[ scanCode - layer->first ] != 0 )
  357. {
  358. // Set the layer cache
  359. macroTriggerListLayerCache[ scanCode ] = macroLayerIndexStack[ layerIndex ];
  360. return map[ scanCode - layer->first ];
  361. }
  362. }
  363. }
  364. // Do lookup on default layer
  365. nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
  366. // Lookup default layer
  367. const Layer *layer = &LayerIndex[0];
  368. // Make sure scanCode is between layer first and last scancodes
  369. if ( map != 0
  370. && scanCode <= layer->last
  371. && scanCode >= layer->first
  372. && *map[ scanCode - layer->first ] != 0 )
  373. {
  374. // Set the layer cache to default map
  375. macroTriggerListLayerCache[ scanCode ] = 0;
  376. return map[ scanCode - layer->first ];
  377. }
  378. // Otherwise no defined Trigger Macro
  379. erro_msg("Scan Code has no defined Trigger Macro: ");
  380. printHex( scanCode );
  381. print( NL );
  382. return 0;
  383. }
  384. // Add an interconnect ScanCode
  385. // These are handled differently (less information is sent, hold/off states must be assumed)
  386. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  387. void Macro_pressReleaseAdd( void *trigger_ptr )
  388. {
  389. TriggerGuide *trigger = (TriggerGuide*)trigger_ptr;
  390. // Error checking
  391. uint8_t error = 0;
  392. switch ( trigger->type )
  393. {
  394. case 0x00: // Normal key
  395. switch ( trigger->state )
  396. {
  397. case 0x00:
  398. case 0x01:
  399. case 0x02:
  400. case 0x03:
  401. break;
  402. default:
  403. erro_msg("Invalid key state - ");
  404. error = 1;
  405. break;
  406. }
  407. break;
  408. // Invalid TriggerGuide type
  409. default:
  410. erro_msg("Invalid type - ");
  411. error = 1;
  412. break;
  413. }
  414. // Check if ScanCode is out of range
  415. if ( trigger->scanCode > MaxScanCode )
  416. {
  417. warn_msg("ScanCode is out of range/not defined - ");
  418. error = 1;
  419. }
  420. // Display TriggerGuide
  421. if ( error )
  422. {
  423. printHex( trigger->type );
  424. print(" ");
  425. printHex( trigger->state );
  426. print(" ");
  427. printHex( trigger->scanCode );
  428. print( NL );
  429. return;
  430. }
  431. // Add trigger to the Interconnect Cache
  432. // During each processing loop, a scancode may be re-added depending on it's state
  433. for ( var_uint_t c = 0; c < macroInterconnectCacheSize; c++ )
  434. {
  435. // Check if the same ScanCode
  436. if ( macroInterconnectCache[ c ].scanCode == trigger->scanCode )
  437. {
  438. // Update the state
  439. macroInterconnectCache[ c ].state = trigger->state;
  440. return;
  441. }
  442. }
  443. // If not in the list, add it
  444. macroInterconnectCache[ macroInterconnectCacheSize++ ] = *trigger;
  445. }
  446. #endif
  447. // Update the scancode key state
  448. // States:
  449. // * 0x00 - Off
  450. // * 0x01 - Pressed
  451. // * 0x02 - Held
  452. // * 0x03 - Released
  453. // * 0x04 - Unpressed (this is currently ignored)
  454. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  455. {
  456. #if defined(ConnectEnabled_define)
  457. // Only compile in if a Connect node module is available
  458. if ( !Connect_master )
  459. {
  460. // ScanCodes are only added if there was a state change (on/off)
  461. switch ( state )
  462. {
  463. case 0x00: // Off
  464. case 0x02: // Held
  465. return;
  466. }
  467. }
  468. #endif
  469. // Only add to macro trigger list if one of three states
  470. switch ( state )
  471. {
  472. case 0x01: // Pressed
  473. case 0x02: // Held
  474. case 0x03: // Released
  475. // Check if ScanCode is out of range
  476. if ( scanCode > MaxScanCode )
  477. {
  478. warn_msg("ScanCode is out of range/not defined: ");
  479. printHex( scanCode );
  480. print( NL );
  481. return;
  482. }
  483. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  484. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  485. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
  486. macroTriggerListBufferSize++;
  487. break;
  488. }
  489. }
  490. // Update the scancode analog state
  491. // States:
  492. // * 0x00 - Off
  493. // * 0x01 - Released
  494. // * 0x02-0xFF - Analog value (low to high)
  495. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  496. {
  497. // Only add to macro trigger list if non-off
  498. // TODO Handle change for interconnect
  499. if ( state != 0x00 )
  500. {
  501. // Check if ScanCode is out of range
  502. if ( scanCode > MaxScanCode )
  503. {
  504. warn_msg("ScanCode is out of range/not defined: ");
  505. printHex( scanCode );
  506. print( NL );
  507. return;
  508. }
  509. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  510. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  511. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
  512. macroTriggerListBufferSize++;
  513. }
  514. }
  515. // Update led state
  516. // States:
  517. // * 0x00 - Off
  518. // * 0x01 - On
  519. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  520. {
  521. // Only add to macro trigger list if non-off
  522. // TODO Handle change for interconnect
  523. if ( state != 0x00 )
  524. {
  525. // Check if LedCode is out of range
  526. // TODO
  527. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
  528. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  529. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
  530. macroTriggerListBufferSize++;
  531. }
  532. }
  533. // Append result macro to pending list, checking for duplicates
  534. // Do nothing if duplicate
  535. void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMacro )
  536. {
  537. // Lookup result macro index
  538. var_uint_t resultMacroIndex = triggerMacro->result;
  539. // Iterate through result macro pending list, making sure this macro hasn't been added yet
  540. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  541. {
  542. // If duplicate found, do nothing
  543. if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
  544. return;
  545. }
  546. // No duplicates found, add to pending list
  547. macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
  548. // Lookup scanCode of the last key in the last combo
  549. var_uint_t pos = 0;
  550. for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
  551. {
  552. pos += TriggerGuideSize * comboLength + 1;
  553. comboLength = triggerMacro->guide[ pos ];
  554. }
  555. uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
  556. // Lookup scanCode in buffer list for the current state and stateType
  557. for ( var_uint_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
  558. {
  559. if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
  560. {
  561. ResultMacroRecordList[ resultMacroIndex ].state = macroTriggerListBuffer[ keyIndex ].state;
  562. ResultMacroRecordList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
  563. }
  564. }
  565. // Reset the macro position
  566. ResultMacroRecordList[ resultMacroIndex ].pos = 0;
  567. }
  568. // Macro Procesing Loop
  569. // Called once per USB buffer send
  570. inline void Macro_process()
  571. {
  572. #if defined(ConnectEnabled_define)
  573. // Only compile in if a Connect node module is available
  574. // If this is a interconnect slave node, send all scancodes to master node
  575. if ( !Connect_master )
  576. {
  577. if ( macroTriggerListBufferSize > 0 )
  578. {
  579. Connect_send_ScanCode( Connect_id, macroTriggerListBuffer, macroTriggerListBufferSize );
  580. macroTriggerListBufferSize = 0;
  581. }
  582. return;
  583. }
  584. #endif
  585. // Only do one round of macro processing between Output Module timer sends
  586. if ( USBKeys_Sent != 0 )
  587. return;
  588. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  589. #if defined(ConnectEnabled_define)
  590. // Check if there are any ScanCodes in the interconnect cache to process
  591. if ( Connect_master && macroInterconnectCacheSize > 0 )
  592. #endif
  593. {
  594. // Iterate over all the cache ScanCodes
  595. uint8_t currentInterconnectCacheSize = macroInterconnectCacheSize;
  596. macroInterconnectCacheSize = 0;
  597. for ( uint8_t c = 0; c < currentInterconnectCacheSize; c++ )
  598. {
  599. // Add to the trigger list
  600. macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = macroInterconnectCache[ c ];
  601. // TODO Handle other TriggerGuide types (e.g. analog)
  602. switch ( macroInterconnectCache[ c ].type )
  603. {
  604. // Normal (Press/Hold/Release)
  605. case 0x00:
  606. // Decide what to do based on the current state
  607. switch ( macroInterconnectCache[ c ].state )
  608. {
  609. // Re-add to interconnect cache in hold state
  610. case 0x01: // Press
  611. //case 0x02: // Hold // XXX Why does this not work? -HaaTa
  612. macroInterconnectCache[ c ].state = 0x02;
  613. macroInterconnectCache[ macroInterconnectCacheSize++ ] = macroInterconnectCache[ c ];
  614. break;
  615. case 0x03: // Remove
  616. break;
  617. // Otherwise, do not re-add
  618. }
  619. }
  620. }
  621. }
  622. #endif
  623. // If the pause flag is set, only process if the step counter is non-zero
  624. if ( macroPauseMode )
  625. {
  626. if ( macroStepCounter == 0 )
  627. return;
  628. // Proceed, decrementing the step counter
  629. macroStepCounter--;
  630. dbug_print("Macro Step");
  631. }
  632. // Process Trigger Macros
  633. Trigger_process();
  634. // Process result macros
  635. Result_process();
  636. // Signal buffer that we've used it
  637. Scan_finishedWithMacro( macroTriggerListBufferSize );
  638. // Reset TriggerList buffer
  639. macroTriggerListBufferSize = 0;
  640. // If Macro debug mode is set, clear the USB Buffer
  641. if ( macroDebugMode )
  642. {
  643. USBKeys_Modifiers = 0;
  644. USBKeys_Sent = 0;
  645. }
  646. }
  647. inline void Macro_setup()
  648. {
  649. // Register Macro CLI dictionary
  650. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  651. // Disable Macro debug mode
  652. macroDebugMode = 0;
  653. // Disable Macro pause flag
  654. macroPauseMode = 0;
  655. // Set Macro step counter to zero
  656. macroStepCounter = 0;
  657. // Make sure macro trigger buffer is empty
  658. macroTriggerListBufferSize = 0;
  659. // Set the current rotated layer to 0
  660. Macro_rotationLayer = 0;
  661. // Setup Triggers
  662. Trigger_setup();
  663. // Setup Results
  664. Result_setup();
  665. }
  666. // ----- CLI Command Functions -----
  667. void cliFunc_capList( char* args )
  668. {
  669. print( NL );
  670. info_msg("Capabilities List ");
  671. printHex( CapabilitiesNum );
  672. // Iterate through all of the capabilities and display them
  673. for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
  674. {
  675. print( NL "\t" );
  676. printHex( cap );
  677. print(" - ");
  678. // Display/Lookup Capability Name (utilize debug mode of capability)
  679. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  680. capability( 0xFF, 0xFF, 0 );
  681. }
  682. }
  683. void cliFunc_capSelect( char* args )
  684. {
  685. // Parse code from argument
  686. char* curArgs;
  687. char* arg1Ptr;
  688. char* arg2Ptr = args;
  689. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  690. var_uint_t totalArgs = 2; // Always at least two args
  691. var_uint_t cap = 0;
  692. // Arguments used for keyboard capability function
  693. var_uint_t argSetCount = 0;
  694. uint8_t *argSet = (uint8_t*)args;
  695. // Process all args
  696. for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
  697. {
  698. curArgs = arg2Ptr;
  699. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  700. // Stop processing args if no more are found
  701. // Extra arguments are ignored
  702. if ( *arg1Ptr == '\0' )
  703. break;
  704. // For the first argument, choose the capability
  705. if ( c == 0 ) switch ( arg1Ptr[0] )
  706. {
  707. // Keyboard Capability
  708. case 'K':
  709. // Determine capability index
  710. cap = numToInt( &arg1Ptr[1] );
  711. // Lookup the number of args
  712. totalArgs += CapabilitiesList[ cap ].argCount;
  713. continue;
  714. }
  715. // Because allocating memory isn't doable, and the argument count is arbitrary
  716. // The argument pointer is repurposed as the argument list (much smaller anyways)
  717. argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
  718. // Once all the arguments are prepared, call the keyboard capability function
  719. if ( argSetCount == totalArgs )
  720. {
  721. // Indicate that the capability was called
  722. print( NL );
  723. info_msg("K");
  724. printInt8( cap );
  725. print(" - ");
  726. printHex( argSet[0] );
  727. print(" - ");
  728. printHex( argSet[1] );
  729. print(" - ");
  730. printHex( argSet[2] );
  731. print( "..." NL );
  732. // Make sure this isn't the reload capability
  733. // If it is, and the remote reflash define is not set, ignore
  734. if ( flashModeEnabled_define == 0 ) for ( uint32_t cap = 0; cap < CapabilitiesNum; cap++ )
  735. {
  736. if ( CapabilitiesList[ cap ].func == (const void*)Output_flashMode_capability )
  737. {
  738. print( NL );
  739. warn_print("flashModeEnabled not set, cancelling firmware reload...");
  740. info_msg("Set flashModeEnabled to 1 in your kll configuration.");
  741. return;
  742. }
  743. }
  744. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  745. capability( argSet[0], argSet[1], &argSet[2] );
  746. }
  747. }
  748. }
  749. void cliFunc_keyHold( char* args )
  750. {
  751. // Parse codes from arguments
  752. char* curArgs;
  753. char* arg1Ptr;
  754. char* arg2Ptr = args;
  755. // Process all args
  756. for ( ;; )
  757. {
  758. curArgs = arg2Ptr;
  759. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  760. // Stop processing args if no more are found
  761. if ( *arg1Ptr == '\0' )
  762. break;
  763. // Ignore non-Scancode numbers
  764. switch ( arg1Ptr[0] )
  765. {
  766. // Scancode
  767. case 'S':
  768. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
  769. break;
  770. }
  771. }
  772. }
  773. void cliFunc_keyPress( char* args )
  774. {
  775. // Parse codes from arguments
  776. char* curArgs;
  777. char* arg1Ptr;
  778. char* arg2Ptr = args;
  779. // Process all args
  780. for ( ;; )
  781. {
  782. curArgs = arg2Ptr;
  783. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  784. // Stop processing args if no more are found
  785. if ( *arg1Ptr == '\0' )
  786. break;
  787. // Ignore non-Scancode numbers
  788. switch ( arg1Ptr[0] )
  789. {
  790. // Scancode
  791. case 'S':
  792. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  793. break;
  794. }
  795. }
  796. }
  797. void cliFunc_keyRelease( char* args )
  798. {
  799. // Parse codes from arguments
  800. char* curArgs;
  801. char* arg1Ptr;
  802. char* arg2Ptr = args;
  803. // Process all args
  804. for ( ;; )
  805. {
  806. curArgs = arg2Ptr;
  807. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  808. // Stop processing args if no more are found
  809. if ( *arg1Ptr == '\0' )
  810. break;
  811. // Ignore non-Scancode numbers
  812. switch ( arg1Ptr[0] )
  813. {
  814. // Scancode
  815. case 'S':
  816. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  817. break;
  818. }
  819. }
  820. }
  821. void cliFunc_layerDebug( char *args )
  822. {
  823. // Toggle layer debug mode
  824. layerDebugMode = layerDebugMode ? 0 : 1;
  825. print( NL );
  826. info_msg("Layer Debug Mode: ");
  827. printInt8( layerDebugMode );
  828. }
  829. void cliFunc_layerList( char* args )
  830. {
  831. print( NL );
  832. info_msg("Layer List");
  833. // Iterate through all of the layers and display them
  834. for ( uint16_t layer = 0; layer < LayerNum; layer++ )
  835. {
  836. print( NL "\t" );
  837. printHex( layer );
  838. print(" - ");
  839. // Display layer name
  840. dPrint( (char*)LayerIndex[ layer ].name );
  841. // Default map
  842. if ( layer == 0 )
  843. print(" \033[1m(default)\033[0m");
  844. // Layer State
  845. print( NL "\t\t Layer State: " );
  846. printHex( LayerState[ layer ] );
  847. // First -> Last Indices
  848. print(" First -> Last Indices: ");
  849. printHex( LayerIndex[ layer ].first );
  850. print(" -> ");
  851. printHex( LayerIndex[ layer ].last );
  852. }
  853. }
  854. void cliFunc_layerState( char* args )
  855. {
  856. // Parse codes from arguments
  857. char* curArgs;
  858. char* arg1Ptr;
  859. char* arg2Ptr = args;
  860. uint8_t arg1 = 0;
  861. uint8_t arg2 = 0;
  862. // Process first two args
  863. for ( uint8_t c = 0; c < 2; c++ )
  864. {
  865. curArgs = arg2Ptr;
  866. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  867. // Stop processing args if no more are found
  868. if ( *arg1Ptr == '\0' )
  869. break;
  870. switch ( c )
  871. {
  872. // First argument (e.g. L1)
  873. case 0:
  874. if ( arg1Ptr[0] != 'L' )
  875. return;
  876. arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
  877. break;
  878. // Second argument (e.g. 4)
  879. case 1:
  880. arg2 = (uint8_t)numToInt( arg1Ptr );
  881. // Display operation (to indicate that it worked)
  882. print( NL );
  883. info_msg("Setting Layer L");
  884. printInt8( arg1 );
  885. print(" to - ");
  886. printHex( arg2 );
  887. // Set the layer state
  888. LayerState[ arg1 ] = arg2;
  889. break;
  890. }
  891. }
  892. }
  893. void cliFunc_macroDebug( char* args )
  894. {
  895. // Toggle macro debug mode
  896. macroDebugMode = macroDebugMode ? 0 : 1;
  897. print( NL );
  898. info_msg("Macro Debug Mode: ");
  899. printInt8( macroDebugMode );
  900. }
  901. void cliFunc_macroList( char* args )
  902. {
  903. // Show pending key events
  904. print( NL );
  905. info_msg("Pending Key Events: ");
  906. printInt16( (uint16_t)macroTriggerListBufferSize );
  907. print(" : ");
  908. for ( var_uint_t key = 0; key < macroTriggerListBufferSize; key++ )
  909. {
  910. printHex( macroTriggerListBuffer[ key ].scanCode );
  911. print(" ");
  912. }
  913. // Show pending trigger macros
  914. print( NL );
  915. info_msg("Pending Trigger Macros: ");
  916. printInt16( (uint16_t)macroTriggerMacroPendingListSize );
  917. print(" : ");
  918. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  919. {
  920. printHex( macroTriggerMacroPendingList[ macro ] );
  921. print(" ");
  922. }
  923. // Show pending result macros
  924. print( NL );
  925. info_msg("Pending Result Macros: ");
  926. printInt16( (uint16_t)macroResultMacroPendingListSize );
  927. print(" : ");
  928. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  929. {
  930. printHex( macroResultMacroPendingList[ macro ] );
  931. print(" ");
  932. }
  933. // Show available trigger macro indices
  934. print( NL );
  935. info_msg("Trigger Macros Range: T0 -> T");
  936. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  937. // Show available result macro indices
  938. print( NL );
  939. info_msg("Result Macros Range: R0 -> R");
  940. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  941. // Show Trigger to Result Macro Links
  942. print( NL );
  943. info_msg("Trigger : Result Macro Pairs");
  944. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  945. {
  946. print( NL );
  947. print("\tT");
  948. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  949. print(" : R");
  950. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  951. }
  952. }
  953. void cliFunc_macroProc( char* args )
  954. {
  955. // Toggle macro pause mode
  956. macroPauseMode = macroPauseMode ? 0 : 1;
  957. print( NL );
  958. info_msg("Macro Processing Mode: ");
  959. printInt8( macroPauseMode );
  960. }
  961. void macroDebugShowTrigger( var_uint_t index )
  962. {
  963. // Only proceed if the macro exists
  964. if ( index >= TriggerMacroNum )
  965. return;
  966. // Trigger Macro Show
  967. const TriggerMacro *macro = &TriggerMacroList[ index ];
  968. TriggerMacroRecord *record = &TriggerMacroRecordList[ index ];
  969. print( NL );
  970. info_msg("Trigger Macro Index: ");
  971. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  972. print( NL );
  973. // Read the comboLength for combo in the sequence (sequence of combos)
  974. var_uint_t pos = 0;
  975. uint8_t comboLength = macro->guide[ pos ];
  976. // Iterate through and interpret the guide
  977. while ( comboLength != 0 )
  978. {
  979. // Initial position of the combo
  980. var_uint_t comboPos = ++pos;
  981. // Iterate through the combo
  982. while ( pos < comboLength * TriggerGuideSize + comboPos )
  983. {
  984. // Assign TriggerGuide element (key type, state and scancode)
  985. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  986. // Display guide information about trigger key
  987. printHex( guide->scanCode );
  988. print("|");
  989. printHex( guide->type );
  990. print("|");
  991. printHex( guide->state );
  992. // Increment position
  993. pos += TriggerGuideSize;
  994. // Only show combo separator if there are combos left in the sequence element
  995. if ( pos < comboLength * TriggerGuideSize + comboPos )
  996. print("+");
  997. }
  998. // Read the next comboLength
  999. comboLength = macro->guide[ pos ];
  1000. // Only show sequence separator if there is another combo to process
  1001. if ( comboLength != 0 )
  1002. print(";");
  1003. }
  1004. // Display current position
  1005. print( NL "Position: " );
  1006. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1007. // Display result macro index
  1008. print( NL "Result Macro Index: " );
  1009. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  1010. // Display trigger macro state
  1011. print( NL "Trigger Macro State: " );
  1012. switch ( record->state )
  1013. {
  1014. case TriggerMacro_Press: print("Press"); break;
  1015. case TriggerMacro_Release: print("Release"); break;
  1016. case TriggerMacro_Waiting: print("Waiting"); break;
  1017. }
  1018. }
  1019. void macroDebugShowResult( var_uint_t index )
  1020. {
  1021. // Only proceed if the macro exists
  1022. if ( index >= ResultMacroNum )
  1023. return;
  1024. // Trigger Macro Show
  1025. const ResultMacro *macro = &ResultMacroList[ index ];
  1026. ResultMacroRecord *record = &ResultMacroRecordList[ index ];
  1027. print( NL );
  1028. info_msg("Result Macro Index: ");
  1029. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1030. print( NL );
  1031. // Read the comboLength for combo in the sequence (sequence of combos)
  1032. var_uint_t pos = 0;
  1033. uint8_t comboLength = macro->guide[ pos++ ];
  1034. // Iterate through and interpret the guide
  1035. while ( comboLength != 0 )
  1036. {
  1037. // Function Counter, used to keep track of the combos processed
  1038. var_uint_t funcCount = 0;
  1039. // Iterate through the combo
  1040. while ( funcCount < comboLength )
  1041. {
  1042. // Assign TriggerGuide element (key type, state and scancode)
  1043. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  1044. // Display Function Index
  1045. printHex( guide->index );
  1046. print("|");
  1047. // Display Function Ptr Address
  1048. printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
  1049. print("|");
  1050. // Display/Lookup Capability Name (utilize debug mode of capability)
  1051. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  1052. capability( 0xFF, 0xFF, 0 );
  1053. // Display Argument(s)
  1054. print("(");
  1055. for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  1056. {
  1057. // Arguments are only 8 bit values
  1058. printHex( (&guide->args)[ arg ] );
  1059. // Only show arg separator if there are args left
  1060. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  1061. print(",");
  1062. }
  1063. print(")");
  1064. // Increment position
  1065. pos += ResultGuideSize( guide );
  1066. // Increment function count
  1067. funcCount++;
  1068. // Only show combo separator if there are combos left in the sequence element
  1069. if ( funcCount < comboLength )
  1070. print("+");
  1071. }
  1072. // Read the next comboLength
  1073. comboLength = macro->guide[ pos++ ];
  1074. // Only show sequence separator if there is another combo to process
  1075. if ( comboLength != 0 )
  1076. print(";");
  1077. }
  1078. // Display current position
  1079. print( NL "Position: " );
  1080. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1081. // Display final trigger state/type
  1082. print( NL "Final Trigger State (State/Type): " );
  1083. printHex( record->state );
  1084. print("/");
  1085. printHex( record->stateType );
  1086. }
  1087. void cliFunc_macroShow( char* args )
  1088. {
  1089. // Parse codes from arguments
  1090. char* curArgs;
  1091. char* arg1Ptr;
  1092. char* arg2Ptr = args;
  1093. // Process all args
  1094. for ( ;; )
  1095. {
  1096. curArgs = arg2Ptr;
  1097. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1098. // Stop processing args if no more are found
  1099. if ( *arg1Ptr == '\0' )
  1100. break;
  1101. // Ignore invalid codes
  1102. switch ( arg1Ptr[0] )
  1103. {
  1104. // Indexed Trigger Macro
  1105. case 'T':
  1106. macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
  1107. break;
  1108. // Indexed Result Macro
  1109. case 'R':
  1110. macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. void cliFunc_macroStep( char* args )
  1116. {
  1117. // Parse number from argument
  1118. // NOTE: Only first argument is used
  1119. char* arg1Ptr;
  1120. char* arg2Ptr;
  1121. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  1122. // Default to 1, if no argument given
  1123. var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
  1124. if ( count == 0 )
  1125. count = 1;
  1126. // Set the macro step counter, negative int's are cast to uint
  1127. macroStepCounter = count;
  1128. }