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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. #include <output_com.h>
  25. // Keymaps
  26. #include "usb_hid.h"
  27. #include <defaultMap.h>
  28. #include "generatedKeymap.h" // TODO Use actual generated version
  29. // Local Includes
  30. #include "macro.h"
  31. // ----- Function Declarations -----
  32. void cliFunc_capList ( char* args );
  33. void cliFunc_capSelect ( char* args );
  34. void cliFunc_keyPress ( char* args );
  35. void cliFunc_keyRelease( char* args );
  36. void cliFunc_layerList ( char* args );
  37. void cliFunc_layerState( char* args );
  38. void cliFunc_macroDebug( char* args );
  39. void cliFunc_macroList ( char* args );
  40. void cliFunc_macroProc ( char* args );
  41. void cliFunc_macroShow ( char* args );
  42. void cliFunc_macroStep ( char* args );
  43. // ----- Enums -----
  44. // Bit positions are important, passes (correct key) always trump incorrect key votes
  45. typedef enum TriggerMacroVote {
  46. TriggerMacroVote_Release = 0x8, // Correct key
  47. TriggerMacroVote_PassRelease = 0xC, // Correct key (both pass and release)
  48. TriggerMacroVote_Pass = 0x4, // Correct 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. char* macroCLIDictName = "Macro Module Commands";
  66. 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. { "keyPress", "Send key-presses to the macro module. Held until released. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
  70. { "keyRelease", "Release a key-press from the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
  71. { "layerList", "List available layers.", cliFunc_layerList },
  72. { "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 },
  73. { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
  74. { "macroList", "List the defined trigger and result macros.", cliFunc_macroList },
  75. { "macroProc", "Pause/Resume macro processing.", cliFunc_macroProc },
  76. { "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 },
  77. { "macroStep", "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
  78. { 0, 0, 0 } // Null entry for dictionary end
  79. };
  80. // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
  81. uint8_t macroDebugMode = 0;
  82. // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
  83. uint8_t macroPauseMode = 0;
  84. // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
  85. unsigned int macroStepCounter = 0;
  86. // Key Trigger List Buffer
  87. TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
  88. uint8_t macroTriggerListBufferSize = 0;
  89. // Pending Trigger Macro Index List
  90. // * Any trigger macros that need processing from a previous macro processing loop
  91. // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
  92. // Possibly could be calculated by the KLL compiler
  93. // XXX It may be possible to calculate the worst case using the KLL compiler
  94. unsigned int macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
  95. unsigned int macroTriggerMacroPendingListSize = 0;
  96. // Layer Index Stack
  97. // * When modifying layer state and the state is non-0x0, the stack must be adjusted
  98. unsigned int macroLayerIndexStack[ LayerNum ] = { 0 };
  99. unsigned int macroLayerIndexStackSize = 0;
  100. // Pending Result Macro Index List
  101. // * Any result macro that needs processing from a previous macro processing loop
  102. unsigned int macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
  103. unsigned int macroResultMacroPendingListSize = 0;
  104. // ----- Functions -----
  105. // Looks up the trigger list for the given scan code (from the active layer)
  106. // NOTE: Calling function must handle the NULL pointer case
  107. unsigned int *Macro_layerLookup( uint8_t scanCode )
  108. {
  109. // If no trigger macro is defined at the given layer, fallthrough to the next layer
  110. for ( unsigned int layer = 0; layer < macroLayerIndexStackSize; layer++ )
  111. {
  112. // Lookup layer
  113. unsigned int **map = LayerIndex[ macroLayerIndexStack[ layer ] ].triggerMap;
  114. // Determine if layer has key defined
  115. if ( map != 0 && *map[ scanCode ] != 0 )
  116. return map[ scanCode ];
  117. }
  118. // Do lookup on default layer
  119. unsigned int **map = LayerIndex[0].triggerMap;
  120. // Determine if layer has key defined
  121. if ( map == 0 && *map[ scanCode ] == 0 )
  122. {
  123. erro_msg("Scan Code has no defined Trigger Macro: ");
  124. printHex( scanCode );
  125. return 0;
  126. }
  127. // Return lookup result
  128. return map[ scanCode ];
  129. }
  130. // Update the scancode key state
  131. // States:
  132. // * 0x00 - Off
  133. // * 0x01 - Pressed
  134. // * 0x02 - Held
  135. // * 0x03 - Released
  136. // * 0x04 - Unpressed (this is currently ignored)
  137. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  138. {
  139. // Only add to macro trigger list if one of three states
  140. switch ( state )
  141. {
  142. case 0x01: // Pressed
  143. case 0x02: // Held
  144. case 0x03: // Released
  145. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  146. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  147. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
  148. macroTriggerListBufferSize++;
  149. break;
  150. }
  151. }
  152. // Update the scancode analog state
  153. // States:
  154. // * 0x00 - Off
  155. // * 0x01 - Released
  156. // * 0x02-0xFF - Analog value (low to high)
  157. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  158. {
  159. // Only add to macro trigger list if non-off
  160. if ( state != 0x00 )
  161. {
  162. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  163. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  164. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
  165. macroTriggerListBufferSize++;
  166. }
  167. }
  168. // Update led state
  169. // States:
  170. // * 0x00 - Off
  171. // * 0x01 - On
  172. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  173. {
  174. // Only add to macro trigger list if non-off
  175. if ( state != 0x00 )
  176. {
  177. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
  178. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  179. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
  180. macroTriggerListBufferSize++;
  181. }
  182. }
  183. // Append result macro to pending list, checking for duplicates
  184. // Do nothing if duplicate
  185. inline void Macro_appendResultMacroToPendingList( unsigned int resultMacroIndex )
  186. {
  187. // Iterate through result macro pending list, making sure this macro hasn't been added yet
  188. for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  189. {
  190. // If duplicate found, do nothing
  191. if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
  192. return;
  193. }
  194. // No duplicates found, add to pending list
  195. macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
  196. }
  197. // Determine if long ResultMacro (more than 1 seqence element)
  198. inline uint8_t Macro_isLongResultMacro( ResultMacro *macro )
  199. {
  200. // Check the second sequence combo length
  201. // If non-zero return 1 (long sequence)
  202. // 0 otherwise (short sequence)
  203. return macro->guide[ macro->guide[0] * ResultGuideSize( (ResultGuide*)macro->guide ) ] > 0 ? 1 : 0;
  204. }
  205. // Votes on the given key vs. guide
  206. inline TriggerMacroVote Macro_evalTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  207. {
  208. // Depending on key type
  209. switch ( guide->type )
  210. {
  211. // Normal State Type
  212. case 0x00:
  213. // Depending on the state of the buffered key, make voting decision
  214. // Incorrect key
  215. if ( guide->scanCode != key->scanCode )
  216. {
  217. switch ( key->state )
  218. {
  219. // Wrong key, pressed, fail
  220. case 0x01:
  221. return TriggerMacroVote_Fail;
  222. // Wrong key, held or released, do not pass (no effect)
  223. case 0x02:
  224. case 0x03:
  225. return TriggerMacroVote_DoNothing;
  226. }
  227. }
  228. // Correct key
  229. else
  230. {
  231. switch ( key->state )
  232. {
  233. // Correct key, pressed, possible passing
  234. case 0x01:
  235. return TriggerMacroVote_Pass;
  236. // Correct key, held, possible passing or release
  237. case 0x02:
  238. return TriggerMacroVote_PassRelease;
  239. // Correct key, released, possible release
  240. case 0x03:
  241. return TriggerMacroVote_Release;
  242. }
  243. }
  244. break;
  245. // LED State Type
  246. case 0x01:
  247. erro_print("LED State Type - Not implemented...");
  248. break;
  249. // Analog State Type
  250. case 0x02:
  251. erro_print("Analog State Type - Not implemented...");
  252. break;
  253. // Invalid State Type
  254. default:
  255. erro_print("Invalid State Type. This is a bug.");
  256. break;
  257. }
  258. // XXX Shouldn't reach here
  259. return TriggerMacroVote_Invalid;
  260. }
  261. // Evaluate/Update TriggerMacro
  262. inline TriggerMacroEval Macro_evalTriggerMacro( unsigned int triggerMacroIndex )
  263. {
  264. // Lookup TriggerMacro
  265. TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
  266. // Check if macro has finished and should be incremented sequence elements
  267. if ( macro->state == TriggerMacro_Release )
  268. {
  269. macro->state = TriggerMacro_Waiting;
  270. macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize;
  271. }
  272. // Current Macro position
  273. unsigned int pos = macro->pos;
  274. // Length of the combo being processed
  275. uint8_t comboLength = macro->guide[ pos ];
  276. // If no combo items are left, remove the TriggerMacro from the pending list
  277. if ( comboLength == 0 )
  278. {
  279. return TriggerMacroEval_Remove;
  280. }
  281. // Iterate through the key buffer, comparing to each key in the combo
  282. // If any of the pressed keys do not match, fail the macro
  283. //
  284. // The macro is waiting for input when in the TriggerMacro_Waiting state
  285. // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
  286. // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
  287. // On scan after position increment, change to TriggerMacro_Waiting state
  288. // TODO Add support for system LED states (NumLock, CapsLock, etc.)
  289. // TODO Add support for analog key states
  290. // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
  291. // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
  292. TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
  293. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  294. {
  295. // Lookup key information
  296. TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
  297. // Iterate through the items in the combo, voting the on the key state
  298. TriggerMacroVote vote = TriggerMacroVote_Invalid;
  299. for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
  300. {
  301. // Assign TriggerGuide element (key type, state and scancode)
  302. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
  303. // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
  304. // Also mask all of the non-passing votes
  305. vote |= Macro_evalTriggerMacroVote( keyInfo, guide );
  306. if ( vote >= TriggerMacroVote_Pass )
  307. {
  308. vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
  309. break;
  310. }
  311. }
  312. // After voting, append to overall vote
  313. overallVote |= vote;
  314. }
  315. // Decide new state of macro after voting
  316. // Fail macro, remove from pending list
  317. if ( overallVote & TriggerMacroVote_Fail )
  318. {
  319. return TriggerMacroEval_Remove;
  320. }
  321. // Do nothing, incorrect key is being held or released
  322. else if ( overallVote & TriggerMacroVote_DoNothing )
  323. {
  324. // Just doing nothing :)
  325. }
  326. // If passing and in Waiting state, set macro state to Press
  327. else if ( overallVote & TriggerMacroVote_Pass && macro->state == TriggerMacro_Waiting )
  328. {
  329. macro->state = TriggerMacro_Press;
  330. // If in press state, and this is the final combo, send request for ResultMacro
  331. // Check to see if the result macro only has a single element
  332. // If this result macro has more than 1 key, only send once
  333. // TODO Add option to have macro repeat rate
  334. if ( macro->guide[ pos + comboLength ] == 0 )
  335. {
  336. // Long Macro, only send once (more than 1 sequence item)
  337. // Short Macro (only 1 sequence item)
  338. return Macro_isLongResultMacro( &ResultMacroList[ macro->result ] )
  339. ? TriggerMacroEval_DoResult
  340. : TriggerMacroEval_DoResultAndRemove;
  341. }
  342. }
  343. // If ready for transition and in Press state, set to Waiting and increment combo position
  344. // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
  345. else if ( overallVote & TriggerMacroVote_Release && macro->state == TriggerMacro_Press )
  346. {
  347. macro->state = TriggerMacro_Release;
  348. }
  349. return TriggerMacroEval_DoNothing;
  350. }
  351. // Evaluate/Update ResultMacro
  352. inline ResultMacroEval Macro_evalResultMacro( unsigned int resultMacroIndex )
  353. {
  354. // Lookup ResultMacro
  355. ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
  356. // Current Macro position
  357. unsigned int pos = macro->pos;
  358. // Length of combo being processed
  359. uint8_t comboLength = macro->guide[ pos ];
  360. // If no combo items are left, remove the ResultMacro from the pending list
  361. if ( comboLength == 0 )
  362. {
  363. return ResultMacroEval_Remove;
  364. }
  365. // Function Counter, used to keep track of the combo items processed
  366. unsigned int funcCount = 0;
  367. // Combo Item Position within the guide
  368. unsigned int comboItem = pos + 1;
  369. // Iterate through the Result Combo
  370. while ( funcCount < comboLength )
  371. {
  372. // Assign TriggerGuide element (key type, state and scancode)
  373. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  374. // Do lookup on capability function
  375. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  376. // Call capability
  377. capability( macro->state, macro->stateType, &guide->args );
  378. // Increment counters
  379. funcCount++;
  380. comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
  381. }
  382. // Move to next item in the sequence
  383. macro->pos = comboItem;
  384. // If the ResultMacro is finished, it will be removed on the next iteration
  385. return ResultMacroEval_DoNothing;
  386. }
  387. // Update pending trigger list
  388. void Macro_updateTriggerMacroPendingList()
  389. {
  390. // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
  391. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  392. {
  393. // Lookup Trigger List
  394. unsigned int *triggerList = Macro_layerLookup( macroTriggerListBuffer[ key ].scanCode );
  395. // Number of Triggers in list
  396. unsigned int triggerListSize = triggerList[0];
  397. // Iterate over triggerList to see if any TriggerMacros need to be added
  398. // First item is the number of items in the TriggerList
  399. for ( unsigned int macro = 1; macro < triggerListSize + 1; macro++ )
  400. {
  401. // Lookup trigger macro index
  402. unsigned int triggerMacroIndex = triggerList[ macro ];
  403. // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
  404. // triggerList needs to be added
  405. unsigned int pending = 0;
  406. for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
  407. {
  408. // Stop scanning if the trigger macro index is found in the pending list
  409. if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
  410. break;
  411. }
  412. // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
  413. // Add it to the list
  414. if ( pending == macroTriggerMacroPendingListSize )
  415. {
  416. macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
  417. }
  418. }
  419. }
  420. }
  421. // Macro Procesing Loop
  422. // Called once per USB buffer send
  423. inline void Macro_process()
  424. {
  425. // Only do one round of macro processing between Output Module timer sends
  426. if ( USBKeys_Sent != 0 )
  427. return;
  428. // If the pause flag is set, only process if the step counter is non-zero
  429. if ( macroPauseMode )
  430. {
  431. if ( macroStepCounter == 0 )
  432. return;
  433. // Proceed, decrementing the step counter
  434. macroStepCounter--;
  435. }
  436. // Update pending trigger list, before processing TriggerMacros
  437. Macro_updateTriggerMacroPendingList();
  438. // Tail pointer for macroTriggerMacroPendingList
  439. // Macros must be explicitly re-added
  440. unsigned int macroTriggerMacroPendingListTail = 0;
  441. // Iterate through the pending TriggerMacros, processing each of them
  442. for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  443. {
  444. switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
  445. {
  446. // Trigger Result Macro (purposely falling through)
  447. case TriggerMacroEval_DoResult:
  448. // Append ResultMacro to PendingList
  449. Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
  450. // Otherwise, just re-add
  451. default:
  452. macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
  453. break;
  454. // Trigger Result Macro and Remove (purposely falling through)
  455. case TriggerMacroEval_DoResultAndRemove:
  456. // Append ResultMacro to PendingList
  457. Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
  458. // Remove Macro from Pending List, nothing to do, removing by default
  459. case TriggerMacroEval_Remove:
  460. break;
  461. }
  462. }
  463. // Update the macroTriggerMacroPendingListSize with the tail pointer
  464. macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
  465. // Tail pointer for macroResultMacroPendingList
  466. // Macros must be explicitly re-added
  467. unsigned int macroResultMacroPendingListTail = 0;
  468. // Iterate through the pending ResultMacros, processing each of them
  469. for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  470. {
  471. switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
  472. {
  473. // Re-add macros to pending list
  474. case ResultMacroEval_DoNothing:
  475. default:
  476. macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
  477. break;
  478. // Remove Macro from Pending List, nothing to do, removing by default
  479. case ResultMacroEval_Remove:
  480. break;
  481. }
  482. }
  483. // Update the macroResultMacroPendingListSize with the tail pointer
  484. macroResultMacroPendingListSize = macroResultMacroPendingListTail;
  485. /* TODO
  486. // Loop through input buffer
  487. for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
  488. {
  489. // Get the keycode from the buffer
  490. uint8_t key = KeyIndex_Buffer[index];
  491. // Set the modifier bit if this key is a modifier
  492. if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
  493. {
  494. USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
  495. // Modifier processed, move on to the next key
  496. continue;
  497. }
  498. // Too many keys
  499. if ( USBKeys_Sent >= USBKeys_MaxSize )
  500. {
  501. warn_msg("USB Key limit reached");
  502. errorLED( 1 );
  503. break;
  504. }
  505. USBKeys_Array[USBKeys_Sent++] = key;
  506. }
  507. */
  508. // Signal buffer that we've used it
  509. Scan_finishedWithMacro( macroTriggerListBufferSize );
  510. // Reset TriggerList buffer
  511. macroTriggerListBufferSize = 0;
  512. // If Macro debug mode is set, clear the USB Buffer
  513. if ( macroDebugMode )
  514. {
  515. USBKeys_Modifiers = 0;
  516. USBKeys_Sent = 0;
  517. }
  518. }
  519. inline void Macro_setup()
  520. {
  521. // Register Macro CLI dictionary
  522. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  523. // Disable Macro debug mode
  524. macroDebugMode = 0;
  525. // Disable Macro pause flag
  526. macroPauseMode = 0;
  527. // Set Macro step counter to zero
  528. macroStepCounter = 0;
  529. // Make sure macro trigger buffer is empty
  530. macroTriggerListBufferSize = 0;
  531. // Initialize TriggerMacro states
  532. for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
  533. {
  534. TriggerMacroList[ macro ].result = 0;
  535. TriggerMacroList[ macro ].pos = 0;
  536. TriggerMacroList[ macro ].state = TriggerMacro_Waiting;
  537. }
  538. // Initialize ResultMacro states
  539. for ( unsigned int macro = 0; macro < ResultMacroNum; macro++ )
  540. {
  541. ResultMacroList[ macro ].pos = 0;
  542. ResultMacroList[ macro ].state = 0;
  543. ResultMacroList[ macro ].stateType = 0;
  544. }
  545. }
  546. // ----- CLI Command Functions -----
  547. void cliFunc_capList( char* args )
  548. {
  549. print( NL );
  550. info_msg("Capabilities List");
  551. // Iterate through all of the capabilities and display them
  552. for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
  553. {
  554. print( NL "\t" );
  555. printHex( cap );
  556. print(" - ");
  557. // Display/Lookup Capability Name (utilize debug mode of capability)
  558. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  559. capability( 0xFF, 0xFF, 0 );
  560. }
  561. }
  562. void cliFunc_capSelect( char* args )
  563. {
  564. // Parse code from argument
  565. char* curArgs;
  566. char* arg1Ptr;
  567. char* arg2Ptr = args;
  568. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  569. unsigned int totalArgs = 2; // Always at least two args
  570. unsigned int cap = 0;
  571. // Arguments used for keyboard capability function
  572. unsigned int argSetCount = 0;
  573. uint8_t *argSet = (uint8_t*)args;
  574. // Process all args
  575. for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
  576. {
  577. curArgs = arg2Ptr;
  578. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  579. // Stop processing args if no more are found
  580. // Extra arguments are ignored
  581. if ( *arg1Ptr == '\0' )
  582. break;
  583. // For the first argument, choose the capability
  584. if ( c == 0 ) switch ( arg1Ptr[0] )
  585. {
  586. // Keyboard Capability
  587. case 'K':
  588. // Determine capability index
  589. cap = decToInt( &arg1Ptr[1] );
  590. // Lookup the number of args
  591. totalArgs += CapabilitiesList[ cap ].argCount;
  592. continue;
  593. }
  594. // Because allocating memory isn't doable, and the argument count is arbitrary
  595. // The argument pointer is repurposed as the argument list (much smaller anyways)
  596. argSet[ argSetCount++ ] = (uint8_t)decToInt( arg1Ptr );
  597. // Once all the arguments are prepared, call the keyboard capability function
  598. if ( argSetCount == totalArgs )
  599. {
  600. // Indicate that the capability was called
  601. print( NL );
  602. info_msg("K");
  603. printInt8( cap );
  604. print(" - ");
  605. printHex( argSet[0] );
  606. print(" - ");
  607. printHex( argSet[1] );
  608. print(" - ");
  609. printHex( argSet[2] );
  610. print( "..." NL );
  611. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  612. capability( argSet[0], argSet[1], &argSet[2] );
  613. }
  614. }
  615. }
  616. void cliFunc_keyPress( char* args )
  617. {
  618. // Parse codes from arguments
  619. char* curArgs;
  620. char* arg1Ptr;
  621. char* arg2Ptr = args;
  622. // Process all args
  623. for ( ;; )
  624. {
  625. curArgs = arg2Ptr;
  626. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  627. // Stop processing args if no more are found
  628. if ( *arg1Ptr == '\0' )
  629. break;
  630. // Ignore non-Scancode numbers
  631. switch ( arg1Ptr[0] )
  632. {
  633. // Scancode
  634. case 'S':
  635. Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  636. break;
  637. }
  638. }
  639. }
  640. void cliFunc_keyRelease( char* args )
  641. {
  642. // Parse codes from arguments
  643. char* curArgs;
  644. char* arg1Ptr;
  645. char* arg2Ptr = args;
  646. // Process all args
  647. for ( ;; )
  648. {
  649. curArgs = arg2Ptr;
  650. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  651. // Stop processing args if no more are found
  652. if ( *arg1Ptr == '\0' )
  653. break;
  654. // Ignore non-Scancode numbers
  655. switch ( arg1Ptr[0] )
  656. {
  657. // Scancode
  658. case 'S':
  659. Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  660. break;
  661. }
  662. }
  663. }
  664. void cliFunc_layerList( char* args )
  665. {
  666. print( NL );
  667. info_msg("Layer List");
  668. // Iterate through all of the layers and display them
  669. for ( unsigned int layer = 0; layer < LayerNum; layer++ )
  670. {
  671. print( NL "\t" );
  672. printHex( layer );
  673. print(" - ");
  674. // Display layer name
  675. dPrint( LayerIndex[ layer ].name );
  676. // Default map
  677. if ( layer == 0 )
  678. print(" \033[1m(default)\033[0m");
  679. // Layer State
  680. print( NL "\t\t Layer State: " );
  681. printHex( LayerIndex[ layer ].state );
  682. // Max Index
  683. print(" Max Index: ");
  684. printHex( LayerIndex[ layer ].max );
  685. }
  686. }
  687. void cliFunc_layerState( char* args )
  688. {
  689. // Parse codes from arguments
  690. char* curArgs;
  691. char* arg1Ptr;
  692. char* arg2Ptr = args;
  693. uint8_t arg1 = 0;
  694. uint8_t arg2 = 0;
  695. // Process first two args
  696. for ( uint8_t c = 0; c < 2; c++ )
  697. {
  698. curArgs = arg2Ptr;
  699. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  700. // Stop processing args if no more are found
  701. if ( *arg1Ptr == '\0' )
  702. break;
  703. switch ( c )
  704. {
  705. // First argument (e.g. L1)
  706. case 0:
  707. if ( arg1Ptr[0] != 'L' )
  708. return;
  709. arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
  710. break;
  711. // Second argument (e.g. 4)
  712. case 1:
  713. arg2 = (uint8_t)decToInt( arg1Ptr );
  714. // Display operation (to indicate that it worked)
  715. print( NL );
  716. info_msg("Setting Layer L");
  717. printInt8( arg1 );
  718. print(" to - ");
  719. printHex( arg2 );
  720. // Set the layer state
  721. LayerIndex[ arg1 ].state = arg2;
  722. break;
  723. }
  724. }
  725. }
  726. void cliFunc_macroDebug( char* args )
  727. {
  728. // Toggle macro debug mode
  729. macroDebugMode = macroDebugMode ? 0 : 1;
  730. print( NL );
  731. info_msg("Macro Debug Mode: ");
  732. printInt8( macroDebugMode );
  733. }
  734. void cliFunc_macroList( char* args )
  735. {
  736. // Show available trigger macro indices
  737. print( NL );
  738. info_msg("Trigger Macros Range: T0 -> T");
  739. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  740. // Show available result macro indices
  741. print( NL );
  742. info_msg("Result Macros Range: R0 -> R");
  743. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  744. // Show Trigger to Result Macro Links
  745. print( NL );
  746. info_msg("Trigger : Result Macro Pairs");
  747. for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
  748. {
  749. print( NL );
  750. print("\tT");
  751. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  752. print(" : R");
  753. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  754. }
  755. }
  756. void cliFunc_macroProc( char* args )
  757. {
  758. // Toggle macro pause mode
  759. macroPauseMode = macroPauseMode ? 0 : 1;
  760. print( NL );
  761. info_msg("Macro Processing Mode: ");
  762. printInt8( macroPauseMode );
  763. }
  764. void macroDebugShowTrigger( unsigned int index )
  765. {
  766. // Only proceed if the macro exists
  767. if ( index >= TriggerMacroNum )
  768. return;
  769. // Trigger Macro Show
  770. TriggerMacro *macro = &TriggerMacroList[ index ];
  771. print( NL );
  772. info_msg("Trigger Macro Index: ");
  773. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  774. print( NL );
  775. // Read the comboLength for combo in the sequence (sequence of combos)
  776. unsigned int pos = 0;
  777. uint8_t comboLength = macro->guide[ pos ];
  778. // Iterate through and interpret the guide
  779. while ( comboLength != 0 )
  780. {
  781. // Initial position of the combo
  782. unsigned int comboPos = ++pos;
  783. // Iterate through the combo
  784. while ( pos < comboLength * TriggerGuideSize + comboPos )
  785. {
  786. // Assign TriggerGuide element (key type, state and scancode)
  787. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  788. // Display guide information about trigger key
  789. printHex( guide->scanCode );
  790. print("|");
  791. printHex( guide->type );
  792. print("|");
  793. printHex( guide->state );
  794. // Increment position
  795. pos += TriggerGuideSize;
  796. // Only show combo separator if there are combos left in the sequence element
  797. if ( pos < comboLength * TriggerGuideSize + comboPos )
  798. print("+");
  799. }
  800. // Read the next comboLength
  801. comboLength = macro->guide[ pos ];
  802. // Only show sequence separator if there is another combo to process
  803. if ( comboLength != 0 )
  804. print(";");
  805. }
  806. // Display current position
  807. print( NL "Position: " );
  808. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  809. // Display result macro index
  810. print( NL "Result Macro Index: " );
  811. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  812. }
  813. void macroDebugShowResult( unsigned int index )
  814. {
  815. // Only proceed if the macro exists
  816. if ( index >= ResultMacroNum )
  817. return;
  818. // Trigger Macro Show
  819. ResultMacro *macro = &ResultMacroList[ index ];
  820. print( NL );
  821. info_msg("Result Macro Index: ");
  822. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  823. print( NL );
  824. // Read the comboLength for combo in the sequence (sequence of combos)
  825. unsigned int pos = 0;
  826. uint8_t comboLength = macro->guide[ pos++ ];
  827. // Iterate through and interpret the guide
  828. while ( comboLength != 0 )
  829. {
  830. // Function Counter, used to keep track of the combos processed
  831. unsigned int funcCount = 0;
  832. // Iterate through the combo
  833. while ( funcCount < comboLength )
  834. {
  835. // Assign TriggerGuide element (key type, state and scancode)
  836. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  837. // Display Function Index
  838. printHex( guide->index );
  839. print("|");
  840. // Display Function Ptr Address
  841. printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
  842. print("|");
  843. // Display/Lookup Capability Name (utilize debug mode of capability)
  844. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  845. capability( 0xFF, 0xFF, 0 );
  846. // Display Argument(s)
  847. print("(");
  848. for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  849. {
  850. // Arguments are only 8 bit values
  851. printHex( (&guide->args)[ arg ] );
  852. // Only show arg separator if there are args left
  853. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  854. print(",");
  855. }
  856. print(")");
  857. // Increment position
  858. pos += ResultGuideSize( guide );
  859. // Increment function count
  860. funcCount++;
  861. // Only show combo separator if there are combos left in the sequence element
  862. if ( funcCount < comboLength )
  863. print("+");
  864. }
  865. // Read the next comboLength
  866. comboLength = macro->guide[ pos++ ];
  867. // Only show sequence separator if there is another combo to process
  868. if ( comboLength != 0 )
  869. print(";");
  870. }
  871. // Display current position
  872. print( NL "Position: " );
  873. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  874. // Display final trigger state/type
  875. print( NL "Final Trigger State (State/Type): " );
  876. printHex( macro->state );
  877. print("/");
  878. printHex( macro->stateType );
  879. }
  880. void cliFunc_macroShow( char* args )
  881. {
  882. // Parse codes from arguments
  883. char* curArgs;
  884. char* arg1Ptr;
  885. char* arg2Ptr = args;
  886. // Process all args
  887. for ( ;; )
  888. {
  889. curArgs = arg2Ptr;
  890. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  891. // Stop processing args if no more are found
  892. if ( *arg1Ptr == '\0' )
  893. break;
  894. // Ignore invalid codes
  895. switch ( arg1Ptr[0] )
  896. {
  897. // Indexed Trigger Macro
  898. case 'T':
  899. macroDebugShowTrigger( decToInt( &arg1Ptr[1] ) );
  900. break;
  901. // Indexed Result Macro
  902. case 'R':
  903. macroDebugShowResult( decToInt( &arg1Ptr[1] ) );
  904. break;
  905. }
  906. }
  907. }
  908. void cliFunc_macroStep( char* args )
  909. {
  910. // Parse number from argument
  911. // NOTE: Only first argument is used
  912. char* arg1Ptr;
  913. char* arg2Ptr;
  914. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  915. // Set the macro step counter, negative int's are cast to uint
  916. macroStepCounter = (unsigned int)decToInt( arg1Ptr );
  917. }