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

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