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

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