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 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /* Copyright (C) 2014 by Jacob Alexander
  2. *
  3. * This file is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This file is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this file. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // ----- Includes -----
  17. // Compiler Includes
  18. #include <Lib/MacroLib.h>
  19. // Project Includes
  20. #include <cli.h>
  21. #include <led.h>
  22. #include <print.h>
  23. #include <scan_loop.h>
  24. // Keymaps
  25. #include "usb_hid.h"
  26. #include <defaultMap.h>
  27. #include "generatedKeymap.h" // TODO Use actual generated version
  28. // Local Includes
  29. #include "macro.h"
  30. // ----- Function Declarations -----
  31. void cliFunc_capList ( char* args );
  32. void cliFunc_capSelect ( char* args );
  33. void cliFunc_keyHold ( char* args );
  34. void cliFunc_keyPress ( char* args );
  35. void cliFunc_keyRelease( char* args );
  36. void cliFunc_layerList ( char* args );
  37. void cliFunc_layerState( char* args );
  38. void cliFunc_macroDebug( char* args );
  39. void cliFunc_macroList ( char* args );
  40. void cliFunc_macroProc ( char* args );
  41. void cliFunc_macroShow ( char* args );
  42. void cliFunc_macroStep ( char* args );
  43. // ----- Enums -----
  44. // Bit positions are important, passes (correct key) always trump incorrect key votes
  45. typedef enum TriggerMacroVote {
  46. TriggerMacroVote_Release = 0x10, // Correct key
  47. TriggerMacroVote_PassRelease = 0x18, // Correct key (both pass and release)
  48. TriggerMacroVote_Pass = 0x8, // Correct key
  49. TriggerMacroVote_DoNothingRelease = 0x4, // Incorrect key
  50. TriggerMacroVote_DoNothing = 0x2, // Incorrect key
  51. TriggerMacroVote_Fail = 0x1, // Incorrect key
  52. TriggerMacroVote_Invalid = 0x0, // Invalid state
  53. } TriggerMacroVote;
  54. typedef enum TriggerMacroEval {
  55. TriggerMacroEval_DoNothing,
  56. TriggerMacroEval_DoResult,
  57. TriggerMacroEval_DoResultAndRemove,
  58. TriggerMacroEval_Remove,
  59. } TriggerMacroEval;
  60. typedef enum ResultMacroEval {
  61. ResultMacroEval_DoNothing,
  62. ResultMacroEval_Remove,
  63. } ResultMacroEval;
  64. // ----- Variables -----
  65. // Macro Module command dictionary
  66. const char macroCLIDictName[] = "Macro Module Commands";
  67. const CLIDictItem macroCLIDict[] = {
  68. { "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
  69. { "capSelect", "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
  70. { "keyHold", "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyHold },
  71. { "keyPress", "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
  72. { "keyRelease", "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
  73. { "layerList", "List available layers.", cliFunc_layerList },
  74. { "layerState", "Modify specified indexed layer state <layer> <state byte>." NL "\t\t\033[35mL2\033[0m Indexed Layer 0x02" NL "\t\t0 Off, 1 Shift, 2 Latch, 4 Lock States", cliFunc_layerState },
  75. { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
  76. { "macroList", "List the defined trigger and result macros.", cliFunc_macroList },
  77. { "macroProc", "Pause/Resume macro processing.", cliFunc_macroProc },
  78. { "macroShow", "Show the macro corresponding to the given index." NL "\t\t\033[35mT16\033[0m Indexed Trigger Macro 0x10, \033[35mR12\033[0m Indexed Result Macro 0x0C", cliFunc_macroShow },
  79. { "macroStep", "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
  80. { 0, 0, 0 } // Null entry for dictionary end
  81. };
  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. unsigned int macroStepCounter = 0;
  88. // Key Trigger List Buffer
  89. TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
  90. uint8_t macroTriggerListBufferSize = 0;
  91. // Pending Trigger Macro Index List
  92. // * Any trigger macros that need processing from a previous macro processing loop
  93. // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
  94. // Possibly could be calculated by the KLL compiler
  95. // XXX It may be possible to calculate the worst case using the KLL compiler
  96. unsigned int macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
  97. unsigned int macroTriggerMacroPendingListSize = 0;
  98. // Layer Index Stack
  99. // * When modifying layer state and the state is non-0x0, the stack must be adjusted
  100. unsigned int macroLayerIndexStack[ LayerNum ] = { 0 };
  101. unsigned int macroLayerIndexStackSize = 0;
  102. // Pending Result Macro Index List
  103. // * Any result macro that needs processing from a previous macro processing loop
  104. unsigned int macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
  105. unsigned int macroResultMacroPendingListSize = 0;
  106. // ----- Capabilities -----
  107. // Modifies the specified Layer control byte
  108. // Argument #1: Layer Index -> unsigned int
  109. // Argument #2: Toggle byte -> uint8_t
  110. void Macro_layerStateToggle_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  111. {
  112. // Display capability name
  113. if ( stateType == 0xFF && state == 0xFF )
  114. {
  115. print("Macro_layerState(layerIndex,toggleByte)");
  116. return;
  117. }
  118. // Get layer index from arguments
  119. // Cast pointer to uint8_t to unsigned int then access that memory location
  120. unsigned int layer = *(unsigned int*)(&args[0]);
  121. // Get layer toggle byte
  122. uint8_t toggleByte = args[ sizeof(unsigned int) ];
  123. // Is layer in the LayerIndexStack?
  124. uint8_t inLayerIndexStack = 0;
  125. unsigned int stackItem = 0;
  126. while ( stackItem < macroLayerIndexStackSize )
  127. {
  128. // Flag if layer is already in the LayerIndexStack
  129. if ( macroLayerIndexStack[ stackItem ] == layer )
  130. {
  131. inLayerIndexStack = 1;
  132. break;
  133. }
  134. // Increment to next item
  135. stackItem++;
  136. }
  137. // Toggle Layer State Byte
  138. if ( LayerIndex[ layer ].state & toggleByte )
  139. {
  140. // Unset
  141. LayerIndex[ layer ].state &= ~toggleByte;
  142. }
  143. else
  144. {
  145. // Set
  146. LayerIndex[ layer ].state |= toggleByte;
  147. }
  148. // If the layer was not in the LayerIndexStack add it
  149. if ( !inLayerIndexStack )
  150. {
  151. macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
  152. }
  153. // If the layer is in the LayerIndexStack and the state is 0x00, remove
  154. if ( LayerIndex[ layer ].state == 0x00 && inLayerIndexStack )
  155. {
  156. // Remove the layer from the LayerIndexStack
  157. // Using the already positioned stackItem variable from the loop above
  158. while ( stackItem < macroLayerIndexStackSize )
  159. {
  160. macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
  161. stackItem++;
  162. }
  163. // Reduce LayerIndexStack size
  164. macroLayerIndexStackSize--;
  165. }
  166. }
  167. // ----- Functions -----
  168. // Looks up the trigger list for the given scan code (from the active layer)
  169. // NOTE: Calling function must handle the NULL pointer case
  170. unsigned int *Macro_layerLookup( uint8_t scanCode )
  171. {
  172. // If no trigger macro is defined at the given layer, fallthrough to the next layer
  173. for ( unsigned int layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
  174. {
  175. // Lookup Layer
  176. Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
  177. // Check if latch has been pressed for this layer
  178. // XXX Regardless of whether a key is found, the latch is removed on first lookup
  179. uint8_t latch = layer->state & 0x02;
  180. if ( latch )
  181. {
  182. layer->state &= ~0x02;
  183. }
  184. // Only use layer, if state is valid
  185. // XOR each of the state bits
  186. // If only two are enabled, do not use this state
  187. if ( (layer->state & 0x01) ^ (latch>>1) ^ ((layer->state & 0x04)>>2) )
  188. {
  189. // Lookup layer
  190. unsigned int **map = (unsigned int**)layer->triggerMap;
  191. // Determine if layer has key defined
  192. if ( map != 0 && *map[ scanCode ] != 0 )
  193. return map[ scanCode ];
  194. }
  195. }
  196. // Do lookup on default layer
  197. unsigned int **map = (unsigned int**)LayerIndex[0].triggerMap;
  198. // Determine if layer has key defined
  199. if ( map == 0 && *map[ scanCode ] == 0 )
  200. {
  201. erro_msg("Scan Code has no defined Trigger Macro: ");
  202. printHex( scanCode );
  203. return 0;
  204. }
  205. // Return lookup result
  206. return map[ scanCode ];
  207. }
  208. // Update the scancode key state
  209. // States:
  210. // * 0x00 - Off
  211. // * 0x01 - Pressed
  212. // * 0x02 - Held
  213. // * 0x03 - Released
  214. // * 0x04 - Unpressed (this is currently ignored)
  215. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  216. {
  217. // Only add to macro trigger list if one of three states
  218. switch ( state )
  219. {
  220. case 0x01: // Pressed
  221. case 0x02: // Held
  222. case 0x03: // Released
  223. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  224. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  225. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
  226. macroTriggerListBufferSize++;
  227. break;
  228. }
  229. }
  230. // Update the scancode analog state
  231. // States:
  232. // * 0x00 - Off
  233. // * 0x01 - Released
  234. // * 0x02-0xFF - Analog value (low to high)
  235. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  236. {
  237. // Only add to macro trigger list if non-off
  238. if ( state != 0x00 )
  239. {
  240. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  241. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  242. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
  243. macroTriggerListBufferSize++;
  244. }
  245. }
  246. // Update led state
  247. // States:
  248. // * 0x00 - Off
  249. // * 0x01 - On
  250. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  251. {
  252. // Only add to macro trigger list if non-off
  253. if ( state != 0x00 )
  254. {
  255. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
  256. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  257. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
  258. macroTriggerListBufferSize++;
  259. }
  260. }
  261. // Append result macro to pending list, checking for duplicates
  262. // Do nothing if duplicate
  263. inline void Macro_appendResultMacroToPendingList( TriggerMacro *triggerMacro )
  264. {
  265. // Lookup result macro index
  266. unsigned int resultMacroIndex = triggerMacro->result;
  267. // Iterate through result macro pending list, making sure this macro hasn't been added yet
  268. for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  269. {
  270. // If duplicate found, do nothing
  271. if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
  272. return;
  273. }
  274. // No duplicates found, add to pending list
  275. macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
  276. // Lookup scanCode of the last key in the last combo
  277. unsigned int pos = 0;
  278. for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
  279. {
  280. pos += TriggerGuideSize * comboLength + 1;
  281. comboLength = triggerMacro->guide[ pos ];
  282. }
  283. uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
  284. // Lookup scanCode in buffer list for the current state and stateType
  285. for ( uint8_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
  286. {
  287. if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
  288. {
  289. ResultMacroList[ resultMacroIndex ].state = macroTriggerListBuffer[ keyIndex ].state;
  290. ResultMacroList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
  291. }
  292. }
  293. // Reset the macro position
  294. ResultMacroList[ resultMacroIndex ].pos = 0;
  295. }
  296. // Determine if long ResultMacro (more than 1 seqence element)
  297. inline uint8_t Macro_isLongResultMacro( ResultMacro *macro )
  298. {
  299. // Check the second sequence combo length
  300. // If non-zero return non-zero (long sequence)
  301. // 0 otherwise (short sequence)
  302. unsigned int position = 1;
  303. for ( unsigned int result = 0; result < macro->guide[0]; result++ )
  304. position += ResultGuideSize( (ResultGuide*)&macro->guide[ position ] );
  305. return macro->guide[ position ];
  306. }
  307. // Determine if long TriggerMacro (more than 1 sequence element)
  308. inline uint8_t Macro_isLongTriggerMacro( TriggerMacro *macro )
  309. {
  310. // Check the second sequence combo length
  311. // If non-zero return non-zero (long sequence)
  312. // 0 otherwise (short sequence)
  313. return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ];
  314. }
  315. // Votes on the given key vs. guide, short macros
  316. inline TriggerMacroVote Macro_evalShortTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  317. {
  318. // Depending on key type
  319. switch ( guide->type )
  320. {
  321. // Normal State Type
  322. case 0x00:
  323. // For short TriggerMacros completely ignore incorrect keys
  324. if ( guide->scanCode == key->scanCode )
  325. {
  326. switch ( key->state )
  327. {
  328. // Correct key, pressed, possible passing
  329. case 0x01:
  330. return TriggerMacroVote_Pass;
  331. // Correct key, held, possible passing or release
  332. case 0x02:
  333. return TriggerMacroVote_PassRelease;
  334. // Correct key, released, possible release
  335. case 0x03:
  336. return TriggerMacroVote_Release;
  337. }
  338. }
  339. return TriggerMacroVote_DoNothing;
  340. // LED State Type
  341. case 0x01:
  342. erro_print("LED State Type - Not implemented...");
  343. break;
  344. // Analog State Type
  345. case 0x02:
  346. erro_print("Analog State Type - Not implemented...");
  347. break;
  348. // Invalid State Type
  349. default:
  350. erro_print("Invalid State Type. This is a bug.");
  351. break;
  352. }
  353. // XXX Shouldn't reach here
  354. return TriggerMacroVote_Invalid;
  355. }
  356. // Votes on the given key vs. guide, long macros
  357. // A long macro is defined as a guide with more than 1 combo
  358. inline TriggerMacroVote Macro_evalLongTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  359. {
  360. // Depending on key type
  361. switch ( guide->type )
  362. {
  363. // Normal State Type
  364. case 0x00:
  365. // Depending on the state of the buffered key, make voting decision
  366. // Incorrect key
  367. if ( guide->scanCode != key->scanCode )
  368. {
  369. switch ( key->state )
  370. {
  371. // Wrong key, pressed, fail
  372. case 0x01:
  373. return TriggerMacroVote_Fail;
  374. // Wrong key, held, do not pass (no effect)
  375. case 0x02:
  376. return TriggerMacroVote_DoNothing;
  377. // Wrong key released, fail out if pos == 0
  378. case 0x03:
  379. return TriggerMacroVote_DoNothing | TriggerMacroVote_DoNothingRelease;
  380. }
  381. }
  382. // Correct key
  383. else
  384. {
  385. switch ( key->state )
  386. {
  387. // Correct key, pressed, possible passing
  388. case 0x01:
  389. return TriggerMacroVote_Pass;
  390. // Correct key, held, possible passing or release
  391. case 0x02:
  392. return TriggerMacroVote_PassRelease;
  393. // Correct key, released, possible release
  394. case 0x03:
  395. return TriggerMacroVote_Release;
  396. }
  397. }
  398. break;
  399. // LED State Type
  400. case 0x01:
  401. erro_print("LED State Type - Not implemented...");
  402. break;
  403. // Analog State Type
  404. case 0x02:
  405. erro_print("Analog State Type - Not implemented...");
  406. break;
  407. // Invalid State Type
  408. default:
  409. erro_print("Invalid State Type. This is a bug.");
  410. break;
  411. }
  412. // XXX Shouldn't reach here
  413. return TriggerMacroVote_Invalid;
  414. }
  415. // Evaluate/Update TriggerMacro
  416. inline TriggerMacroEval Macro_evalTriggerMacro( unsigned int triggerMacroIndex )
  417. {
  418. // Lookup TriggerMacro
  419. TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
  420. // Check if macro has finished and should be incremented sequence elements
  421. if ( macro->state == TriggerMacro_Release )
  422. {
  423. macro->state = TriggerMacro_Waiting;
  424. macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1;
  425. }
  426. // Current Macro position
  427. unsigned int pos = macro->pos;
  428. // Length of the combo being processed
  429. uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
  430. // If no combo items are left, remove the TriggerMacro from the pending list
  431. if ( comboLength == 0 )
  432. {
  433. return TriggerMacroEval_Remove;
  434. }
  435. // Check if this is a long Trigger Macro
  436. uint8_t longMacro = Macro_isLongTriggerMacro( macro );
  437. // Iterate through the items in the combo, voting the on the key state
  438. // If any of the pressed keys do not match, fail the macro
  439. //
  440. // The macro is waiting for input when in the TriggerMacro_Waiting state
  441. // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
  442. // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
  443. // On scan after position increment, change to TriggerMacro_Waiting state
  444. // TODO Add support for system LED states (NumLock, CapsLock, etc.)
  445. // TODO Add support for analog key states
  446. // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
  447. // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
  448. TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
  449. for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
  450. {
  451. // Assign TriggerGuide element (key type, state and scancode)
  452. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
  453. TriggerMacroVote vote = TriggerMacroVote_Invalid;
  454. // Iterate through the key buffer, comparing to each key in the combo
  455. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  456. {
  457. // Lookup key information
  458. TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
  459. // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
  460. // Also mask all of the non-passing votes
  461. vote |= longMacro
  462. ? Macro_evalLongTriggerMacroVote( keyInfo, guide )
  463. : Macro_evalShortTriggerMacroVote( keyInfo, guide );
  464. if ( vote >= TriggerMacroVote_Pass )
  465. {
  466. vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
  467. break;
  468. }
  469. }
  470. // If no pass vote was found after scanning all of the keys
  471. // Fail the combo, if this is a short macro (long macros already will have a fail vote)
  472. if ( !longMacro && vote < TriggerMacroVote_Pass )
  473. vote |= TriggerMacroVote_Fail;
  474. // After voting, append to overall vote
  475. overallVote |= vote;
  476. }
  477. // If no pass vote was found after scanning the entire combo
  478. // And this is the first position in the combo, just remove it (nothing important happened)
  479. if ( longMacro && overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
  480. overallVote |= TriggerMacroVote_Fail;
  481. // Decide new state of macro after voting
  482. // Fail macro, remove from pending list
  483. if ( overallVote & TriggerMacroVote_Fail )
  484. {
  485. return TriggerMacroEval_Remove;
  486. }
  487. // Do nothing, incorrect key is being held or released
  488. else if ( overallVote & TriggerMacroVote_DoNothing && longMacro )
  489. {
  490. // Just doing nothing :)
  491. }
  492. // If passing and in Waiting state, set macro state to Press
  493. else if ( overallVote & TriggerMacroVote_Pass
  494. && ( macro->state == TriggerMacro_Waiting || macro->state == TriggerMacro_Press ) )
  495. {
  496. macro->state = TriggerMacro_Press;
  497. // If in press state, and this is the final combo, send request for ResultMacro
  498. // Check to see if the result macro only has a single element
  499. // If this result macro has more than 1 key, only send once
  500. // TODO Add option to have long macro repeat rate
  501. if ( macro->guide[ pos + comboLength + 1 ] == 0 )
  502. {
  503. // Long result macro (more than 1 combo)
  504. if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
  505. {
  506. // Only ever trigger result once, on press
  507. if ( overallVote == TriggerMacroVote_Pass )
  508. {
  509. return TriggerMacroEval_DoResultAndRemove;
  510. }
  511. }
  512. // Short result macro
  513. else
  514. {
  515. // Only trigger result once, on press, if long trigger (more than 1 combo)
  516. if ( Macro_isLongTriggerMacro( macro ) )
  517. {
  518. return TriggerMacroEval_DoResultAndRemove;
  519. }
  520. // Otherwise, trigger result continuously
  521. else
  522. {
  523. return TriggerMacroEval_DoResult;
  524. }
  525. }
  526. }
  527. }
  528. // If ready for transition and in Press state, set to Waiting and increment combo position
  529. // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
  530. else if ( overallVote & TriggerMacroVote_Release && macro->state == TriggerMacro_Press )
  531. {
  532. macro->state = TriggerMacro_Release;
  533. // If this is the last combo in the sequence, remove from the pending list
  534. if ( macro->guide[ macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1 ] == 0 )
  535. return TriggerMacroEval_Remove;
  536. }
  537. // Otherwise, just remove the macro on key release
  538. // XXX Might cause some issues
  539. else if ( overallVote & TriggerMacroVote_Release )
  540. {
  541. return TriggerMacroEval_Remove;
  542. }
  543. // If this is a short macro, just remove it
  544. // The state can be rebuilt on the next iteration
  545. if ( !longMacro )
  546. return TriggerMacroEval_Remove;
  547. return TriggerMacroEval_DoNothing;
  548. }
  549. // Evaluate/Update ResultMacro
  550. inline ResultMacroEval Macro_evalResultMacro( unsigned int resultMacroIndex )
  551. {
  552. // Lookup ResultMacro
  553. ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
  554. // Current Macro position
  555. unsigned int pos = macro->pos;
  556. // Length of combo being processed
  557. uint8_t comboLength = macro->guide[ pos ];
  558. // Function Counter, used to keep track of the combo items processed
  559. unsigned int funcCount = 0;
  560. // Combo Item Position within the guide
  561. unsigned int comboItem = pos + 1;
  562. // Iterate through the Result Combo
  563. while ( funcCount < comboLength )
  564. {
  565. // Assign TriggerGuide element (key type, state and scancode)
  566. ResultGuide *guide = (ResultGuide*)(&macro->guide[ comboItem ]);
  567. // Do lookup on capability function
  568. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  569. // Call capability
  570. capability( macro->state, macro->stateType, &guide->args );
  571. // Increment counters
  572. funcCount++;
  573. comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
  574. }
  575. // Move to next item in the sequence
  576. macro->pos = comboItem;
  577. // If the ResultMacro is finished, remove
  578. if ( macro->guide[ comboItem ] == 0 )
  579. {
  580. return ResultMacroEval_Remove;
  581. }
  582. // Otherwise leave the macro in the list
  583. return ResultMacroEval_DoNothing;
  584. }
  585. // Update pending trigger list
  586. inline void Macro_updateTriggerMacroPendingList()
  587. {
  588. // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
  589. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  590. {
  591. // TODO LED States
  592. // TODO Analog Switches
  593. // Only add TriggerMacro to pending list if key was pressed (not held, released or off)
  594. if ( macroTriggerListBuffer[ key ].state == 0x00 && macroTriggerListBuffer[ key ].state != 0x01 )
  595. continue;
  596. // Lookup Trigger List
  597. unsigned int *triggerList = Macro_layerLookup( macroTriggerListBuffer[ key ].scanCode );
  598. // Number of Triggers in list
  599. unsigned int triggerListSize = triggerList[0];
  600. // Iterate over triggerList to see if any TriggerMacros need to be added
  601. // First item is the number of items in the TriggerList
  602. for ( unsigned int macro = 1; macro < triggerListSize + 1; macro++ )
  603. {
  604. // Lookup trigger macro index
  605. unsigned int triggerMacroIndex = triggerList[ macro ];
  606. // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
  607. // triggerList needs to be added
  608. unsigned int pending = 0;
  609. for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
  610. {
  611. // Stop scanning if the trigger macro index is found in the pending list
  612. if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
  613. break;
  614. }
  615. // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
  616. // Add it to the list
  617. if ( pending == macroTriggerMacroPendingListSize )
  618. {
  619. macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
  620. // Reset macro position
  621. TriggerMacroList[ triggerMacroIndex ].pos = 0;
  622. TriggerMacroList[ triggerMacroIndex ].state = TriggerMacro_Waiting;
  623. }
  624. }
  625. }
  626. }
  627. // Macro Procesing Loop
  628. // Called once per USB buffer send
  629. inline void Macro_process()
  630. {
  631. // Only do one round of macro processing between Output Module timer sends
  632. if ( USBKeys_Sent != 0 )
  633. return;
  634. // If the pause flag is set, only process if the step counter is non-zero
  635. if ( macroPauseMode )
  636. {
  637. if ( macroStepCounter == 0 )
  638. return;
  639. // Proceed, decrementing the step counter
  640. macroStepCounter--;
  641. dbug_print("Macro Step");
  642. }
  643. // Update pending trigger list, before processing TriggerMacros
  644. Macro_updateTriggerMacroPendingList();
  645. // Tail pointer for macroTriggerMacroPendingList
  646. // Macros must be explicitly re-added
  647. unsigned int macroTriggerMacroPendingListTail = 0;
  648. // Iterate through the pending TriggerMacros, processing each of them
  649. for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  650. {
  651. switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
  652. {
  653. // Trigger Result Macro (purposely falling through)
  654. case TriggerMacroEval_DoResult:
  655. // Append ResultMacro to PendingList
  656. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  657. default:
  658. macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
  659. break;
  660. // Trigger Result Macro and Remove (purposely falling through)
  661. case TriggerMacroEval_DoResultAndRemove:
  662. // Append ResultMacro to PendingList
  663. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  664. // Remove Macro from Pending List, nothing to do, removing by default
  665. case TriggerMacroEval_Remove:
  666. break;
  667. }
  668. }
  669. // Update the macroTriggerMacroPendingListSize with the tail pointer
  670. macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
  671. // Tail pointer for macroResultMacroPendingList
  672. // Macros must be explicitly re-added
  673. unsigned int macroResultMacroPendingListTail = 0;
  674. // Iterate through the pending ResultMacros, processing each of them
  675. for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  676. {
  677. switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
  678. {
  679. // Re-add macros to pending list
  680. case ResultMacroEval_DoNothing:
  681. default:
  682. macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
  683. break;
  684. // Remove Macro from Pending List, nothing to do, removing by default
  685. case ResultMacroEval_Remove:
  686. break;
  687. }
  688. }
  689. // Update the macroResultMacroPendingListSize with the tail pointer
  690. macroResultMacroPendingListSize = macroResultMacroPendingListTail;
  691. // Signal buffer that we've used it
  692. Scan_finishedWithMacro( macroTriggerListBufferSize );
  693. // Reset TriggerList buffer
  694. macroTriggerListBufferSize = 0;
  695. // If Macro debug mode is set, clear the USB Buffer
  696. if ( macroDebugMode )
  697. {
  698. USBKeys_Modifiers = 0;
  699. USBKeys_Sent = 0;
  700. }
  701. }
  702. inline void Macro_setup()
  703. {
  704. // Register Macro CLI dictionary
  705. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  706. // Disable Macro debug mode
  707. macroDebugMode = 0;
  708. // Disable Macro pause flag
  709. macroPauseMode = 0;
  710. // Set Macro step counter to zero
  711. macroStepCounter = 0;
  712. // Make sure macro trigger buffer is empty
  713. macroTriggerListBufferSize = 0;
  714. // Initialize TriggerMacro states
  715. for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
  716. {
  717. TriggerMacroList[ macro ].pos = 0;
  718. TriggerMacroList[ macro ].state = TriggerMacro_Waiting;
  719. }
  720. // Initialize ResultMacro states
  721. for ( unsigned int macro = 0; macro < ResultMacroNum; macro++ )
  722. {
  723. ResultMacroList[ macro ].pos = 0;
  724. ResultMacroList[ macro ].state = 0;
  725. ResultMacroList[ macro ].stateType = 0;
  726. }
  727. }
  728. // ----- CLI Command Functions -----
  729. void cliFunc_capList( char* args )
  730. {
  731. print( NL );
  732. info_msg("Capabilities List");
  733. printHex( CapabilitiesNum );
  734. // Iterate through all of the capabilities and display them
  735. for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
  736. {
  737. print( NL "\t" );
  738. printHex( cap );
  739. print(" - ");
  740. // Display/Lookup Capability Name (utilize debug mode of capability)
  741. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  742. capability( 0xFF, 0xFF, 0 );
  743. }
  744. }
  745. void cliFunc_capSelect( char* args )
  746. {
  747. // Parse code from argument
  748. char* curArgs;
  749. char* arg1Ptr;
  750. char* arg2Ptr = args;
  751. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  752. unsigned int totalArgs = 2; // Always at least two args
  753. unsigned int cap = 0;
  754. // Arguments used for keyboard capability function
  755. unsigned int argSetCount = 0;
  756. uint8_t *argSet = (uint8_t*)args;
  757. // Process all args
  758. for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
  759. {
  760. curArgs = arg2Ptr;
  761. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  762. // Stop processing args if no more are found
  763. // Extra arguments are ignored
  764. if ( *arg1Ptr == '\0' )
  765. break;
  766. // For the first argument, choose the capability
  767. if ( c == 0 ) switch ( arg1Ptr[0] )
  768. {
  769. // Keyboard Capability
  770. case 'K':
  771. // Determine capability index
  772. cap = numToInt( &arg1Ptr[1] );
  773. // Lookup the number of args
  774. totalArgs += CapabilitiesList[ cap ].argCount;
  775. continue;
  776. }
  777. // Because allocating memory isn't doable, and the argument count is arbitrary
  778. // The argument pointer is repurposed as the argument list (much smaller anyways)
  779. argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
  780. // Once all the arguments are prepared, call the keyboard capability function
  781. if ( argSetCount == totalArgs )
  782. {
  783. // Indicate that the capability was called
  784. print( NL );
  785. info_msg("K");
  786. printInt8( cap );
  787. print(" - ");
  788. printHex( argSet[0] );
  789. print(" - ");
  790. printHex( argSet[1] );
  791. print(" - ");
  792. printHex( argSet[2] );
  793. print( "..." NL );
  794. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  795. capability( argSet[0], argSet[1], &argSet[2] );
  796. }
  797. }
  798. }
  799. void cliFunc_keyHold( char* args )
  800. {
  801. // Parse codes from arguments
  802. char* curArgs;
  803. char* arg1Ptr;
  804. char* arg2Ptr = args;
  805. // Process all args
  806. for ( ;; )
  807. {
  808. curArgs = arg2Ptr;
  809. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  810. // Stop processing args if no more are found
  811. if ( *arg1Ptr == '\0' )
  812. break;
  813. // Ignore non-Scancode numbers
  814. switch ( arg1Ptr[0] )
  815. {
  816. // Scancode
  817. case 'S':
  818. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
  819. break;
  820. }
  821. }
  822. }
  823. void cliFunc_keyPress( char* args )
  824. {
  825. // Parse codes from arguments
  826. char* curArgs;
  827. char* arg1Ptr;
  828. char* arg2Ptr = args;
  829. // Process all args
  830. for ( ;; )
  831. {
  832. curArgs = arg2Ptr;
  833. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  834. // Stop processing args if no more are found
  835. if ( *arg1Ptr == '\0' )
  836. break;
  837. // Ignore non-Scancode numbers
  838. switch ( arg1Ptr[0] )
  839. {
  840. // Scancode
  841. case 'S':
  842. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  843. break;
  844. }
  845. }
  846. }
  847. void cliFunc_keyRelease( char* args )
  848. {
  849. // Parse codes from arguments
  850. char* curArgs;
  851. char* arg1Ptr;
  852. char* arg2Ptr = args;
  853. // Process all args
  854. for ( ;; )
  855. {
  856. curArgs = arg2Ptr;
  857. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  858. // Stop processing args if no more are found
  859. if ( *arg1Ptr == '\0' )
  860. break;
  861. // Ignore non-Scancode numbers
  862. switch ( arg1Ptr[0] )
  863. {
  864. // Scancode
  865. case 'S':
  866. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  867. break;
  868. }
  869. }
  870. }
  871. void cliFunc_layerList( char* args )
  872. {
  873. print( NL );
  874. info_msg("Layer List");
  875. // Iterate through all of the layers and display them
  876. for ( unsigned int layer = 0; layer < LayerNum; layer++ )
  877. {
  878. print( NL "\t" );
  879. printHex( layer );
  880. print(" - ");
  881. // Display layer name
  882. dPrint( (char*)LayerIndex[ layer ].name );
  883. // Default map
  884. if ( layer == 0 )
  885. print(" \033[1m(default)\033[0m");
  886. // Layer State
  887. print( NL "\t\t Layer State: " );
  888. printHex( LayerIndex[ layer ].state );
  889. // Max Index
  890. print(" Max Index: ");
  891. printHex( LayerIndex[ layer ].max );
  892. }
  893. }
  894. void cliFunc_layerState( char* args )
  895. {
  896. // Parse codes from arguments
  897. char* curArgs;
  898. char* arg1Ptr;
  899. char* arg2Ptr = args;
  900. uint8_t arg1 = 0;
  901. uint8_t arg2 = 0;
  902. // Process first two args
  903. for ( uint8_t c = 0; c < 2; c++ )
  904. {
  905. curArgs = arg2Ptr;
  906. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  907. // Stop processing args if no more are found
  908. if ( *arg1Ptr == '\0' )
  909. break;
  910. switch ( c )
  911. {
  912. // First argument (e.g. L1)
  913. case 0:
  914. if ( arg1Ptr[0] != 'L' )
  915. return;
  916. arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
  917. break;
  918. // Second argument (e.g. 4)
  919. case 1:
  920. arg2 = (uint8_t)numToInt( arg1Ptr );
  921. // Display operation (to indicate that it worked)
  922. print( NL );
  923. info_msg("Setting Layer L");
  924. printInt8( arg1 );
  925. print(" to - ");
  926. printHex( arg2 );
  927. // Set the layer state
  928. LayerIndex[ arg1 ].state = arg2;
  929. break;
  930. }
  931. }
  932. }
  933. void cliFunc_macroDebug( char* args )
  934. {
  935. // Toggle macro debug mode
  936. macroDebugMode = macroDebugMode ? 0 : 1;
  937. print( NL );
  938. info_msg("Macro Debug Mode: ");
  939. printInt8( macroDebugMode );
  940. }
  941. void cliFunc_macroList( char* args )
  942. {
  943. // Show pending key events
  944. print( NL );
  945. info_msg("Pending Key Events: ");
  946. printInt16( (uint16_t)macroTriggerListBufferSize );
  947. print(" : ");
  948. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  949. {
  950. printHex( macroTriggerListBuffer[ key ].scanCode );
  951. print(" ");
  952. }
  953. // Show pending trigger macros
  954. print( NL );
  955. info_msg("Pending Trigger Macros: ");
  956. printInt16( (uint16_t)macroTriggerMacroPendingListSize );
  957. print(" : ");
  958. for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  959. {
  960. printHex( macroTriggerMacroPendingList[ macro ] );
  961. print(" ");
  962. }
  963. // Show pending result macros
  964. print( NL );
  965. info_msg("Pending Result Macros: ");
  966. printInt16( (uint16_t)macroResultMacroPendingListSize );
  967. print(" : ");
  968. for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  969. {
  970. printHex( macroResultMacroPendingList[ macro ] );
  971. print(" ");
  972. }
  973. // Show available trigger macro indices
  974. print( NL );
  975. info_msg("Trigger Macros Range: T0 -> T");
  976. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  977. // Show available result macro indices
  978. print( NL );
  979. info_msg("Result Macros Range: R0 -> R");
  980. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  981. // Show Trigger to Result Macro Links
  982. print( NL );
  983. info_msg("Trigger : Result Macro Pairs");
  984. for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
  985. {
  986. print( NL );
  987. print("\tT");
  988. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  989. print(" : R");
  990. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  991. }
  992. }
  993. void cliFunc_macroProc( char* args )
  994. {
  995. // Toggle macro pause mode
  996. macroPauseMode = macroPauseMode ? 0 : 1;
  997. print( NL );
  998. info_msg("Macro Processing Mode: ");
  999. printInt8( macroPauseMode );
  1000. }
  1001. void macroDebugShowTrigger( unsigned int index )
  1002. {
  1003. // Only proceed if the macro exists
  1004. if ( index >= TriggerMacroNum )
  1005. return;
  1006. // Trigger Macro Show
  1007. TriggerMacro *macro = &TriggerMacroList[ index ];
  1008. print( NL );
  1009. info_msg("Trigger Macro Index: ");
  1010. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1011. print( NL );
  1012. // Read the comboLength for combo in the sequence (sequence of combos)
  1013. unsigned int pos = 0;
  1014. uint8_t comboLength = macro->guide[ pos ];
  1015. // Iterate through and interpret the guide
  1016. while ( comboLength != 0 )
  1017. {
  1018. // Initial position of the combo
  1019. unsigned int comboPos = ++pos;
  1020. // Iterate through the combo
  1021. while ( pos < comboLength * TriggerGuideSize + comboPos )
  1022. {
  1023. // Assign TriggerGuide element (key type, state and scancode)
  1024. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  1025. // Display guide information about trigger key
  1026. printHex( guide->scanCode );
  1027. print("|");
  1028. printHex( guide->type );
  1029. print("|");
  1030. printHex( guide->state );
  1031. // Increment position
  1032. pos += TriggerGuideSize;
  1033. // Only show combo separator if there are combos left in the sequence element
  1034. if ( pos < comboLength * TriggerGuideSize + comboPos )
  1035. print("+");
  1036. }
  1037. // Read the next comboLength
  1038. comboLength = macro->guide[ pos ];
  1039. // Only show sequence separator if there is another combo to process
  1040. if ( comboLength != 0 )
  1041. print(";");
  1042. }
  1043. // Display current position
  1044. print( NL "Position: " );
  1045. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1046. // Display result macro index
  1047. print( NL "Result Macro Index: " );
  1048. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  1049. // Display trigger macro state
  1050. print( NL "Trigger Macro State: " );
  1051. switch ( macro->state )
  1052. {
  1053. case TriggerMacro_Press: print("Press"); break;
  1054. case TriggerMacro_Release: print("Release"); break;
  1055. case TriggerMacro_Waiting: print("Waiting"); break;
  1056. }
  1057. }
  1058. void macroDebugShowResult( unsigned int index )
  1059. {
  1060. // Only proceed if the macro exists
  1061. if ( index >= ResultMacroNum )
  1062. return;
  1063. // Trigger Macro Show
  1064. ResultMacro *macro = &ResultMacroList[ index ];
  1065. print( NL );
  1066. info_msg("Result Macro Index: ");
  1067. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1068. print( NL );
  1069. // Read the comboLength for combo in the sequence (sequence of combos)
  1070. unsigned int pos = 0;
  1071. uint8_t comboLength = macro->guide[ pos++ ];
  1072. // Iterate through and interpret the guide
  1073. while ( comboLength != 0 )
  1074. {
  1075. // Function Counter, used to keep track of the combos processed
  1076. unsigned int funcCount = 0;
  1077. // Iterate through the combo
  1078. while ( funcCount < comboLength )
  1079. {
  1080. // Assign TriggerGuide element (key type, state and scancode)
  1081. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  1082. // Display Function Index
  1083. printHex( guide->index );
  1084. print("|");
  1085. // Display Function Ptr Address
  1086. printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
  1087. print("|");
  1088. // Display/Lookup Capability Name (utilize debug mode of capability)
  1089. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  1090. capability( 0xFF, 0xFF, 0 );
  1091. // Display Argument(s)
  1092. print("(");
  1093. for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  1094. {
  1095. // Arguments are only 8 bit values
  1096. printHex( (&guide->args)[ arg ] );
  1097. // Only show arg separator if there are args left
  1098. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  1099. print(",");
  1100. }
  1101. print(")");
  1102. // Increment position
  1103. pos += ResultGuideSize( guide );
  1104. // Increment function count
  1105. funcCount++;
  1106. // Only show combo separator if there are combos left in the sequence element
  1107. if ( funcCount < comboLength )
  1108. print("+");
  1109. }
  1110. // Read the next comboLength
  1111. comboLength = macro->guide[ pos++ ];
  1112. // Only show sequence separator if there is another combo to process
  1113. if ( comboLength != 0 )
  1114. print(";");
  1115. }
  1116. // Display current position
  1117. print( NL "Position: " );
  1118. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1119. // Display final trigger state/type
  1120. print( NL "Final Trigger State (State/Type): " );
  1121. printHex( macro->state );
  1122. print("/");
  1123. printHex( macro->stateType );
  1124. }
  1125. void cliFunc_macroShow( char* args )
  1126. {
  1127. // Parse codes from arguments
  1128. char* curArgs;
  1129. char* arg1Ptr;
  1130. char* arg2Ptr = args;
  1131. // Process all args
  1132. for ( ;; )
  1133. {
  1134. curArgs = arg2Ptr;
  1135. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1136. // Stop processing args if no more are found
  1137. if ( *arg1Ptr == '\0' )
  1138. break;
  1139. // Ignore invalid codes
  1140. switch ( arg1Ptr[0] )
  1141. {
  1142. // Indexed Trigger Macro
  1143. case 'T':
  1144. macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
  1145. break;
  1146. // Indexed Result Macro
  1147. case 'R':
  1148. macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
  1149. break;
  1150. }
  1151. }
  1152. }
  1153. void cliFunc_macroStep( char* args )
  1154. {
  1155. // Parse number from argument
  1156. // NOTE: Only first argument is used
  1157. char* arg1Ptr;
  1158. char* arg2Ptr;
  1159. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  1160. // Default to 1, if no argument given
  1161. unsigned int count = (unsigned int)numToInt( arg1Ptr );
  1162. if ( count == 0 )
  1163. count = 1;
  1164. // Set the macro step counter, negative int's are cast to uint
  1165. macroStepCounter = count;
  1166. }