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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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 ( LayerState[ layer ] & layerState )
  125. {
  126. // Unset
  127. LayerState[ layer ] &= ~layerState;
  128. }
  129. else
  130. {
  131. // Set
  132. LayerState[ layer ] |= 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 ( LayerState[ layer ] == 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. const 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 = LayerState[ layerIndex ] & 0x02;
  248. if ( latch )
  249. {
  250. LayerState[ layerIndex ] &= ~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 ( (LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x01) ^ (latch>>1) ^ ((LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x04)>>2) )
  256. {
  257. // Lookup layer
  258. nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
  259. // Determine if layer has key defined
  260. // Make sure scanCode is between layer first and last scancodes
  261. if ( map != 0
  262. && scanCode <= layer->last
  263. && scanCode >= layer->first
  264. && *map[ scanCode - layer->first ] != 0 )
  265. {
  266. return map[ scanCode - layer->first ];
  267. }
  268. }
  269. }
  270. // Do lookup on default layer
  271. nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
  272. // Lookup default layer
  273. const Layer *layer = &LayerIndex[0];
  274. // Make sure scanCode is between layer first and last scancodes
  275. if ( map != 0
  276. && scanCode <= layer->last
  277. && scanCode >= layer->first
  278. && *map[ scanCode - layer->first ] != 0 )
  279. {
  280. return map[ scanCode - layer->first ];
  281. }
  282. // Otherwise no defined Trigger Macro
  283. erro_msg("Scan Code has no defined Trigger Macro: ");
  284. printHex( scanCode );
  285. return 0;
  286. }
  287. // Update the scancode key state
  288. // States:
  289. // * 0x00 - Off
  290. // * 0x01 - Pressed
  291. // * 0x02 - Held
  292. // * 0x03 - Released
  293. // * 0x04 - Unpressed (this is currently ignored)
  294. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  295. {
  296. // Only add to macro trigger list if one of three states
  297. switch ( state )
  298. {
  299. case 0x01: // Pressed
  300. case 0x02: // Held
  301. case 0x03: // Released
  302. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  303. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  304. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
  305. macroTriggerListBufferSize++;
  306. break;
  307. }
  308. }
  309. // Update the scancode analog state
  310. // States:
  311. // * 0x00 - Off
  312. // * 0x01 - Released
  313. // * 0x02-0xFF - Analog value (low to high)
  314. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  315. {
  316. // Only add to macro trigger list if non-off
  317. if ( state != 0x00 )
  318. {
  319. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  320. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  321. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
  322. macroTriggerListBufferSize++;
  323. }
  324. }
  325. // Update led state
  326. // States:
  327. // * 0x00 - Off
  328. // * 0x01 - On
  329. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  330. {
  331. // Only add to macro trigger list if non-off
  332. if ( state != 0x00 )
  333. {
  334. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
  335. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  336. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
  337. macroTriggerListBufferSize++;
  338. }
  339. }
  340. // Append result macro to pending list, checking for duplicates
  341. // Do nothing if duplicate
  342. inline void Macro_appendResultMacroToPendingList( TriggerMacro *triggerMacro )
  343. {
  344. // Lookup result macro index
  345. var_uint_t resultMacroIndex = triggerMacro->result;
  346. // Iterate through result macro pending list, making sure this macro hasn't been added yet
  347. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  348. {
  349. // If duplicate found, do nothing
  350. if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
  351. return;
  352. }
  353. // No duplicates found, add to pending list
  354. macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
  355. // Lookup scanCode of the last key in the last combo
  356. var_uint_t pos = 0;
  357. for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
  358. {
  359. pos += TriggerGuideSize * comboLength + 1;
  360. comboLength = triggerMacro->guide[ pos ];
  361. }
  362. uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
  363. // Lookup scanCode in buffer list for the current state and stateType
  364. for ( uint8_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
  365. {
  366. if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
  367. {
  368. ResultMacroList[ resultMacroIndex ].state = macroTriggerListBuffer[ keyIndex ].state;
  369. ResultMacroList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
  370. }
  371. }
  372. // Reset the macro position
  373. ResultMacroList[ resultMacroIndex ].pos = 0;
  374. }
  375. // Determine if long ResultMacro (more than 1 seqence element)
  376. inline uint8_t Macro_isLongResultMacro( ResultMacro *macro )
  377. {
  378. // Check the second sequence combo length
  379. // If non-zero return non-zero (long sequence)
  380. // 0 otherwise (short sequence)
  381. var_uint_t position = 1;
  382. for ( var_uint_t result = 0; result < macro->guide[0]; result++ )
  383. position += ResultGuideSize( (ResultGuide*)&macro->guide[ position ] );
  384. return macro->guide[ position ];
  385. }
  386. // Determine if long TriggerMacro (more than 1 sequence element)
  387. inline uint8_t Macro_isLongTriggerMacro( TriggerMacro *macro )
  388. {
  389. // Check the second sequence combo length
  390. // If non-zero return non-zero (long sequence)
  391. // 0 otherwise (short sequence)
  392. return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ];
  393. }
  394. // Votes on the given key vs. guide, short macros
  395. inline TriggerMacroVote Macro_evalShortTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  396. {
  397. // Depending on key type
  398. switch ( guide->type )
  399. {
  400. // Normal State Type
  401. case 0x00:
  402. // For short TriggerMacros completely ignore incorrect keys
  403. if ( guide->scanCode == key->scanCode )
  404. {
  405. switch ( key->state )
  406. {
  407. // Correct key, pressed, possible passing
  408. case 0x01:
  409. return TriggerMacroVote_Pass;
  410. // Correct key, held, possible passing or release
  411. case 0x02:
  412. return TriggerMacroVote_PassRelease;
  413. // Correct key, released, possible release
  414. case 0x03:
  415. return TriggerMacroVote_Release;
  416. }
  417. }
  418. return TriggerMacroVote_DoNothing;
  419. // LED State Type
  420. case 0x01:
  421. erro_print("LED State Type - Not implemented...");
  422. break;
  423. // Analog State Type
  424. case 0x02:
  425. erro_print("Analog State Type - Not implemented...");
  426. break;
  427. // Invalid State Type
  428. default:
  429. erro_print("Invalid State Type. This is a bug.");
  430. break;
  431. }
  432. // XXX Shouldn't reach here
  433. return TriggerMacroVote_Invalid;
  434. }
  435. // Votes on the given key vs. guide, long macros
  436. // A long macro is defined as a guide with more than 1 combo
  437. inline TriggerMacroVote Macro_evalLongTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  438. {
  439. // Depending on key type
  440. switch ( guide->type )
  441. {
  442. // Normal State Type
  443. case 0x00:
  444. // Depending on the state of the buffered key, make voting decision
  445. // Incorrect key
  446. if ( guide->scanCode != key->scanCode )
  447. {
  448. switch ( key->state )
  449. {
  450. // Wrong key, pressed, fail
  451. case 0x01:
  452. return TriggerMacroVote_Fail;
  453. // Wrong key, held, do not pass (no effect)
  454. case 0x02:
  455. return TriggerMacroVote_DoNothing;
  456. // Wrong key released, fail out if pos == 0
  457. case 0x03:
  458. return TriggerMacroVote_DoNothing | TriggerMacroVote_DoNothingRelease;
  459. }
  460. }
  461. // Correct key
  462. else
  463. {
  464. switch ( key->state )
  465. {
  466. // Correct key, pressed, possible passing
  467. case 0x01:
  468. return TriggerMacroVote_Pass;
  469. // Correct key, held, possible passing or release
  470. case 0x02:
  471. return TriggerMacroVote_PassRelease;
  472. // Correct key, released, possible release
  473. case 0x03:
  474. return TriggerMacroVote_Release;
  475. }
  476. }
  477. break;
  478. // LED State Type
  479. case 0x01:
  480. erro_print("LED State Type - Not implemented...");
  481. break;
  482. // Analog State Type
  483. case 0x02:
  484. erro_print("Analog State Type - Not implemented...");
  485. break;
  486. // Invalid State Type
  487. default:
  488. erro_print("Invalid State Type. This is a bug.");
  489. break;
  490. }
  491. // XXX Shouldn't reach here
  492. return TriggerMacroVote_Invalid;
  493. }
  494. // Evaluate/Update TriggerMacro
  495. inline TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
  496. {
  497. // Lookup TriggerMacro
  498. TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
  499. // Check if macro has finished and should be incremented sequence elements
  500. if ( macro->state == TriggerMacro_Release )
  501. {
  502. macro->state = TriggerMacro_Waiting;
  503. macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1;
  504. }
  505. // Current Macro position
  506. var_uint_t pos = macro->pos;
  507. // Length of the combo being processed
  508. uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
  509. // If no combo items are left, remove the TriggerMacro from the pending list
  510. if ( comboLength == 0 )
  511. {
  512. return TriggerMacroEval_Remove;
  513. }
  514. // Check if this is a long Trigger Macro
  515. uint8_t longMacro = Macro_isLongTriggerMacro( macro );
  516. // Iterate through the items in the combo, voting the on the key state
  517. // If any of the pressed keys do not match, fail the macro
  518. //
  519. // The macro is waiting for input when in the TriggerMacro_Waiting state
  520. // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
  521. // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
  522. // On scan after position increment, change to TriggerMacro_Waiting state
  523. // TODO Add support for system LED states (NumLock, CapsLock, etc.)
  524. // TODO Add support for analog key states
  525. // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
  526. // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
  527. TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
  528. for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
  529. {
  530. // Assign TriggerGuide element (key type, state and scancode)
  531. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
  532. TriggerMacroVote vote = TriggerMacroVote_Invalid;
  533. // Iterate through the key buffer, comparing to each key in the combo
  534. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  535. {
  536. // Lookup key information
  537. TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
  538. // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
  539. // Also mask all of the non-passing votes
  540. vote |= longMacro
  541. ? Macro_evalLongTriggerMacroVote( keyInfo, guide )
  542. : Macro_evalShortTriggerMacroVote( keyInfo, guide );
  543. if ( vote >= TriggerMacroVote_Pass )
  544. {
  545. vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
  546. break;
  547. }
  548. }
  549. // If no pass vote was found after scanning all of the keys
  550. // Fail the combo, if this is a short macro (long macros already will have a fail vote)
  551. if ( !longMacro && vote < TriggerMacroVote_Pass )
  552. vote |= TriggerMacroVote_Fail;
  553. // After voting, append to overall vote
  554. overallVote |= vote;
  555. }
  556. // If no pass vote was found after scanning the entire combo
  557. // And this is the first position in the combo, just remove it (nothing important happened)
  558. if ( longMacro && overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
  559. overallVote |= TriggerMacroVote_Fail;
  560. // Decide new state of macro after voting
  561. // Fail macro, remove from pending list
  562. if ( overallVote & TriggerMacroVote_Fail )
  563. {
  564. return TriggerMacroEval_Remove;
  565. }
  566. // Do nothing, incorrect key is being held or released
  567. else if ( overallVote & TriggerMacroVote_DoNothing && longMacro )
  568. {
  569. // Just doing nothing :)
  570. }
  571. // If ready for transition and in Press state, set to Waiting and increment combo position
  572. // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
  573. else if ( overallVote & TriggerMacroVote_Release && macro->state == TriggerMacro_Press )
  574. {
  575. macro->state = TriggerMacro_Release;
  576. // If this is the last combo in the sequence, remove from the pending list
  577. if ( macro->guide[ macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1 ] == 0 )
  578. return TriggerMacroEval_DoResultAndRemove;
  579. }
  580. // If passing and in Waiting state, set macro state to Press
  581. else if ( overallVote & TriggerMacroVote_Pass
  582. && ( macro->state == TriggerMacro_Waiting || macro->state == TriggerMacro_Press ) )
  583. {
  584. macro->state = TriggerMacro_Press;
  585. // If in press state, and this is the final combo, send request for ResultMacro
  586. // Check to see if the result macro only has a single element
  587. // If this result macro has more than 1 key, only send once
  588. // TODO Add option to have long macro repeat rate
  589. if ( macro->guide[ pos + comboLength + 1 ] == 0 )
  590. {
  591. // Long result macro (more than 1 combo)
  592. if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
  593. {
  594. // Only ever trigger result once, on press
  595. if ( overallVote == TriggerMacroVote_Pass )
  596. {
  597. return TriggerMacroEval_DoResultAndRemove;
  598. }
  599. }
  600. // Short result macro
  601. else
  602. {
  603. // Only trigger result once, on press, if long trigger (more than 1 combo)
  604. if ( Macro_isLongTriggerMacro( macro ) )
  605. {
  606. return TriggerMacroEval_DoResultAndRemove;
  607. }
  608. // Otherwise, trigger result continuously
  609. else
  610. {
  611. return TriggerMacroEval_DoResult;
  612. }
  613. }
  614. }
  615. }
  616. // Otherwise, just remove the macro on key release
  617. // One more result has to be called to indicate to the ResultMacro that the key transitioned to the release state
  618. else if ( overallVote & TriggerMacroVote_Release )
  619. {
  620. return TriggerMacroEval_DoResultAndRemove;
  621. }
  622. // If this is a short macro, just remove it
  623. // The state can be rebuilt on the next iteration
  624. if ( !longMacro )
  625. return TriggerMacroEval_Remove;
  626. return TriggerMacroEval_DoNothing;
  627. }
  628. // Evaluate/Update ResultMacro
  629. inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex )
  630. {
  631. // Lookup ResultMacro
  632. ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
  633. // Current Macro position
  634. var_uint_t pos = macro->pos;
  635. // Length of combo being processed
  636. uint8_t comboLength = macro->guide[ pos ];
  637. // Function Counter, used to keep track of the combo items processed
  638. var_uint_t funcCount = 0;
  639. // Combo Item Position within the guide
  640. var_uint_t comboItem = pos + 1;
  641. // Iterate through the Result Combo
  642. while ( funcCount < comboLength )
  643. {
  644. // Assign TriggerGuide element (key type, state and scancode)
  645. ResultGuide *guide = (ResultGuide*)(&macro->guide[ comboItem ]);
  646. // Do lookup on capability function
  647. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  648. // Call capability
  649. capability( macro->state, macro->stateType, &guide->args );
  650. // Increment counters
  651. funcCount++;
  652. comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
  653. }
  654. // Move to next item in the sequence
  655. macro->pos = comboItem;
  656. // If the ResultMacro is finished, remove
  657. if ( macro->guide[ comboItem ] == 0 )
  658. {
  659. macro->pos = 0;
  660. return ResultMacroEval_Remove;
  661. }
  662. // Otherwise leave the macro in the list
  663. return ResultMacroEval_DoNothing;
  664. }
  665. // Update pending trigger list
  666. inline void Macro_updateTriggerMacroPendingList()
  667. {
  668. // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
  669. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  670. {
  671. // TODO LED States
  672. // TODO Analog Switches
  673. // Only add TriggerMacro to pending list if key was pressed (not held, released or off)
  674. if ( macroTriggerListBuffer[ key ].state == 0x00 && macroTriggerListBuffer[ key ].state != 0x01 )
  675. continue;
  676. // Lookup Trigger List
  677. nat_ptr_t *triggerList = Macro_layerLookup( macroTriggerListBuffer[ key ].scanCode );
  678. // Number of Triggers in list
  679. nat_ptr_t triggerListSize = triggerList[0];
  680. // Iterate over triggerList to see if any TriggerMacros need to be added
  681. // First item is the number of items in the TriggerList
  682. for ( var_uint_t macro = 1; macro < triggerListSize + 1; macro++ )
  683. {
  684. // Lookup trigger macro index
  685. var_uint_t triggerMacroIndex = triggerList[ macro ];
  686. // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
  687. // triggerList needs to be added
  688. var_uint_t pending = 0;
  689. for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
  690. {
  691. // Stop scanning if the trigger macro index is found in the pending list
  692. if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
  693. break;
  694. }
  695. // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
  696. // Add it to the list
  697. if ( pending == macroTriggerMacroPendingListSize )
  698. {
  699. macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
  700. // Reset macro position
  701. TriggerMacroList[ triggerMacroIndex ].pos = 0;
  702. TriggerMacroList[ triggerMacroIndex ].state = TriggerMacro_Waiting;
  703. }
  704. }
  705. }
  706. }
  707. // Macro Procesing Loop
  708. // Called once per USB buffer send
  709. inline void Macro_process()
  710. {
  711. // Only do one round of macro processing between Output Module timer sends
  712. if ( USBKeys_Sent != 0 )
  713. return;
  714. // If the pause flag is set, only process if the step counter is non-zero
  715. if ( macroPauseMode )
  716. {
  717. if ( macroStepCounter == 0 )
  718. return;
  719. // Proceed, decrementing the step counter
  720. macroStepCounter--;
  721. dbug_print("Macro Step");
  722. }
  723. // Update pending trigger list, before processing TriggerMacros
  724. Macro_updateTriggerMacroPendingList();
  725. // Tail pointer for macroTriggerMacroPendingList
  726. // Macros must be explicitly re-added
  727. var_uint_t macroTriggerMacroPendingListTail = 0;
  728. // Iterate through the pending TriggerMacros, processing each of them
  729. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  730. {
  731. switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
  732. {
  733. // Trigger Result Macro (purposely falling through)
  734. case TriggerMacroEval_DoResult:
  735. // Append ResultMacro to PendingList
  736. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  737. default:
  738. macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
  739. break;
  740. // Trigger Result Macro and Remove (purposely falling through)
  741. case TriggerMacroEval_DoResultAndRemove:
  742. // Append ResultMacro to PendingList
  743. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  744. // Remove Macro from Pending List, nothing to do, removing by default
  745. case TriggerMacroEval_Remove:
  746. break;
  747. }
  748. }
  749. // Update the macroTriggerMacroPendingListSize with the tail pointer
  750. macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
  751. // Tail pointer for macroResultMacroPendingList
  752. // Macros must be explicitly re-added
  753. var_uint_t macroResultMacroPendingListTail = 0;
  754. // Iterate through the pending ResultMacros, processing each of them
  755. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  756. {
  757. switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
  758. {
  759. // Re-add macros to pending list
  760. case ResultMacroEval_DoNothing:
  761. default:
  762. macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
  763. break;
  764. // Remove Macro from Pending List, nothing to do, removing by default
  765. case ResultMacroEval_Remove:
  766. break;
  767. }
  768. }
  769. // Update the macroResultMacroPendingListSize with the tail pointer
  770. macroResultMacroPendingListSize = macroResultMacroPendingListTail;
  771. // Signal buffer that we've used it
  772. Scan_finishedWithMacro( macroTriggerListBufferSize );
  773. // Reset TriggerList buffer
  774. macroTriggerListBufferSize = 0;
  775. // If Macro debug mode is set, clear the USB Buffer
  776. if ( macroDebugMode )
  777. {
  778. USBKeys_Modifiers = 0;
  779. USBKeys_Sent = 0;
  780. }
  781. }
  782. inline void Macro_setup()
  783. {
  784. // Register Macro CLI dictionary
  785. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  786. // Disable Macro debug mode
  787. macroDebugMode = 0;
  788. // Disable Macro pause flag
  789. macroPauseMode = 0;
  790. // Set Macro step counter to zero
  791. macroStepCounter = 0;
  792. // Make sure macro trigger buffer is empty
  793. macroTriggerListBufferSize = 0;
  794. // Initialize TriggerMacro states
  795. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  796. {
  797. TriggerMacroList[ macro ].pos = 0;
  798. TriggerMacroList[ macro ].state = TriggerMacro_Waiting;
  799. }
  800. // Initialize ResultMacro states
  801. for ( var_uint_t macro = 0; macro < ResultMacroNum; macro++ )
  802. {
  803. ResultMacroList[ macro ].pos = 0;
  804. ResultMacroList[ macro ].state = 0;
  805. ResultMacroList[ macro ].stateType = 0;
  806. }
  807. }
  808. // ----- CLI Command Functions -----
  809. void cliFunc_capList( char* args )
  810. {
  811. print( NL );
  812. info_msg("Capabilities List");
  813. printHex( CapabilitiesNum );
  814. // Iterate through all of the capabilities and display them
  815. for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
  816. {
  817. print( NL "\t" );
  818. printHex( cap );
  819. print(" - ");
  820. // Display/Lookup Capability Name (utilize debug mode of capability)
  821. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  822. capability( 0xFF, 0xFF, 0 );
  823. }
  824. }
  825. void cliFunc_capSelect( char* args )
  826. {
  827. // Parse code from argument
  828. char* curArgs;
  829. char* arg1Ptr;
  830. char* arg2Ptr = args;
  831. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  832. var_uint_t totalArgs = 2; // Always at least two args
  833. var_uint_t cap = 0;
  834. // Arguments used for keyboard capability function
  835. var_uint_t argSetCount = 0;
  836. uint8_t *argSet = (uint8_t*)args;
  837. // Process all args
  838. for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
  839. {
  840. curArgs = arg2Ptr;
  841. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  842. // Stop processing args if no more are found
  843. // Extra arguments are ignored
  844. if ( *arg1Ptr == '\0' )
  845. break;
  846. // For the first argument, choose the capability
  847. if ( c == 0 ) switch ( arg1Ptr[0] )
  848. {
  849. // Keyboard Capability
  850. case 'K':
  851. // Determine capability index
  852. cap = numToInt( &arg1Ptr[1] );
  853. // Lookup the number of args
  854. totalArgs += CapabilitiesList[ cap ].argCount;
  855. continue;
  856. }
  857. // Because allocating memory isn't doable, and the argument count is arbitrary
  858. // The argument pointer is repurposed as the argument list (much smaller anyways)
  859. argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
  860. // Once all the arguments are prepared, call the keyboard capability function
  861. if ( argSetCount == totalArgs )
  862. {
  863. // Indicate that the capability was called
  864. print( NL );
  865. info_msg("K");
  866. printInt8( cap );
  867. print(" - ");
  868. printHex( argSet[0] );
  869. print(" - ");
  870. printHex( argSet[1] );
  871. print(" - ");
  872. printHex( argSet[2] );
  873. print( "..." NL );
  874. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  875. capability( argSet[0], argSet[1], &argSet[2] );
  876. }
  877. }
  878. }
  879. void cliFunc_keyHold( char* args )
  880. {
  881. // Parse codes from arguments
  882. char* curArgs;
  883. char* arg1Ptr;
  884. char* arg2Ptr = args;
  885. // Process all args
  886. for ( ;; )
  887. {
  888. curArgs = arg2Ptr;
  889. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  890. // Stop processing args if no more are found
  891. if ( *arg1Ptr == '\0' )
  892. break;
  893. // Ignore non-Scancode numbers
  894. switch ( arg1Ptr[0] )
  895. {
  896. // Scancode
  897. case 'S':
  898. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
  899. break;
  900. }
  901. }
  902. }
  903. void cliFunc_keyPress( char* args )
  904. {
  905. // Parse codes from arguments
  906. char* curArgs;
  907. char* arg1Ptr;
  908. char* arg2Ptr = args;
  909. // Process all args
  910. for ( ;; )
  911. {
  912. curArgs = arg2Ptr;
  913. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  914. // Stop processing args if no more are found
  915. if ( *arg1Ptr == '\0' )
  916. break;
  917. // Ignore non-Scancode numbers
  918. switch ( arg1Ptr[0] )
  919. {
  920. // Scancode
  921. case 'S':
  922. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  923. break;
  924. }
  925. }
  926. }
  927. void cliFunc_keyRelease( char* args )
  928. {
  929. // Parse codes from arguments
  930. char* curArgs;
  931. char* arg1Ptr;
  932. char* arg2Ptr = args;
  933. // Process all args
  934. for ( ;; )
  935. {
  936. curArgs = arg2Ptr;
  937. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  938. // Stop processing args if no more are found
  939. if ( *arg1Ptr == '\0' )
  940. break;
  941. // Ignore non-Scancode numbers
  942. switch ( arg1Ptr[0] )
  943. {
  944. // Scancode
  945. case 'S':
  946. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  947. break;
  948. }
  949. }
  950. }
  951. void cliFunc_layerList( char* args )
  952. {
  953. print( NL );
  954. info_msg("Layer List");
  955. // Iterate through all of the layers and display them
  956. for ( uint16_t layer = 0; layer < LayerNum; layer++ )
  957. {
  958. print( NL "\t" );
  959. printHex( layer );
  960. print(" - ");
  961. // Display layer name
  962. dPrint( (char*)LayerIndex[ layer ].name );
  963. // Default map
  964. if ( layer == 0 )
  965. print(" \033[1m(default)\033[0m");
  966. // Layer State
  967. print( NL "\t\t Layer State: " );
  968. printHex( LayerState[ layer ] );
  969. // First -> Last Indices
  970. print(" First -> Last Indices: ");
  971. printHex( LayerIndex[ layer ].first );
  972. print(" -> ");
  973. printHex( LayerIndex[ layer ].last );
  974. }
  975. }
  976. void cliFunc_layerState( char* args )
  977. {
  978. // Parse codes from arguments
  979. char* curArgs;
  980. char* arg1Ptr;
  981. char* arg2Ptr = args;
  982. uint8_t arg1 = 0;
  983. uint8_t arg2 = 0;
  984. // Process first two args
  985. for ( uint8_t c = 0; c < 2; c++ )
  986. {
  987. curArgs = arg2Ptr;
  988. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  989. // Stop processing args if no more are found
  990. if ( *arg1Ptr == '\0' )
  991. break;
  992. switch ( c )
  993. {
  994. // First argument (e.g. L1)
  995. case 0:
  996. if ( arg1Ptr[0] != 'L' )
  997. return;
  998. arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
  999. break;
  1000. // Second argument (e.g. 4)
  1001. case 1:
  1002. arg2 = (uint8_t)numToInt( arg1Ptr );
  1003. // Display operation (to indicate that it worked)
  1004. print( NL );
  1005. info_msg("Setting Layer L");
  1006. printInt8( arg1 );
  1007. print(" to - ");
  1008. printHex( arg2 );
  1009. // Set the layer state
  1010. LayerState[ arg1 ] = arg2;
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. void cliFunc_macroDebug( char* args )
  1016. {
  1017. // Toggle macro debug mode
  1018. macroDebugMode = macroDebugMode ? 0 : 1;
  1019. print( NL );
  1020. info_msg("Macro Debug Mode: ");
  1021. printInt8( macroDebugMode );
  1022. }
  1023. void cliFunc_macroList( char* args )
  1024. {
  1025. // Show pending key events
  1026. print( NL );
  1027. info_msg("Pending Key Events: ");
  1028. printInt16( (uint16_t)macroTriggerListBufferSize );
  1029. print(" : ");
  1030. for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
  1031. {
  1032. printHex( macroTriggerListBuffer[ key ].scanCode );
  1033. print(" ");
  1034. }
  1035. // Show pending trigger macros
  1036. print( NL );
  1037. info_msg("Pending Trigger Macros: ");
  1038. printInt16( (uint16_t)macroTriggerMacroPendingListSize );
  1039. print(" : ");
  1040. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  1041. {
  1042. printHex( macroTriggerMacroPendingList[ macro ] );
  1043. print(" ");
  1044. }
  1045. // Show pending result macros
  1046. print( NL );
  1047. info_msg("Pending Result Macros: ");
  1048. printInt16( (uint16_t)macroResultMacroPendingListSize );
  1049. print(" : ");
  1050. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  1051. {
  1052. printHex( macroResultMacroPendingList[ macro ] );
  1053. print(" ");
  1054. }
  1055. // Show available trigger macro indices
  1056. print( NL );
  1057. info_msg("Trigger Macros Range: T0 -> T");
  1058. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  1059. // Show available result macro indices
  1060. print( NL );
  1061. info_msg("Result Macros Range: R0 -> R");
  1062. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  1063. // Show Trigger to Result Macro Links
  1064. print( NL );
  1065. info_msg("Trigger : Result Macro Pairs");
  1066. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  1067. {
  1068. print( NL );
  1069. print("\tT");
  1070. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  1071. print(" : R");
  1072. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  1073. }
  1074. }
  1075. void cliFunc_macroProc( char* args )
  1076. {
  1077. // Toggle macro pause mode
  1078. macroPauseMode = macroPauseMode ? 0 : 1;
  1079. print( NL );
  1080. info_msg("Macro Processing Mode: ");
  1081. printInt8( macroPauseMode );
  1082. }
  1083. void macroDebugShowTrigger( var_uint_t index )
  1084. {
  1085. // Only proceed if the macro exists
  1086. if ( index >= TriggerMacroNum )
  1087. return;
  1088. // Trigger Macro Show
  1089. TriggerMacro *macro = &TriggerMacroList[ index ];
  1090. print( NL );
  1091. info_msg("Trigger Macro Index: ");
  1092. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1093. print( NL );
  1094. // Read the comboLength for combo in the sequence (sequence of combos)
  1095. var_uint_t pos = 0;
  1096. uint8_t comboLength = macro->guide[ pos ];
  1097. // Iterate through and interpret the guide
  1098. while ( comboLength != 0 )
  1099. {
  1100. // Initial position of the combo
  1101. var_uint_t comboPos = ++pos;
  1102. // Iterate through the combo
  1103. while ( pos < comboLength * TriggerGuideSize + comboPos )
  1104. {
  1105. // Assign TriggerGuide element (key type, state and scancode)
  1106. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  1107. // Display guide information about trigger key
  1108. printHex( guide->scanCode );
  1109. print("|");
  1110. printHex( guide->type );
  1111. print("|");
  1112. printHex( guide->state );
  1113. // Increment position
  1114. pos += TriggerGuideSize;
  1115. // Only show combo separator if there are combos left in the sequence element
  1116. if ( pos < comboLength * TriggerGuideSize + comboPos )
  1117. print("+");
  1118. }
  1119. // Read the next comboLength
  1120. comboLength = macro->guide[ pos ];
  1121. // Only show sequence separator if there is another combo to process
  1122. if ( comboLength != 0 )
  1123. print(";");
  1124. }
  1125. // Display current position
  1126. print( NL "Position: " );
  1127. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1128. // Display result macro index
  1129. print( NL "Result Macro Index: " );
  1130. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  1131. // Display trigger macro state
  1132. print( NL "Trigger Macro State: " );
  1133. switch ( macro->state )
  1134. {
  1135. case TriggerMacro_Press: print("Press"); break;
  1136. case TriggerMacro_Release: print("Release"); break;
  1137. case TriggerMacro_Waiting: print("Waiting"); break;
  1138. }
  1139. }
  1140. void macroDebugShowResult( var_uint_t index )
  1141. {
  1142. // Only proceed if the macro exists
  1143. if ( index >= ResultMacroNum )
  1144. return;
  1145. // Trigger Macro Show
  1146. ResultMacro *macro = &ResultMacroList[ index ];
  1147. print( NL );
  1148. info_msg("Result Macro Index: ");
  1149. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1150. print( NL );
  1151. // Read the comboLength for combo in the sequence (sequence of combos)
  1152. var_uint_t pos = 0;
  1153. uint8_t comboLength = macro->guide[ pos++ ];
  1154. // Iterate through and interpret the guide
  1155. while ( comboLength != 0 )
  1156. {
  1157. // Function Counter, used to keep track of the combos processed
  1158. var_uint_t funcCount = 0;
  1159. // Iterate through the combo
  1160. while ( funcCount < comboLength )
  1161. {
  1162. // Assign TriggerGuide element (key type, state and scancode)
  1163. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  1164. // Display Function Index
  1165. printHex( guide->index );
  1166. print("|");
  1167. // Display Function Ptr Address
  1168. printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
  1169. print("|");
  1170. // Display/Lookup Capability Name (utilize debug mode of capability)
  1171. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  1172. capability( 0xFF, 0xFF, 0 );
  1173. // Display Argument(s)
  1174. print("(");
  1175. for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  1176. {
  1177. // Arguments are only 8 bit values
  1178. printHex( (&guide->args)[ arg ] );
  1179. // Only show arg separator if there are args left
  1180. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  1181. print(",");
  1182. }
  1183. print(")");
  1184. // Increment position
  1185. pos += ResultGuideSize( guide );
  1186. // Increment function count
  1187. funcCount++;
  1188. // Only show combo separator if there are combos left in the sequence element
  1189. if ( funcCount < comboLength )
  1190. print("+");
  1191. }
  1192. // Read the next comboLength
  1193. comboLength = macro->guide[ pos++ ];
  1194. // Only show sequence separator if there is another combo to process
  1195. if ( comboLength != 0 )
  1196. print(";");
  1197. }
  1198. // Display current position
  1199. print( NL "Position: " );
  1200. printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1201. // Display final trigger state/type
  1202. print( NL "Final Trigger State (State/Type): " );
  1203. printHex( macro->state );
  1204. print("/");
  1205. printHex( macro->stateType );
  1206. }
  1207. void cliFunc_macroShow( char* args )
  1208. {
  1209. // Parse codes from arguments
  1210. char* curArgs;
  1211. char* arg1Ptr;
  1212. char* arg2Ptr = args;
  1213. // Process all args
  1214. for ( ;; )
  1215. {
  1216. curArgs = arg2Ptr;
  1217. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1218. // Stop processing args if no more are found
  1219. if ( *arg1Ptr == '\0' )
  1220. break;
  1221. // Ignore invalid codes
  1222. switch ( arg1Ptr[0] )
  1223. {
  1224. // Indexed Trigger Macro
  1225. case 'T':
  1226. macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
  1227. break;
  1228. // Indexed Result Macro
  1229. case 'R':
  1230. macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
  1231. break;
  1232. }
  1233. }
  1234. }
  1235. void cliFunc_macroStep( char* args )
  1236. {
  1237. // Parse number from argument
  1238. // NOTE: Only first argument is used
  1239. char* arg1Ptr;
  1240. char* arg2Ptr;
  1241. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  1242. // Default to 1, if no argument given
  1243. var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
  1244. if ( count == 0 )
  1245. count = 1;
  1246. // Set the macro step counter, negative int's are cast to uint
  1247. macroStepCounter = count;
  1248. }