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

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