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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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)
  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)
  387. inline void Macro_interconnectAdd( 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)
  589. // Check if there are any ScanCodes in the interconnect cache to process
  590. if ( Connect_master && macroInterconnectCacheSize > 0 )
  591. {
  592. // Iterate over all the cache ScanCodes
  593. uint8_t currentInterconnectCacheSize = macroInterconnectCacheSize;
  594. macroInterconnectCacheSize = 0;
  595. for ( uint8_t c = 0; c < currentInterconnectCacheSize; c++ )
  596. {
  597. // Add to the trigger list
  598. macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = macroInterconnectCache[ c ];
  599. // TODO Handle other TriggerGuide types (e.g. analog)
  600. switch ( macroInterconnectCache[ c ].type )
  601. {
  602. // Normal (Press/Hold/Release)
  603. case 0x00:
  604. // Decide what to do based on the current state
  605. switch ( macroInterconnectCache[ c ].state )
  606. {
  607. // Re-add to interconnect cache in hold state
  608. case 0x01: // Press
  609. //case 0x02: // Hold // XXX Why does this not work? -HaaTa
  610. macroInterconnectCache[ c ].state = 0x02;
  611. macroInterconnectCache[ macroInterconnectCacheSize++ ] = macroInterconnectCache[ c ];
  612. break;
  613. case 0x03: // Remove
  614. break;
  615. // Otherwise, do not re-add
  616. }
  617. }
  618. }
  619. }
  620. #endif
  621. // If the pause flag is set, only process if the step counter is non-zero
  622. if ( macroPauseMode )
  623. {
  624. if ( macroStepCounter == 0 )
  625. return;
  626. // Proceed, decrementing the step counter
  627. macroStepCounter--;
  628. dbug_print("Macro Step");
  629. }
  630. // Process Trigger Macros
  631. Trigger_process();
  632. // Process result macros
  633. Result_process();
  634. // Signal buffer that we've used it
  635. Scan_finishedWithMacro( macroTriggerListBufferSize );
  636. // Reset TriggerList buffer
  637. macroTriggerListBufferSize = 0;
  638. // If Macro debug mode is set, clear the USB Buffer
  639. if ( macroDebugMode )
  640. {
  641. USBKeys_Modifiers = 0;
  642. USBKeys_Sent = 0;
  643. }
  644. }
  645. inline void Macro_setup()
  646. {
  647. // Register Macro CLI dictionary
  648. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  649. // Disable Macro debug mode
  650. macroDebugMode = 0;
  651. // Disable Macro pause flag
  652. macroPauseMode = 0;
  653. // Set Macro step counter to zero
  654. macroStepCounter = 0;
  655. // Make sure macro trigger buffer is empty
  656. macroTriggerListBufferSize = 0;
  657. // Set the current rotated layer to 0
  658. Macro_rotationLayer = 0;
  659. // Setup Triggers
  660. Trigger_setup();
  661. // Setup Results
  662. Result_setup();
  663. }
  664. // ----- CLI Command Functions -----
  665. void cliFunc_capList( char* args )
  666. {
  667. print( NL );
  668. info_msg("Capabilities List ");
  669. printHex( CapabilitiesNum );
  670. // Iterate through all of the capabilities and display them
  671. for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
  672. {
  673. print( NL "\t" );
  674. printHex( cap );
  675. print(" - ");
  676. // Display/Lookup Capability Name (utilize debug mode of capability)
  677. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  678. capability( 0xFF, 0xFF, 0 );
  679. }
  680. }
  681. void cliFunc_capSelect( char* args )
  682. {
  683. // Parse code from argument
  684. char* curArgs;
  685. char* arg1Ptr;
  686. char* arg2Ptr = args;
  687. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  688. var_uint_t totalArgs = 2; // Always at least two args
  689. var_uint_t cap = 0;
  690. // Arguments used for keyboard capability function
  691. var_uint_t argSetCount = 0;
  692. uint8_t *argSet = (uint8_t*)args;
  693. // Process all args
  694. for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
  695. {
  696. curArgs = arg2Ptr;
  697. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  698. // Stop processing args if no more are found
  699. // Extra arguments are ignored
  700. if ( *arg1Ptr == '\0' )
  701. break;
  702. // For the first argument, choose the capability
  703. if ( c == 0 ) switch ( arg1Ptr[0] )
  704. {
  705. // Keyboard Capability
  706. case 'K':
  707. // Determine capability index
  708. cap = numToInt( &arg1Ptr[1] );
  709. // Lookup the number of args
  710. totalArgs += CapabilitiesList[ cap ].argCount;
  711. continue;
  712. }
  713. // Because allocating memory isn't doable, and the argument count is arbitrary
  714. // The argument pointer is repurposed as the argument list (much smaller anyways)
  715. argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
  716. // Once all the arguments are prepared, call the keyboard capability function
  717. if ( argSetCount == totalArgs )
  718. {
  719. // Indicate that the capability was called
  720. print( NL );
  721. info_msg("K");
  722. printInt8( cap );
  723. print(" - ");
  724. printHex( argSet[0] );
  725. print(" - ");
  726. printHex( argSet[1] );
  727. print(" - ");
  728. printHex( argSet[2] );
  729. print( "..." NL );
  730. // Make sure this isn't the reload capability
  731. // If it is, and the remote reflash define is not set, ignore
  732. if ( flashModeEnabled_define == 0 ) for ( uint32_t cap = 0; cap < CapabilitiesNum; cap++ )
  733. {
  734. if ( CapabilitiesList[ cap ].func == (const void*)Output_flashMode_capability )
  735. {
  736. print( NL );
  737. warn_print("flashModeEnabled not set, cancelling firmware reload...");
  738. info_msg("Set flashModeEnabled to 1 in your kll configuration.");
  739. return;
  740. }
  741. }
  742. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  743. capability( argSet[0], argSet[1], &argSet[2] );
  744. }
  745. }
  746. }
  747. void cliFunc_keyHold( char* args )
  748. {
  749. // Parse codes from arguments
  750. char* curArgs;
  751. char* arg1Ptr;
  752. char* arg2Ptr = args;
  753. // Process all args
  754. for ( ;; )
  755. {
  756. curArgs = arg2Ptr;
  757. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  758. // Stop processing args if no more are found
  759. if ( *arg1Ptr == '\0' )
  760. break;
  761. // Ignore non-Scancode numbers
  762. switch ( arg1Ptr[0] )
  763. {
  764. // Scancode
  765. case 'S':
  766. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
  767. break;
  768. }
  769. }
  770. }
  771. void cliFunc_keyPress( char* args )
  772. {
  773. // Parse codes from arguments
  774. char* curArgs;
  775. char* arg1Ptr;
  776. char* arg2Ptr = args;
  777. // Process all args
  778. for ( ;; )
  779. {
  780. curArgs = arg2Ptr;
  781. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  782. // Stop processing args if no more are found
  783. if ( *arg1Ptr == '\0' )
  784. break;
  785. // Ignore non-Scancode numbers
  786. switch ( arg1Ptr[0] )
  787. {
  788. // Scancode
  789. case 'S':
  790. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  791. break;
  792. }
  793. }
  794. }
  795. void cliFunc_keyRelease( char* args )
  796. {
  797. // Parse codes from arguments
  798. char* curArgs;
  799. char* arg1Ptr;
  800. char* arg2Ptr = args;
  801. // Process all args
  802. for ( ;; )
  803. {
  804. curArgs = arg2Ptr;
  805. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  806. // Stop processing args if no more are found
  807. if ( *arg1Ptr == '\0' )
  808. break;
  809. // Ignore non-Scancode numbers
  810. switch ( arg1Ptr[0] )
  811. {
  812. // Scancode
  813. case 'S':
  814. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  815. break;
  816. }
  817. }
  818. }
  819. void cliFunc_layerDebug( char *args )
  820. {
  821. // Toggle layer debug mode
  822. layerDebugMode = layerDebugMode ? 0 : 1;
  823. print( NL );
  824. info_msg("Layer Debug Mode: ");
  825. printInt8( layerDebugMode );
  826. }
  827. void cliFunc_layerList( char* args )
  828. {
  829. print( NL );
  830. info_msg("Layer List");
  831. // Iterate through all of the layers and display them
  832. for ( uint16_t layer = 0; layer < LayerNum; layer++ )
  833. {
  834. print( NL "\t" );
  835. printHex( layer );
  836. print(" - ");
  837. // Display layer name
  838. dPrint( (char*)LayerIndex[ layer ].name );
  839. // Default map
  840. if ( layer == 0 )
  841. print(" \033[1m(default)\033[0m");
  842. // Layer State
  843. print( NL "\t\t Layer State: " );
  844. printHex( LayerState[ layer ] );
  845. // First -> Last Indices
  846. print(" First -> Last Indices: ");
  847. printHex( LayerIndex[ layer ].first );
  848. print(" -> ");
  849. printHex( LayerIndex[ layer ].last );
  850. }
  851. }
  852. void cliFunc_layerState( char* args )
  853. {
  854. // Parse codes from arguments
  855. char* curArgs;
  856. char* arg1Ptr;
  857. char* arg2Ptr = args;
  858. uint8_t arg1 = 0;
  859. uint8_t arg2 = 0;
  860. // Process first two args
  861. for ( uint8_t c = 0; c < 2; c++ )
  862. {
  863. curArgs = arg2Ptr;
  864. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  865. // Stop processing args if no more are found
  866. if ( *arg1Ptr == '\0' )
  867. break;
  868. switch ( c )
  869. {
  870. // First argument (e.g. L1)
  871. case 0:
  872. if ( arg1Ptr[0] != 'L' )
  873. return;
  874. arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
  875. break;
  876. // Second argument (e.g. 4)
  877. case 1:
  878. arg2 = (uint8_t)numToInt( arg1Ptr );
  879. // Display operation (to indicate that it worked)
  880. print( NL );
  881. info_msg("Setting Layer L");
  882. printInt8( arg1 );
  883. print(" to - ");
  884. printHex( arg2 );
  885. // Set the layer state
  886. LayerState[ arg1 ] = arg2;
  887. break;
  888. }
  889. }
  890. }
  891. void cliFunc_macroDebug( char* args )
  892. {
  893. // Toggle macro debug mode
  894. macroDebugMode = macroDebugMode ? 0 : 1;
  895. print( NL );
  896. info_msg("Macro Debug Mode: ");
  897. printInt8( macroDebugMode );
  898. }
  899. void cliFunc_macroList( char* args )
  900. {
  901. // Show pending key events
  902. print( NL );
  903. info_msg("Pending Key Events: ");
  904. printInt16( (uint16_t)macroTriggerListBufferSize );
  905. print(" : ");
  906. for ( var_uint_t key = 0; key < macroTriggerListBufferSize; key++ )
  907. {
  908. printHex( macroTriggerListBuffer[ key ].scanCode );
  909. print(" ");
  910. }
  911. // Show pending trigger macros
  912. print( NL );
  913. info_msg("Pending Trigger Macros: ");
  914. printInt16( (uint16_t)macroTriggerMacroPendingListSize );
  915. print(" : ");
  916. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  917. {
  918. printHex( macroTriggerMacroPendingList[ macro ] );
  919. print(" ");
  920. }
  921. // Show pending result macros
  922. print( NL );
  923. info_msg("Pending Result Macros: ");
  924. printInt16( (uint16_t)macroResultMacroPendingListSize );
  925. print(" : ");
  926. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  927. {
  928. printHex( macroResultMacroPendingList[ macro ] );
  929. print(" ");
  930. }
  931. // Show available trigger macro indices
  932. print( NL );
  933. info_msg("Trigger Macros Range: T0 -> T");
  934. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  935. // Show available result macro indices
  936. print( NL );
  937. info_msg("Result Macros Range: R0 -> R");
  938. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  939. // Show Trigger to Result Macro Links
  940. print( NL );
  941. info_msg("Trigger : Result Macro Pairs");
  942. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  943. {
  944. print( NL );
  945. print("\tT");
  946. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  947. print(" : R");
  948. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  949. }
  950. }
  951. void cliFunc_macroProc( char* args )
  952. {
  953. // Toggle macro pause mode
  954. macroPauseMode = macroPauseMode ? 0 : 1;
  955. print( NL );
  956. info_msg("Macro Processing Mode: ");
  957. printInt8( macroPauseMode );
  958. }
  959. void macroDebugShowTrigger( var_uint_t index )
  960. {
  961. // Only proceed if the macro exists
  962. if ( index >= TriggerMacroNum )
  963. return;
  964. // Trigger Macro Show
  965. const TriggerMacro *macro = &TriggerMacroList[ index ];
  966. TriggerMacroRecord *record = &TriggerMacroRecordList[ index ];
  967. print( NL );
  968. info_msg("Trigger Macro Index: ");
  969. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  970. print( NL );
  971. // Read the comboLength for combo in the sequence (sequence of combos)
  972. var_uint_t pos = 0;
  973. uint8_t comboLength = macro->guide[ pos ];
  974. // Iterate through and interpret the guide
  975. while ( comboLength != 0 )
  976. {
  977. // Initial position of the combo
  978. var_uint_t comboPos = ++pos;
  979. // Iterate through the combo
  980. while ( pos < comboLength * TriggerGuideSize + comboPos )
  981. {
  982. // Assign TriggerGuide element (key type, state and scancode)
  983. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  984. // Display guide information about trigger key
  985. printHex( guide->scanCode );
  986. print("|");
  987. printHex( guide->type );
  988. print("|");
  989. printHex( guide->state );
  990. // Increment position
  991. pos += TriggerGuideSize;
  992. // Only show combo separator if there are combos left in the sequence element
  993. if ( pos < comboLength * TriggerGuideSize + comboPos )
  994. print("+");
  995. }
  996. // Read the next comboLength
  997. comboLength = macro->guide[ pos ];
  998. // Only show sequence separator if there is another combo to process
  999. if ( comboLength != 0 )
  1000. print(";");
  1001. }
  1002. // Display current position
  1003. print( NL "Position: " );
  1004. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1005. // Display result macro index
  1006. print( NL "Result Macro Index: " );
  1007. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  1008. // Display trigger macro state
  1009. print( NL "Trigger Macro State: " );
  1010. switch ( record->state )
  1011. {
  1012. case TriggerMacro_Press: print("Press"); break;
  1013. case TriggerMacro_Release: print("Release"); break;
  1014. case TriggerMacro_Waiting: print("Waiting"); break;
  1015. }
  1016. }
  1017. void macroDebugShowResult( var_uint_t index )
  1018. {
  1019. // Only proceed if the macro exists
  1020. if ( index >= ResultMacroNum )
  1021. return;
  1022. // Trigger Macro Show
  1023. const ResultMacro *macro = &ResultMacroList[ index ];
  1024. ResultMacroRecord *record = &ResultMacroRecordList[ index ];
  1025. print( NL );
  1026. info_msg("Result Macro Index: ");
  1027. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1028. print( NL );
  1029. // Read the comboLength for combo in the sequence (sequence of combos)
  1030. var_uint_t pos = 0;
  1031. uint8_t comboLength = macro->guide[ pos++ ];
  1032. // Iterate through and interpret the guide
  1033. while ( comboLength != 0 )
  1034. {
  1035. // Function Counter, used to keep track of the combos processed
  1036. var_uint_t funcCount = 0;
  1037. // Iterate through the combo
  1038. while ( funcCount < comboLength )
  1039. {
  1040. // Assign TriggerGuide element (key type, state and scancode)
  1041. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  1042. // Display Function Index
  1043. printHex( guide->index );
  1044. print("|");
  1045. // Display Function Ptr Address
  1046. printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
  1047. print("|");
  1048. // Display/Lookup Capability Name (utilize debug mode of capability)
  1049. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  1050. capability( 0xFF, 0xFF, 0 );
  1051. // Display Argument(s)
  1052. print("(");
  1053. for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  1054. {
  1055. // Arguments are only 8 bit values
  1056. printHex( (&guide->args)[ arg ] );
  1057. // Only show arg separator if there are args left
  1058. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  1059. print(",");
  1060. }
  1061. print(")");
  1062. // Increment position
  1063. pos += ResultGuideSize( guide );
  1064. // Increment function count
  1065. funcCount++;
  1066. // Only show combo separator if there are combos left in the sequence element
  1067. if ( funcCount < comboLength )
  1068. print("+");
  1069. }
  1070. // Read the next comboLength
  1071. comboLength = macro->guide[ pos++ ];
  1072. // Only show sequence separator if there is another combo to process
  1073. if ( comboLength != 0 )
  1074. print(";");
  1075. }
  1076. // Display current position
  1077. print( NL "Position: " );
  1078. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1079. // Display final trigger state/type
  1080. print( NL "Final Trigger State (State/Type): " );
  1081. printHex( record->state );
  1082. print("/");
  1083. printHex( record->stateType );
  1084. }
  1085. void cliFunc_macroShow( char* args )
  1086. {
  1087. // Parse codes from arguments
  1088. char* curArgs;
  1089. char* arg1Ptr;
  1090. char* arg2Ptr = args;
  1091. // Process all args
  1092. for ( ;; )
  1093. {
  1094. curArgs = arg2Ptr;
  1095. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1096. // Stop processing args if no more are found
  1097. if ( *arg1Ptr == '\0' )
  1098. break;
  1099. // Ignore invalid codes
  1100. switch ( arg1Ptr[0] )
  1101. {
  1102. // Indexed Trigger Macro
  1103. case 'T':
  1104. macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
  1105. break;
  1106. // Indexed Result Macro
  1107. case 'R':
  1108. macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. void cliFunc_macroStep( char* args )
  1114. {
  1115. // Parse number from argument
  1116. // NOTE: Only first argument is used
  1117. char* arg1Ptr;
  1118. char* arg2Ptr;
  1119. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  1120. // Default to 1, if no argument given
  1121. var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
  1122. if ( count == 0 )
  1123. count = 1;
  1124. // Set the macro step counter, negative int's are cast to uint
  1125. macroStepCounter = count;
  1126. }