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

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