Kiibohd Controller
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

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