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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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 ] = { 0 };
  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 ] = { 0 };
  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 ] = { 0 };
  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. // Append result macro to pending list, checking for duplicates
  372. // Do nothing if duplicate
  373. inline void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMacro )
  374. {
  375. // Lookup result macro index
  376. var_uint_t resultMacroIndex = triggerMacro->result;
  377. // Iterate through result macro pending list, making sure this macro hasn't been added yet
  378. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  379. {
  380. // If duplicate found, do nothing
  381. if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
  382. return;
  383. }
  384. // No duplicates found, add to pending list
  385. macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
  386. // Lookup scanCode of the last key in the last combo
  387. var_uint_t pos = 0;
  388. for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
  389. {
  390. pos += TriggerGuideSize * comboLength + 1;
  391. comboLength = triggerMacro->guide[ pos ];
  392. }
  393. uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
  394. // Lookup scanCode in buffer list for the current state and stateType
  395. for ( uint8_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
  396. {
  397. if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
  398. {
  399. ResultMacroRecordList[ resultMacroIndex ].state = macroTriggerListBuffer[ keyIndex ].state;
  400. ResultMacroRecordList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
  401. }
  402. }
  403. // Reset the macro position
  404. ResultMacroRecordList[ resultMacroIndex ].pos = 0;
  405. }
  406. // Determine if long ResultMacro (more than 1 seqence element)
  407. inline uint8_t Macro_isLongResultMacro( const ResultMacro *macro )
  408. {
  409. // Check the second sequence combo length
  410. // If non-zero return non-zero (long sequence)
  411. // 0 otherwise (short sequence)
  412. var_uint_t position = 1;
  413. for ( var_uint_t result = 0; result < macro->guide[0]; result++ )
  414. position += ResultGuideSize( (ResultGuide*)&macro->guide[ position ] );
  415. return macro->guide[ position ];
  416. }
  417. // Determine if long TriggerMacro (more than 1 sequence element)
  418. inline uint8_t Macro_isLongTriggerMacro( const TriggerMacro *macro )
  419. {
  420. // Check the second sequence combo length
  421. // If non-zero return non-zero (long sequence)
  422. // 0 otherwise (short sequence)
  423. return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ];
  424. }
  425. // Votes on the given key vs. guide, short macros
  426. inline TriggerMacroVote Macro_evalShortTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  427. {
  428. // Depending on key type
  429. switch ( guide->type )
  430. {
  431. // Normal State Type
  432. case 0x00:
  433. // For short TriggerMacros completely ignore incorrect keys
  434. if ( guide->scanCode == key->scanCode )
  435. {
  436. switch ( key->state )
  437. {
  438. // Correct key, pressed, possible passing
  439. case 0x01:
  440. return TriggerMacroVote_Pass;
  441. // Correct key, held, possible passing or release
  442. case 0x02:
  443. return TriggerMacroVote_PassRelease;
  444. // Correct key, released, possible release
  445. case 0x03:
  446. return TriggerMacroVote_Release;
  447. }
  448. }
  449. return TriggerMacroVote_DoNothing;
  450. // LED State Type
  451. case 0x01:
  452. erro_print("LED State Type - Not implemented...");
  453. break;
  454. // Analog State Type
  455. case 0x02:
  456. erro_print("Analog State Type - Not implemented...");
  457. break;
  458. // Invalid State Type
  459. default:
  460. erro_print("Invalid State Type. This is a bug.");
  461. break;
  462. }
  463. // XXX Shouldn't reach here
  464. return TriggerMacroVote_Invalid;
  465. }
  466. // Votes on the given key vs. guide, long macros
  467. // A long macro is defined as a guide with more than 1 combo
  468. inline TriggerMacroVote Macro_evalLongTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  469. {
  470. // Depending on key type
  471. switch ( guide->type )
  472. {
  473. // Normal State Type
  474. case 0x00:
  475. // Depending on the state of the buffered key, make voting decision
  476. // Incorrect key
  477. if ( guide->scanCode != key->scanCode )
  478. {
  479. switch ( key->state )
  480. {
  481. // Wrong key, pressed, fail
  482. case 0x01:
  483. return TriggerMacroVote_Fail;
  484. // Wrong key, held, do not pass (no effect)
  485. case 0x02:
  486. return TriggerMacroVote_DoNothing;
  487. // Wrong key released, fail out if pos == 0
  488. case 0x03:
  489. return TriggerMacroVote_DoNothing | TriggerMacroVote_DoNothingRelease;
  490. }
  491. }
  492. // Correct key
  493. else
  494. {
  495. switch ( key->state )
  496. {
  497. // Correct key, pressed, possible passing
  498. case 0x01:
  499. return TriggerMacroVote_Pass;
  500. // Correct key, held, possible passing or release
  501. case 0x02:
  502. return TriggerMacroVote_PassRelease;
  503. // Correct key, released, possible release
  504. case 0x03:
  505. return TriggerMacroVote_Release;
  506. }
  507. }
  508. break;
  509. // LED State Type
  510. case 0x01:
  511. erro_print("LED State Type - Not implemented...");
  512. break;
  513. // Analog State Type
  514. case 0x02:
  515. erro_print("Analog State Type - Not implemented...");
  516. break;
  517. // Invalid State Type
  518. default:
  519. erro_print("Invalid State Type. This is a bug.");
  520. break;
  521. }
  522. // XXX Shouldn't reach here
  523. return TriggerMacroVote_Invalid;
  524. }
  525. // Evaluate/Update TriggerMacro
  526. TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
  527. {
  528. // Lookup TriggerMacro
  529. const TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
  530. TriggerMacroRecord *record = &TriggerMacroRecordList[ triggerMacroIndex ];
  531. // Check if macro has finished and should be incremented sequence elements
  532. if ( record->state == TriggerMacro_Release )
  533. {
  534. record->state = TriggerMacro_Waiting;
  535. record->pos = record->pos + macro->guide[ record->pos ] * TriggerGuideSize + 1;
  536. }
  537. // Current Macro position
  538. var_uint_t pos = record->pos;
  539. // Length of the combo being processed
  540. uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
  541. // If no combo items are left, remove the TriggerMacro from the pending list
  542. if ( comboLength == 0 )
  543. {
  544. return TriggerMacroEval_Remove;
  545. }
  546. // Check if this is a long Trigger Macro
  547. uint8_t longMacro = Macro_isLongTriggerMacro( macro );
  548. // Iterate through the items in the combo, voting the on the key state
  549. // If any of the pressed keys do not match, fail the macro
  550. //
  551. // The macro is waiting for input when in the TriggerMacro_Waiting state
  552. // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
  553. // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
  554. // On scan after position increment, change to TriggerMacro_Waiting state
  555. // TODO Add support for system LED states (NumLock, CapsLock, etc.)
  556. // TODO Add support for analog key states
  557. // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
  558. // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
  559. TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
  560. for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
  561. {
  562. // Assign TriggerGuide element (key type, state and scancode)
  563. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
  564. TriggerMacroVote vote = TriggerMacroVote_Invalid;
  565. // Iterate through the key buffer, comparing to each key in the combo
  566. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  567. {
  568. // Lookup key information
  569. TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
  570. // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
  571. // Also mask all of the non-passing votes
  572. vote |= longMacro
  573. ? Macro_evalLongTriggerMacroVote( keyInfo, guide )
  574. : Macro_evalShortTriggerMacroVote( keyInfo, guide );
  575. if ( vote >= TriggerMacroVote_Pass )
  576. {
  577. vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
  578. break;
  579. }
  580. }
  581. // If no pass vote was found after scanning all of the keys
  582. // Fail the combo, if this is a short macro (long macros already will have a fail vote)
  583. if ( !longMacro && vote < TriggerMacroVote_Pass )
  584. vote |= TriggerMacroVote_Fail;
  585. // After voting, append to overall vote
  586. overallVote |= vote;
  587. }
  588. // If no pass vote was found after scanning the entire combo
  589. // And this is the first position in the combo, just remove it (nothing important happened)
  590. if ( longMacro && overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
  591. overallVote |= TriggerMacroVote_Fail;
  592. // Decide new state of macro after voting
  593. // Fail macro, remove from pending list
  594. if ( overallVote & TriggerMacroVote_Fail )
  595. {
  596. return TriggerMacroEval_Remove;
  597. }
  598. // Do nothing, incorrect key is being held or released
  599. else if ( overallVote & TriggerMacroVote_DoNothing && longMacro )
  600. {
  601. // Just doing nothing :)
  602. }
  603. // If ready for transition and in Press state, set to Waiting and increment combo position
  604. // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
  605. else if ( overallVote & TriggerMacroVote_Release && record->state == TriggerMacro_Press )
  606. {
  607. record->state = TriggerMacro_Release;
  608. // If this is the last combo in the sequence, remove from the pending list
  609. if ( macro->guide[ record->pos + macro->guide[ record->pos ] * TriggerGuideSize + 1 ] == 0 )
  610. return TriggerMacroEval_DoResultAndRemove;
  611. }
  612. // If passing and in Waiting state, set macro state to Press
  613. else if ( overallVote & TriggerMacroVote_Pass
  614. && ( record->state == TriggerMacro_Waiting || record->state == TriggerMacro_Press ) )
  615. {
  616. record->state = TriggerMacro_Press;
  617. // If in press state, and this is the final combo, send request for ResultMacro
  618. // Check to see if the result macro only has a single element
  619. // If this result macro has more than 1 key, only send once
  620. // TODO Add option to have long macro repeat rate
  621. if ( macro->guide[ pos + comboLength + 1 ] == 0 )
  622. {
  623. // Long result macro (more than 1 combo)
  624. if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
  625. {
  626. // Only ever trigger result once, on press
  627. if ( overallVote == TriggerMacroVote_Pass )
  628. {
  629. return TriggerMacroEval_DoResultAndRemove;
  630. }
  631. }
  632. // Short result macro
  633. else
  634. {
  635. // Only trigger result once, on press, if long trigger (more than 1 combo)
  636. if ( Macro_isLongTriggerMacro( macro ) )
  637. {
  638. return TriggerMacroEval_DoResultAndRemove;
  639. }
  640. // Otherwise, trigger result continuously
  641. else
  642. {
  643. return TriggerMacroEval_DoResult;
  644. }
  645. }
  646. }
  647. }
  648. // Otherwise, just remove the macro on key release
  649. // One more result has to be called to indicate to the ResultMacro that the key transitioned to the release state
  650. else if ( overallVote & TriggerMacroVote_Release )
  651. {
  652. return TriggerMacroEval_DoResultAndRemove;
  653. }
  654. // If this is a short macro, just remove it
  655. // The state can be rebuilt on the next iteration
  656. if ( !longMacro )
  657. return TriggerMacroEval_Remove;
  658. return TriggerMacroEval_DoNothing;
  659. }
  660. // Evaluate/Update ResultMacro
  661. inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex )
  662. {
  663. // Lookup ResultMacro
  664. const ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
  665. ResultMacroRecord *record = &ResultMacroRecordList[ resultMacroIndex ];
  666. // Current Macro position
  667. var_uint_t pos = record->pos;
  668. // Length of combo being processed
  669. uint8_t comboLength = macro->guide[ pos ];
  670. // Function Counter, used to keep track of the combo items processed
  671. var_uint_t funcCount = 0;
  672. // Combo Item Position within the guide
  673. var_uint_t comboItem = pos + 1;
  674. // Iterate through the Result Combo
  675. while ( funcCount < comboLength )
  676. {
  677. // Assign TriggerGuide element (key type, state and scancode)
  678. ResultGuide *guide = (ResultGuide*)(&macro->guide[ comboItem ]);
  679. // Do lookup on capability function
  680. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  681. // Call capability
  682. capability( record->state, record->stateType, &guide->args );
  683. // Increment counters
  684. funcCount++;
  685. comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
  686. }
  687. // Move to next item in the sequence
  688. record->pos = comboItem;
  689. // If the ResultMacro is finished, remove
  690. if ( macro->guide[ comboItem ] == 0 )
  691. {
  692. record->pos = 0;
  693. return ResultMacroEval_Remove;
  694. }
  695. // Otherwise leave the macro in the list
  696. return ResultMacroEval_DoNothing;
  697. }
  698. // Update pending trigger list
  699. inline void Macro_updateTriggerMacroPendingList()
  700. {
  701. // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
  702. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  703. {
  704. // TODO LED States
  705. // TODO Analog Switches
  706. // Only add TriggerMacro to pending list if key was pressed (not held, released or off)
  707. if ( macroTriggerListBuffer[ key ].state == 0x00 && macroTriggerListBuffer[ key ].state != 0x01 )
  708. continue;
  709. // TODO Analog
  710. // If this is a release case, indicate to layer lookup for possible latch expiry
  711. uint8_t latch_expire = macroTriggerListBuffer[ key ].state == 0x03;
  712. // Lookup Trigger List
  713. nat_ptr_t *triggerList = Macro_layerLookup( &macroTriggerListBuffer[ key ], latch_expire );
  714. // Number of Triggers in list
  715. nat_ptr_t triggerListSize = triggerList[0];
  716. // Iterate over triggerList to see if any TriggerMacros need to be added
  717. // First item is the number of items in the TriggerList
  718. for ( var_uint_t macro = 1; macro < triggerListSize + 1; macro++ )
  719. {
  720. // Lookup trigger macro index
  721. var_uint_t triggerMacroIndex = triggerList[ macro ];
  722. // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
  723. // triggerList needs to be added
  724. var_uint_t pending = 0;
  725. for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
  726. {
  727. // Stop scanning if the trigger macro index is found in the pending list
  728. if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
  729. break;
  730. }
  731. // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
  732. // Add it to the list
  733. if ( pending == macroTriggerMacroPendingListSize )
  734. {
  735. macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
  736. // Reset macro position
  737. TriggerMacroRecordList[ triggerMacroIndex ].pos = 0;
  738. TriggerMacroRecordList[ triggerMacroIndex ].state = TriggerMacro_Waiting;
  739. }
  740. }
  741. }
  742. }
  743. // Macro Procesing Loop
  744. // Called once per USB buffer send
  745. inline void Macro_process()
  746. {
  747. // Only do one round of macro processing between Output Module timer sends
  748. if ( USBKeys_Sent != 0 )
  749. return;
  750. // If the pause flag is set, only process if the step counter is non-zero
  751. if ( macroPauseMode )
  752. {
  753. if ( macroStepCounter == 0 )
  754. return;
  755. // Proceed, decrementing the step counter
  756. macroStepCounter--;
  757. dbug_print("Macro Step");
  758. }
  759. // Update pending trigger list, before processing TriggerMacros
  760. Macro_updateTriggerMacroPendingList();
  761. // Tail pointer for macroTriggerMacroPendingList
  762. // Macros must be explicitly re-added
  763. var_uint_t macroTriggerMacroPendingListTail = 0;
  764. // Iterate through the pending TriggerMacros, processing each of them
  765. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  766. {
  767. switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
  768. {
  769. // Trigger Result Macro (purposely falling through)
  770. case TriggerMacroEval_DoResult:
  771. // Append ResultMacro to PendingList
  772. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  773. default:
  774. macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
  775. break;
  776. // Trigger Result Macro and Remove (purposely falling through)
  777. case TriggerMacroEval_DoResultAndRemove:
  778. // Append ResultMacro to PendingList
  779. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  780. // Remove Macro from Pending List, nothing to do, removing by default
  781. case TriggerMacroEval_Remove:
  782. break;
  783. }
  784. }
  785. // Update the macroTriggerMacroPendingListSize with the tail pointer
  786. macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
  787. // Tail pointer for macroResultMacroPendingList
  788. // Macros must be explicitly re-added
  789. var_uint_t macroResultMacroPendingListTail = 0;
  790. // Iterate through the pending ResultMacros, processing each of them
  791. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  792. {
  793. switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
  794. {
  795. // Re-add macros to pending list
  796. case ResultMacroEval_DoNothing:
  797. default:
  798. macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
  799. break;
  800. // Remove Macro from Pending List, nothing to do, removing by default
  801. case ResultMacroEval_Remove:
  802. break;
  803. }
  804. }
  805. // Update the macroResultMacroPendingListSize with the tail pointer
  806. macroResultMacroPendingListSize = macroResultMacroPendingListTail;
  807. // Signal buffer that we've used it
  808. Scan_finishedWithMacro( macroTriggerListBufferSize );
  809. // Reset TriggerList buffer
  810. macroTriggerListBufferSize = 0;
  811. // If Macro debug mode is set, clear the USB Buffer
  812. if ( macroDebugMode )
  813. {
  814. USBKeys_Modifiers = 0;
  815. USBKeys_Sent = 0;
  816. }
  817. }
  818. inline void Macro_setup()
  819. {
  820. // Register Macro CLI dictionary
  821. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  822. // Disable Macro debug mode
  823. macroDebugMode = 0;
  824. // Disable Macro pause flag
  825. macroPauseMode = 0;
  826. // Set Macro step counter to zero
  827. macroStepCounter = 0;
  828. // Make sure macro trigger buffer is empty
  829. macroTriggerListBufferSize = 0;
  830. // Initialize TriggerMacro states
  831. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  832. {
  833. TriggerMacroRecordList[ macro ].pos = 0;
  834. TriggerMacroRecordList[ macro ].state = TriggerMacro_Waiting;
  835. }
  836. // Initialize ResultMacro states
  837. for ( var_uint_t macro = 0; macro < ResultMacroNum; macro++ )
  838. {
  839. ResultMacroRecordList[ macro ].pos = 0;
  840. ResultMacroRecordList[ macro ].state = 0;
  841. ResultMacroRecordList[ macro ].stateType = 0;
  842. }
  843. }
  844. // ----- CLI Command Functions -----
  845. void cliFunc_capList( char* args )
  846. {
  847. print( NL );
  848. info_msg("Capabilities List ");
  849. printHex( CapabilitiesNum );
  850. // Iterate through all of the capabilities and display them
  851. for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
  852. {
  853. print( NL "\t" );
  854. printHex( cap );
  855. print(" - ");
  856. // Display/Lookup Capability Name (utilize debug mode of capability)
  857. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  858. capability( 0xFF, 0xFF, 0 );
  859. }
  860. }
  861. void cliFunc_capSelect( char* args )
  862. {
  863. // Parse code from argument
  864. char* curArgs;
  865. char* arg1Ptr;
  866. char* arg2Ptr = args;
  867. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  868. var_uint_t totalArgs = 2; // Always at least two args
  869. var_uint_t cap = 0;
  870. // Arguments used for keyboard capability function
  871. var_uint_t argSetCount = 0;
  872. uint8_t *argSet = (uint8_t*)args;
  873. // Process all args
  874. for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
  875. {
  876. curArgs = arg2Ptr;
  877. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  878. // Stop processing args if no more are found
  879. // Extra arguments are ignored
  880. if ( *arg1Ptr == '\0' )
  881. break;
  882. // For the first argument, choose the capability
  883. if ( c == 0 ) switch ( arg1Ptr[0] )
  884. {
  885. // Keyboard Capability
  886. case 'K':
  887. // Determine capability index
  888. cap = numToInt( &arg1Ptr[1] );
  889. // Lookup the number of args
  890. totalArgs += CapabilitiesList[ cap ].argCount;
  891. continue;
  892. }
  893. // Because allocating memory isn't doable, and the argument count is arbitrary
  894. // The argument pointer is repurposed as the argument list (much smaller anyways)
  895. argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
  896. // Once all the arguments are prepared, call the keyboard capability function
  897. if ( argSetCount == totalArgs )
  898. {
  899. // Indicate that the capability was called
  900. print( NL );
  901. info_msg("K");
  902. printInt8( cap );
  903. print(" - ");
  904. printHex( argSet[0] );
  905. print(" - ");
  906. printHex( argSet[1] );
  907. print(" - ");
  908. printHex( argSet[2] );
  909. print( "..." NL );
  910. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  911. capability( argSet[0], argSet[1], &argSet[2] );
  912. }
  913. }
  914. }
  915. void cliFunc_keyHold( char* args )
  916. {
  917. // Parse codes from arguments
  918. char* curArgs;
  919. char* arg1Ptr;
  920. char* arg2Ptr = args;
  921. // Process all args
  922. for ( ;; )
  923. {
  924. curArgs = arg2Ptr;
  925. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  926. // Stop processing args if no more are found
  927. if ( *arg1Ptr == '\0' )
  928. break;
  929. // Ignore non-Scancode numbers
  930. switch ( arg1Ptr[0] )
  931. {
  932. // Scancode
  933. case 'S':
  934. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
  935. break;
  936. }
  937. }
  938. }
  939. void cliFunc_keyPress( char* args )
  940. {
  941. // Parse codes from arguments
  942. char* curArgs;
  943. char* arg1Ptr;
  944. char* arg2Ptr = args;
  945. // Process all args
  946. for ( ;; )
  947. {
  948. curArgs = arg2Ptr;
  949. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  950. // Stop processing args if no more are found
  951. if ( *arg1Ptr == '\0' )
  952. break;
  953. // Ignore non-Scancode numbers
  954. switch ( arg1Ptr[0] )
  955. {
  956. // Scancode
  957. case 'S':
  958. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  959. break;
  960. }
  961. }
  962. }
  963. void cliFunc_keyRelease( char* args )
  964. {
  965. // Parse codes from arguments
  966. char* curArgs;
  967. char* arg1Ptr;
  968. char* arg2Ptr = args;
  969. // Process all args
  970. for ( ;; )
  971. {
  972. curArgs = arg2Ptr;
  973. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  974. // Stop processing args if no more are found
  975. if ( *arg1Ptr == '\0' )
  976. break;
  977. // Ignore non-Scancode numbers
  978. switch ( arg1Ptr[0] )
  979. {
  980. // Scancode
  981. case 'S':
  982. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  983. break;
  984. }
  985. }
  986. }
  987. void cliFunc_layerList( char* args )
  988. {
  989. print( NL );
  990. info_msg("Layer List");
  991. // Iterate through all of the layers and display them
  992. for ( uint16_t layer = 0; layer < LayerNum; layer++ )
  993. {
  994. print( NL "\t" );
  995. printHex( layer );
  996. print(" - ");
  997. // Display layer name
  998. dPrint( (char*)LayerIndex[ layer ].name );
  999. // Default map
  1000. if ( layer == 0 )
  1001. print(" \033[1m(default)\033[0m");
  1002. // Layer State
  1003. print( NL "\t\t Layer State: " );
  1004. printHex( LayerState[ layer ] );
  1005. // First -> Last Indices
  1006. print(" First -> Last Indices: ");
  1007. printHex( LayerIndex[ layer ].first );
  1008. print(" -> ");
  1009. printHex( LayerIndex[ layer ].last );
  1010. }
  1011. }
  1012. void cliFunc_layerState( char* args )
  1013. {
  1014. // Parse codes from arguments
  1015. char* curArgs;
  1016. char* arg1Ptr;
  1017. char* arg2Ptr = args;
  1018. uint8_t arg1 = 0;
  1019. uint8_t arg2 = 0;
  1020. // Process first two args
  1021. for ( uint8_t c = 0; c < 2; c++ )
  1022. {
  1023. curArgs = arg2Ptr;
  1024. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1025. // Stop processing args if no more are found
  1026. if ( *arg1Ptr == '\0' )
  1027. break;
  1028. switch ( c )
  1029. {
  1030. // First argument (e.g. L1)
  1031. case 0:
  1032. if ( arg1Ptr[0] != 'L' )
  1033. return;
  1034. arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
  1035. break;
  1036. // Second argument (e.g. 4)
  1037. case 1:
  1038. arg2 = (uint8_t)numToInt( arg1Ptr );
  1039. // Display operation (to indicate that it worked)
  1040. print( NL );
  1041. info_msg("Setting Layer L");
  1042. printInt8( arg1 );
  1043. print(" to - ");
  1044. printHex( arg2 );
  1045. // Set the layer state
  1046. LayerState[ arg1 ] = arg2;
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. void cliFunc_macroDebug( char* args )
  1052. {
  1053. // Toggle macro debug mode
  1054. macroDebugMode = macroDebugMode ? 0 : 1;
  1055. print( NL );
  1056. info_msg("Macro Debug Mode: ");
  1057. printInt8( macroDebugMode );
  1058. }
  1059. void cliFunc_macroList( char* args )
  1060. {
  1061. // Show pending key events
  1062. print( NL );
  1063. info_msg("Pending Key Events: ");
  1064. printInt16( (uint16_t)macroTriggerListBufferSize );
  1065. print(" : ");
  1066. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  1067. {
  1068. printHex( macroTriggerListBuffer[ key ].scanCode );
  1069. print(" ");
  1070. }
  1071. // Show pending trigger macros
  1072. print( NL );
  1073. info_msg("Pending Trigger Macros: ");
  1074. printInt16( (uint16_t)macroTriggerMacroPendingListSize );
  1075. print(" : ");
  1076. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  1077. {
  1078. printHex( macroTriggerMacroPendingList[ macro ] );
  1079. print(" ");
  1080. }
  1081. // Show pending result macros
  1082. print( NL );
  1083. info_msg("Pending Result Macros: ");
  1084. printInt16( (uint16_t)macroResultMacroPendingListSize );
  1085. print(" : ");
  1086. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  1087. {
  1088. printHex( macroResultMacroPendingList[ macro ] );
  1089. print(" ");
  1090. }
  1091. // Show available trigger macro indices
  1092. print( NL );
  1093. info_msg("Trigger Macros Range: T0 -> T");
  1094. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  1095. // Show available result macro indices
  1096. print( NL );
  1097. info_msg("Result Macros Range: R0 -> R");
  1098. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  1099. // Show Trigger to Result Macro Links
  1100. print( NL );
  1101. info_msg("Trigger : Result Macro Pairs");
  1102. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  1103. {
  1104. print( NL );
  1105. print("\tT");
  1106. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  1107. print(" : R");
  1108. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  1109. }
  1110. }
  1111. void cliFunc_macroProc( char* args )
  1112. {
  1113. // Toggle macro pause mode
  1114. macroPauseMode = macroPauseMode ? 0 : 1;
  1115. print( NL );
  1116. info_msg("Macro Processing Mode: ");
  1117. printInt8( macroPauseMode );
  1118. }
  1119. void macroDebugShowTrigger( var_uint_t index )
  1120. {
  1121. // Only proceed if the macro exists
  1122. if ( index >= TriggerMacroNum )
  1123. return;
  1124. // Trigger Macro Show
  1125. const TriggerMacro *macro = &TriggerMacroList[ index ];
  1126. TriggerMacroRecord *record = &TriggerMacroRecordList[ index ];
  1127. print( NL );
  1128. info_msg("Trigger Macro Index: ");
  1129. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1130. print( NL );
  1131. // Read the comboLength for combo in the sequence (sequence of combos)
  1132. var_uint_t pos = 0;
  1133. uint8_t comboLength = macro->guide[ pos ];
  1134. // Iterate through and interpret the guide
  1135. while ( comboLength != 0 )
  1136. {
  1137. // Initial position of the combo
  1138. var_uint_t comboPos = ++pos;
  1139. // Iterate through the combo
  1140. while ( pos < comboLength * TriggerGuideSize + comboPos )
  1141. {
  1142. // Assign TriggerGuide element (key type, state and scancode)
  1143. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  1144. // Display guide information about trigger key
  1145. printHex( guide->scanCode );
  1146. print("|");
  1147. printHex( guide->type );
  1148. print("|");
  1149. printHex( guide->state );
  1150. // Increment position
  1151. pos += TriggerGuideSize;
  1152. // Only show combo separator if there are combos left in the sequence element
  1153. if ( pos < comboLength * TriggerGuideSize + comboPos )
  1154. print("+");
  1155. }
  1156. // Read the next comboLength
  1157. comboLength = macro->guide[ pos ];
  1158. // Only show sequence separator if there is another combo to process
  1159. if ( comboLength != 0 )
  1160. print(";");
  1161. }
  1162. // Display current position
  1163. print( NL "Position: " );
  1164. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1165. // Display result macro index
  1166. print( NL "Result Macro Index: " );
  1167. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  1168. // Display trigger macro state
  1169. print( NL "Trigger Macro State: " );
  1170. switch ( record->state )
  1171. {
  1172. case TriggerMacro_Press: print("Press"); break;
  1173. case TriggerMacro_Release: print("Release"); break;
  1174. case TriggerMacro_Waiting: print("Waiting"); break;
  1175. }
  1176. }
  1177. void macroDebugShowResult( var_uint_t index )
  1178. {
  1179. // Only proceed if the macro exists
  1180. if ( index >= ResultMacroNum )
  1181. return;
  1182. // Trigger Macro Show
  1183. const ResultMacro *macro = &ResultMacroList[ index ];
  1184. ResultMacroRecord *record = &ResultMacroRecordList[ index ];
  1185. print( NL );
  1186. info_msg("Result Macro Index: ");
  1187. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1188. print( NL );
  1189. // Read the comboLength for combo in the sequence (sequence of combos)
  1190. var_uint_t pos = 0;
  1191. uint8_t comboLength = macro->guide[ pos++ ];
  1192. // Iterate through and interpret the guide
  1193. while ( comboLength != 0 )
  1194. {
  1195. // Function Counter, used to keep track of the combos processed
  1196. var_uint_t funcCount = 0;
  1197. // Iterate through the combo
  1198. while ( funcCount < comboLength )
  1199. {
  1200. // Assign TriggerGuide element (key type, state and scancode)
  1201. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  1202. // Display Function Index
  1203. printHex( guide->index );
  1204. print("|");
  1205. // Display Function Ptr Address
  1206. printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
  1207. print("|");
  1208. // Display/Lookup Capability Name (utilize debug mode of capability)
  1209. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  1210. capability( 0xFF, 0xFF, 0 );
  1211. // Display Argument(s)
  1212. print("(");
  1213. for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  1214. {
  1215. // Arguments are only 8 bit values
  1216. printHex( (&guide->args)[ arg ] );
  1217. // Only show arg separator if there are args left
  1218. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  1219. print(",");
  1220. }
  1221. print(")");
  1222. // Increment position
  1223. pos += ResultGuideSize( guide );
  1224. // Increment function count
  1225. funcCount++;
  1226. // Only show combo separator if there are combos left in the sequence element
  1227. if ( funcCount < comboLength )
  1228. print("+");
  1229. }
  1230. // Read the next comboLength
  1231. comboLength = macro->guide[ pos++ ];
  1232. // Only show sequence separator if there is another combo to process
  1233. if ( comboLength != 0 )
  1234. print(";");
  1235. }
  1236. // Display current position
  1237. print( NL "Position: " );
  1238. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1239. // Display final trigger state/type
  1240. print( NL "Final Trigger State (State/Type): " );
  1241. printHex( record->state );
  1242. print("/");
  1243. printHex( record->stateType );
  1244. }
  1245. void cliFunc_macroShow( char* args )
  1246. {
  1247. // Parse codes from arguments
  1248. char* curArgs;
  1249. char* arg1Ptr;
  1250. char* arg2Ptr = args;
  1251. // Process all args
  1252. for ( ;; )
  1253. {
  1254. curArgs = arg2Ptr;
  1255. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1256. // Stop processing args if no more are found
  1257. if ( *arg1Ptr == '\0' )
  1258. break;
  1259. // Ignore invalid codes
  1260. switch ( arg1Ptr[0] )
  1261. {
  1262. // Indexed Trigger Macro
  1263. case 'T':
  1264. macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
  1265. break;
  1266. // Indexed Result Macro
  1267. case 'R':
  1268. macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
  1269. break;
  1270. }
  1271. }
  1272. }
  1273. void cliFunc_macroStep( char* args )
  1274. {
  1275. // Parse number from argument
  1276. // NOTE: Only first argument is used
  1277. char* arg1Ptr;
  1278. char* arg2Ptr;
  1279. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  1280. // Default to 1, if no argument given
  1281. var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
  1282. if ( count == 0 )
  1283. count = 1;
  1284. // Set the macro step counter, negative int's are cast to uint
  1285. macroStepCounter = count;
  1286. }