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

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