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

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