Kiibohd Controller
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. /* Copyright (C) 2014-2016 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 "trigger.h"
  33. #include "result.h"
  34. #include "macro.h"
  35. // ----- Function Declarations -----
  36. void cliFunc_capList ( char* args );
  37. void cliFunc_capSelect ( char* args );
  38. void cliFunc_keyHold ( char* args );
  39. void cliFunc_keyPress ( char* args );
  40. void cliFunc_keyRelease( char* args );
  41. void cliFunc_layerDebug( char* args );
  42. void cliFunc_layerList ( char* args );
  43. void cliFunc_layerState( char* args );
  44. void cliFunc_macroDebug( char* args );
  45. void cliFunc_macroList ( char* args );
  46. void cliFunc_macroProc ( char* args );
  47. void cliFunc_macroShow ( char* args );
  48. void cliFunc_macroStep ( char* args );
  49. // ----- Variables -----
  50. // Macro Module command dictionary
  51. CLIDict_Entry( capList, "Prints an indexed list of all non USB keycode capabilities." );
  52. CLIDict_Entry( capSelect, "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B" );
  53. CLIDict_Entry( keyHold, "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
  54. CLIDict_Entry( keyPress, "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
  55. CLIDict_Entry( keyRelease, "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
  56. CLIDict_Entry( layerDebug, "Layer debug mode. Shows layer stack and any changes." );
  57. CLIDict_Entry( layerList, "List available layers." );
  58. 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" );
  59. CLIDict_Entry( macroDebug, "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes." );
  60. CLIDict_Entry( macroList, "List the defined trigger and result macros." );
  61. CLIDict_Entry( macroProc, "Pause/Resume macro processing." );
  62. 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" );
  63. CLIDict_Entry( macroStep, "Do N macro processing steps. Defaults to 1." );
  64. CLIDict_Def( macroCLIDict, "Macro Module Commands" ) = {
  65. CLIDict_Item( capList ),
  66. CLIDict_Item( capSelect ),
  67. CLIDict_Item( keyHold ),
  68. CLIDict_Item( keyPress ),
  69. CLIDict_Item( keyRelease ),
  70. CLIDict_Item( layerDebug ),
  71. CLIDict_Item( layerList ),
  72. CLIDict_Item( layerState ),
  73. CLIDict_Item( macroDebug ),
  74. CLIDict_Item( macroList ),
  75. CLIDict_Item( macroProc ),
  76. CLIDict_Item( macroShow ),
  77. CLIDict_Item( macroStep ),
  78. { 0, 0, 0 } // Null entry for dictionary end
  79. };
  80. // Layer debug flag - If set, displays any changes to layers and the full layer stack on change
  81. uint8_t layerDebugMode = 0;
  82. // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
  83. uint8_t macroDebugMode = 0;
  84. // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
  85. uint8_t macroPauseMode = 0;
  86. // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
  87. uint16_t macroStepCounter = 0;
  88. // Key Trigger List Buffer and Layer Cache
  89. // The layer cache is set on press only, hold and release events refer to the value set on press
  90. TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
  91. var_uint_t macroTriggerListBufferSize = 0;
  92. var_uint_t macroTriggerListLayerCache[ MaxScanCode ];
  93. // Layer Index Stack
  94. // * When modifying layer state and the state is non-0x0, the stack must be adjusted
  95. index_uint_t macroLayerIndexStack[ LayerNum + 1 ] = { 0 };
  96. index_uint_t macroLayerIndexStackSize = 0;
  97. // TODO REMOVE when dependency no longer exists
  98. extern index_uint_t macroResultMacroPendingList[];
  99. extern index_uint_t macroResultMacroPendingListSize;
  100. extern index_uint_t macroTriggerMacroPendingList[];
  101. extern index_uint_t macroTriggerMacroPendingListSize;
  102. // Interconnect ScanCode Cache
  103. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  104. // TODO This can be shrunk by the size of the max node 0 ScanCode
  105. TriggerGuide macroInterconnectCache[ MaxScanCode ];
  106. uint8_t macroInterconnectCacheSize = 0;
  107. #endif
  108. // Key blocking buffer
  109. uint8_t macroHidBlockList[ Macro_maxBlockCount_define ];
  110. uint8_t macroHidBlockListSize;
  111. // ----- Capabilities -----
  112. // Sets the given layer with the specified layerState
  113. void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t layerState )
  114. {
  115. // Ignore if layer does not exist or trying to manipulate layer 0/Default layer
  116. if ( layer >= LayerNum || layer == 0 )
  117. return;
  118. // Is layer in the LayerIndexStack?
  119. uint8_t inLayerIndexStack = 0;
  120. uint16_t stackItem = 0;
  121. while ( stackItem < macroLayerIndexStackSize )
  122. {
  123. // Flag if layer is already in the LayerIndexStack
  124. if ( macroLayerIndexStack[ stackItem ] == layer )
  125. {
  126. inLayerIndexStack = 1;
  127. break;
  128. }
  129. // Increment to next item
  130. stackItem++;
  131. }
  132. // Toggle Layer State Byte
  133. if ( LayerState[ layer ] & layerState )
  134. {
  135. // Unset
  136. LayerState[ layer ] &= ~layerState;
  137. }
  138. else
  139. {
  140. // Set
  141. LayerState[ layer ] |= layerState;
  142. }
  143. // If the layer was not in the LayerIndexStack add it
  144. if ( !inLayerIndexStack )
  145. {
  146. macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
  147. }
  148. // If the layer is in the LayerIndexStack and the state is 0x00, remove
  149. if ( LayerState[ layer ] == 0x00 && inLayerIndexStack )
  150. {
  151. // Remove the layer from the LayerIndexStack
  152. // Using the already positioned stackItem variable from the loop above
  153. while ( stackItem < macroLayerIndexStackSize )
  154. {
  155. macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
  156. stackItem++;
  157. }
  158. // Reduce LayerIndexStack size
  159. macroLayerIndexStackSize--;
  160. }
  161. // Layer Debug Mode
  162. if ( layerDebugMode )
  163. {
  164. dbug_msg("Layer ");
  165. // Iterate over each of the layers displaying the state as a hex value
  166. for ( index_uint_t index = 0; index < LayerNum; index++ )
  167. {
  168. printHex_op( LayerState[ index ], 0 );
  169. }
  170. // Always show the default layer (it's always 0)
  171. print(" 0");
  172. // Iterate over the layer stack starting from the bottom of the stack
  173. for ( index_uint_t index = macroLayerIndexStackSize; index > 0; index-- )
  174. {
  175. print(":");
  176. printHex_op( macroLayerIndexStack[ index - 1 ], 0 );
  177. }
  178. print( NL );
  179. }
  180. }
  181. // Modifies the specified Layer control byte
  182. // Argument #1: Layer Index -> uint16_t
  183. // Argument #2: Layer State -> uint8_t
  184. void Macro_layerState_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  185. {
  186. // Display capability name
  187. if ( stateType == 0xFF && state == 0xFF )
  188. {
  189. print("Macro_layerState(layerIndex,layerState)");
  190. return;
  191. }
  192. // Only use capability on press or release
  193. // TODO Analog
  194. // XXX This may cause issues, might be better to implement state table here to decide -HaaTa
  195. if ( stateType == 0x00 && state == 0x02 ) // Hold condition
  196. return;
  197. // Get layer index from arguments
  198. // Cast pointer to uint8_t to uint16_t then access that memory location
  199. uint16_t layer = *(uint16_t*)(&args[0]);
  200. // Get layer toggle byte
  201. uint8_t layerState = args[ sizeof(uint16_t) ];
  202. Macro_layerState( state, stateType, layer, layerState );
  203. }
  204. // Latches given layer
  205. // Argument #1: Layer Index -> uint16_t
  206. void Macro_layerLatch_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_layerLatch(layerIndex)");
  212. return;
  213. }
  214. // Only use capability on press
  215. // TODO Analog
  216. if ( stateType == 0x00 && state != 0x03 ) // Only on release
  217. return;
  218. // Get layer index from arguments
  219. // Cast pointer to uint8_t to uint16_t then access that memory location
  220. uint16_t layer = *(uint16_t*)(&args[0]);
  221. Macro_layerState( state, stateType, layer, 0x02 );
  222. }
  223. // Locks given layer
  224. // Argument #1: Layer Index -> uint16_t
  225. void Macro_layerLock_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  226. {
  227. // Display capability name
  228. if ( stateType == 0xFF && state == 0xFF )
  229. {
  230. print("Macro_layerLock(layerIndex)");
  231. return;
  232. }
  233. // Only use capability on press
  234. // TODO Analog
  235. // XXX Could also be on release, but that's sorta dumb -HaaTa
  236. if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
  237. return;
  238. // Get layer index from arguments
  239. // Cast pointer to uint8_t to uint16_t then access that memory location
  240. uint16_t layer = *(uint16_t*)(&args[0]);
  241. Macro_layerState( state, stateType, layer, 0x04 );
  242. }
  243. // Shifts given layer
  244. // Argument #1: Layer Index -> uint16_t
  245. void Macro_layerShift_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  246. {
  247. // Display capability name
  248. if ( stateType == 0xFF && state == 0xFF )
  249. {
  250. print("Macro_layerShift(layerIndex)");
  251. return;
  252. }
  253. // Only use capability on press or release
  254. // TODO Analog
  255. if ( stateType == 0x00 && ( state == 0x00 || state == 0x02 ) ) // Only pass press or release conditions
  256. return;
  257. // Get layer index from arguments
  258. // Cast pointer to uint8_t to uint16_t then access that memory location
  259. uint16_t layer = *(uint16_t*)(&args[0]);
  260. Macro_layerState( state, stateType, layer, 0x01 );
  261. }
  262. // Rotate layer to next/previous
  263. // Uses state variable to keep track of the current layer position
  264. // Layers are still evaluated using the layer stack
  265. uint16_t Macro_rotationLayer;
  266. void Macro_layerRotate_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  267. {
  268. // Display capability name
  269. if ( stateType == 0xFF && state == 0xFF )
  270. {
  271. print("Macro_layerRotate(previous)");
  272. return;
  273. }
  274. // Only use capability on press
  275. // TODO Analog
  276. // XXX Could also be on release, but that's sorta dumb -HaaTa
  277. if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
  278. return;
  279. // Unset previous rotation layer if not 0
  280. if ( Macro_rotationLayer != 0 )
  281. {
  282. Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
  283. }
  284. // Get direction of rotation, 0, next, non-zero previous
  285. uint8_t direction = *args;
  286. // Next
  287. if ( !direction )
  288. {
  289. Macro_rotationLayer++;
  290. // Invalid layer
  291. if ( Macro_rotationLayer >= LayerNum )
  292. Macro_rotationLayer = 0;
  293. }
  294. // Previous
  295. else
  296. {
  297. Macro_rotationLayer--;
  298. // Layer wrap
  299. if ( Macro_rotationLayer >= LayerNum )
  300. Macro_rotationLayer = LayerNum - 1;
  301. }
  302. // Toggle the computed layer rotation
  303. Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
  304. }
  305. // Block USB Key
  306. // During the next processing cycle, queue up a key to be ignored
  307. // e.g. If Shift is assigned to both Shift and Layer 1
  308. // Then if the 1 key on Layer 1 is assigned 2
  309. // Block the Shift key so a USB 2 can be sent via the Output channel
  310. // This works by having a queue of keys to "unset" if they are triggered during the next processing loop
  311. void Macro_blockUSBKey_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  312. {
  313. // Display capability name
  314. if ( stateType == 0xFF && state == 0xFF )
  315. {
  316. print("Macro_blockUSBKey(usbCode)");
  317. return;
  318. }
  319. // Get usb key from arguments
  320. // Access argument directly as it's already uint8_t
  321. uint8_t usbCode = args[0];
  322. // Add to the hid block list
  323. if ( macroHidBlockListSize < Macro_maxBlockCount_define )
  324. {
  325. macroHidBlockList[ macroHidBlockListSize++ ] = usbCode;
  326. }
  327. else
  328. {
  329. warn_print("USB Key Block buffer is full!: ");
  330. printHex( usbCode );
  331. print( NL );
  332. }
  333. }
  334. // ----- Functions -----
  335. // Looks up the trigger list for the given scan code (from the active layer)
  336. // NOTE: Calling function must handle the NULL pointer case
  337. nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire )
  338. {
  339. uint8_t scanCode = guide->scanCode;
  340. // TODO Analog
  341. // If a normal key, and not pressed, do a layer cache lookup
  342. if ( guide->type == 0x00 && guide->state != 0x01 )
  343. {
  344. // Cached layer
  345. var_uint_t cachedLayer = macroTriggerListLayerCache[ scanCode ];
  346. // Lookup map, then layer
  347. nat_ptr_t **map = (nat_ptr_t**)LayerIndex[ cachedLayer ].triggerMap;
  348. const Layer *layer = &LayerIndex[ cachedLayer ];
  349. // Cache trigger list before attempting to expire latch
  350. nat_ptr_t *trigger_list = map[ scanCode - layer->first ];
  351. // Check if latch has been pressed for this layer
  352. uint8_t latch = LayerState[ cachedLayer ] & 0x02;
  353. if ( latch && latch_expire )
  354. {
  355. Macro_layerState( 0, 0, cachedLayer, 0x02 );
  356. #if defined(ConnectEnabled_define) && defined(LCDEnabled_define)
  357. // Evaluate the layerStack capability if available (LCD + Interconnect)
  358. extern void LCD_layerStack_capability( uint8_t state, uint8_t stateType, uint8_t *args );
  359. LCD_layerStack_capability( 0, 0, 0 );
  360. #endif
  361. }
  362. return trigger_list;
  363. }
  364. // If no trigger macro is defined at the given layer, fallthrough to the next layer
  365. for ( uint16_t layerIndex = macroLayerIndexStackSize; layerIndex != 0xFFFF; layerIndex-- )
  366. {
  367. // Lookup Layer
  368. const Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
  369. // Check if latch has been pressed for this layer
  370. // XXX Regardless of whether a key is found, the latch is removed on first lookup
  371. uint8_t latch = LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x02;
  372. if ( latch && latch_expire )
  373. {
  374. Macro_layerState( 0, 0, macroLayerIndexStack[ layerIndex ], 0x02 );
  375. }
  376. // Only use layer, if state is valid
  377. // XOR each of the state bits
  378. // If only two are enabled, do not use this state
  379. if ( (LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x01) ^ (latch>>1) ^ ((LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x04)>>2) )
  380. {
  381. // Lookup layer
  382. nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
  383. // Determine if layer has key defined
  384. // Make sure scanCode is between layer first and last scancodes
  385. if ( map != 0
  386. && scanCode <= layer->last
  387. && scanCode >= layer->first
  388. && *map[ scanCode - layer->first ] != 0 )
  389. {
  390. // Set the layer cache
  391. macroTriggerListLayerCache[ scanCode ] = macroLayerIndexStack[ layerIndex ];
  392. return map[ scanCode - layer->first ];
  393. }
  394. }
  395. }
  396. // Do lookup on default layer
  397. nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
  398. // Lookup default layer
  399. const Layer *layer = &LayerIndex[0];
  400. // Make sure scanCode is between layer first and last scancodes
  401. if ( map != 0
  402. && scanCode <= layer->last
  403. && scanCode >= layer->first
  404. && *map[ scanCode - layer->first ] != 0 )
  405. {
  406. // Set the layer cache to default map
  407. macroTriggerListLayerCache[ scanCode ] = 0;
  408. return map[ scanCode - layer->first ];
  409. }
  410. // Otherwise no defined Trigger Macro
  411. erro_msg("Scan Code has no defined Trigger Macro: ");
  412. printHex( scanCode );
  413. print( NL );
  414. return 0;
  415. }
  416. // Add an interconnect ScanCode
  417. // These are handled differently (less information is sent, hold/off states must be assumed)
  418. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  419. void Macro_pressReleaseAdd( void *trigger_ptr )
  420. {
  421. TriggerGuide *trigger = (TriggerGuide*)trigger_ptr;
  422. // Error checking
  423. uint8_t error = 0;
  424. switch ( trigger->type )
  425. {
  426. case 0x00: // Normal key
  427. switch ( trigger->state )
  428. {
  429. case 0x00:
  430. case 0x01:
  431. case 0x02:
  432. case 0x03:
  433. break;
  434. default:
  435. erro_msg("Invalid key state - ");
  436. error = 1;
  437. break;
  438. }
  439. break;
  440. // Invalid TriggerGuide type
  441. default:
  442. erro_msg("Invalid type - ");
  443. error = 1;
  444. break;
  445. }
  446. // Check if ScanCode is out of range
  447. if ( trigger->scanCode > MaxScanCode )
  448. {
  449. warn_msg("ScanCode is out of range/not defined - ");
  450. error = 1;
  451. }
  452. // Display TriggerGuide
  453. if ( error )
  454. {
  455. printHex( trigger->type );
  456. print(" ");
  457. printHex( trigger->state );
  458. print(" ");
  459. printHex( trigger->scanCode );
  460. print( NL );
  461. return;
  462. }
  463. // Add trigger to the Interconnect Cache
  464. // During each processing loop, a scancode may be re-added depending on it's state
  465. for ( var_uint_t c = 0; c < macroInterconnectCacheSize; c++ )
  466. {
  467. // Check if the same ScanCode
  468. if ( macroInterconnectCache[ c ].scanCode == trigger->scanCode )
  469. {
  470. // Update the state
  471. macroInterconnectCache[ c ].state = trigger->state;
  472. return;
  473. }
  474. }
  475. // If not in the list, add it
  476. macroInterconnectCache[ macroInterconnectCacheSize++ ] = *trigger;
  477. }
  478. #endif
  479. // Update the scancode key state
  480. // States:
  481. // * 0x00 - Off
  482. // * 0x01 - Pressed
  483. // * 0x02 - Held
  484. // * 0x03 - Released
  485. // * 0x04 - Unpressed (this is currently ignored)
  486. inline void Macro_keyState( uint8_t scanCode, uint8_t state )
  487. {
  488. #if defined(ConnectEnabled_define)
  489. // Only compile in if a Connect node module is available
  490. if ( !Connect_master )
  491. {
  492. // ScanCodes are only added if there was a state change (on/off)
  493. switch ( state )
  494. {
  495. case 0x00: // Off
  496. case 0x02: // Held
  497. return;
  498. }
  499. }
  500. #endif
  501. // Only add to macro trigger list if one of three states
  502. switch ( state )
  503. {
  504. case 0x01: // Pressed
  505. case 0x02: // Held
  506. case 0x03: // Released
  507. // Check if ScanCode is out of range
  508. if ( scanCode > MaxScanCode )
  509. {
  510. warn_msg("ScanCode is out of range/not defined: ");
  511. printHex( scanCode );
  512. print( NL );
  513. return;
  514. }
  515. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  516. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  517. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
  518. macroTriggerListBufferSize++;
  519. break;
  520. }
  521. }
  522. // Update the scancode analog state
  523. // States:
  524. // * 0x00 - Off
  525. // * 0x01 - Released
  526. // * 0x02-0xFF - Analog value (low to high)
  527. inline void Macro_analogState( uint8_t scanCode, uint8_t state )
  528. {
  529. // Only add to macro trigger list if non-off
  530. // TODO Handle change for interconnect
  531. if ( state != 0x00 )
  532. {
  533. // Check if ScanCode is out of range
  534. if ( scanCode > MaxScanCode )
  535. {
  536. warn_msg("ScanCode is out of range/not defined: ");
  537. printHex( scanCode );
  538. print( NL );
  539. return;
  540. }
  541. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
  542. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  543. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
  544. macroTriggerListBufferSize++;
  545. }
  546. }
  547. // Update led state
  548. // States:
  549. // * 0x00 - Off
  550. // * 0x01 - On
  551. inline void Macro_ledState( uint8_t ledCode, uint8_t state )
  552. {
  553. // Only add to macro trigger list if non-off
  554. // TODO Handle change for interconnect
  555. if ( state != 0x00 )
  556. {
  557. // Check if LedCode is out of range
  558. // TODO
  559. macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
  560. macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
  561. macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
  562. macroTriggerListBufferSize++;
  563. }
  564. }
  565. // Append result macro to pending list, checking for duplicates
  566. // Do nothing if duplicate
  567. void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMacro )
  568. {
  569. // Lookup result macro index
  570. var_uint_t resultMacroIndex = triggerMacro->result;
  571. // Iterate through result macro pending list, making sure this macro hasn't been added yet
  572. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  573. {
  574. // If duplicate found, do nothing
  575. if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
  576. return;
  577. }
  578. // No duplicates found, add to pending list
  579. macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
  580. // Lookup scanCode of the last key in the last combo
  581. var_uint_t pos = 0;
  582. for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
  583. {
  584. pos += TriggerGuideSize * comboLength + 1;
  585. comboLength = triggerMacro->guide[ pos ];
  586. }
  587. uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
  588. // Lookup scanCode in buffer list for the current state and stateType
  589. for ( var_uint_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
  590. {
  591. if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
  592. {
  593. ResultMacroRecordList[ resultMacroIndex ].state = macroTriggerListBuffer[ keyIndex ].state;
  594. ResultMacroRecordList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
  595. }
  596. }
  597. // Reset the macro position
  598. ResultMacroRecordList[ resultMacroIndex ].pos = 0;
  599. }
  600. // Block any of the keys that may be in the buffer
  601. // These keys may not be pressed during the processing loop, but block them anyways
  602. // See Macro_blockUSBKey_capability for more details on usage
  603. inline void Macro_processKeyBlocking()
  604. {
  605. // Iterate over list of USB keys
  606. for ( uint8_t key = 0; key < Macro_maxBlockCount_define; key++ )
  607. {
  608. // This capability will always unset (doesn't toggle)
  609. // First we need to generate the argument
  610. uint8_t args[] = { macroHidBlockList[ key ] };
  611. // XXX Only handles normal keys (no analog, yet)
  612. // 0x03 is release, which always unsets a key from the USB buffer, even if it's not there
  613. Output_usbCodeSend_capability( 0x03, 0x00, args );
  614. }
  615. }
  616. // Macro Procesing Loop
  617. // Called once per USB buffer send
  618. inline void Macro_process()
  619. {
  620. #if defined(ConnectEnabled_define)
  621. // Only compile in if a Connect node module is available
  622. // If this is a interconnect slave node, send all scancodes to master node
  623. if ( !Connect_master )
  624. {
  625. if ( macroTriggerListBufferSize > 0 )
  626. {
  627. Connect_send_ScanCode( Connect_id, macroTriggerListBuffer, macroTriggerListBufferSize );
  628. macroTriggerListBufferSize = 0;
  629. }
  630. return;
  631. }
  632. #endif
  633. // Only do one round of macro processing between Output Module timer sends
  634. if ( USBKeys_Sent != 0 )
  635. return;
  636. #if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
  637. #if defined(ConnectEnabled_define)
  638. // Check if there are any ScanCodes in the interconnect cache to process
  639. if ( Connect_master && macroInterconnectCacheSize > 0 )
  640. #endif
  641. {
  642. // Iterate over all the cache ScanCodes
  643. uint8_t currentInterconnectCacheSize = macroInterconnectCacheSize;
  644. macroInterconnectCacheSize = 0;
  645. for ( uint8_t c = 0; c < currentInterconnectCacheSize; c++ )
  646. {
  647. // Add to the trigger list
  648. macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = macroInterconnectCache[ c ];
  649. // TODO Handle other TriggerGuide types (e.g. analog)
  650. switch ( macroInterconnectCache[ c ].type )
  651. {
  652. // Normal (Press/Hold/Release)
  653. case 0x00:
  654. // Decide what to do based on the current state
  655. switch ( macroInterconnectCache[ c ].state )
  656. {
  657. // Re-add to interconnect cache in hold state
  658. case 0x01: // Press
  659. //case 0x02: // Hold // XXX Why does this not work? -HaaTa
  660. macroInterconnectCache[ c ].state = 0x02;
  661. macroInterconnectCache[ macroInterconnectCacheSize++ ] = macroInterconnectCache[ c ];
  662. break;
  663. case 0x03: // Remove
  664. break;
  665. // Otherwise, do not re-add
  666. }
  667. }
  668. }
  669. }
  670. #endif
  671. // If the pause flag is set, only process if the step counter is non-zero
  672. if ( macroPauseMode )
  673. {
  674. if ( macroStepCounter == 0 )
  675. return;
  676. // Proceed, decrementing the step counter
  677. macroStepCounter--;
  678. dbug_print("Macro Step");
  679. }
  680. // Process Trigger Macros
  681. Trigger_process();
  682. // Process result macros
  683. Result_process();
  684. // Process Key Blocking
  685. Macro_processKeyBlocking();
  686. // Signal buffer that we've used it
  687. Scan_finishedWithMacro( macroTriggerListBufferSize );
  688. // Reset TriggerList buffer
  689. macroTriggerListBufferSize = 0;
  690. // If Macro debug mode is set, clear the USB Buffer
  691. if ( macroDebugMode )
  692. {
  693. USBKeys_Modifiers = 0;
  694. USBKeys_Sent = 0;
  695. }
  696. }
  697. inline void Macro_setup()
  698. {
  699. // Register Macro CLI dictionary
  700. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  701. // Disable Macro debug mode
  702. macroDebugMode = 0;
  703. // Disable Macro pause flag
  704. macroPauseMode = 0;
  705. // Set Macro step counter to zero
  706. macroStepCounter = 0;
  707. // Make sure macro trigger buffer is empty
  708. macroTriggerListBufferSize = 0;
  709. // Set the current rotated layer to 0
  710. Macro_rotationLayer = 0;
  711. // Setup Triggers
  712. Trigger_setup();
  713. // Setup Results
  714. Result_setup();
  715. }
  716. // ----- CLI Command Functions -----
  717. void cliFunc_capList( char* args )
  718. {
  719. print( NL );
  720. info_msg("Capabilities List ");
  721. printHex( CapabilitiesNum );
  722. // Iterate through all of the capabilities and display them
  723. for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
  724. {
  725. print( NL "\t" );
  726. printHex( cap );
  727. print(" - ");
  728. // Display/Lookup Capability Name (utilize debug mode of capability)
  729. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  730. capability( 0xFF, 0xFF, 0 );
  731. }
  732. }
  733. void cliFunc_capSelect( char* args )
  734. {
  735. // Parse code from argument
  736. char* curArgs;
  737. char* arg1Ptr;
  738. char* arg2Ptr = args;
  739. // Total number of args to scan (must do a lookup if a keyboard capability is selected)
  740. var_uint_t totalArgs = 2; // Always at least two args
  741. var_uint_t cap = 0;
  742. // Arguments used for keyboard capability function
  743. var_uint_t argSetCount = 0;
  744. uint8_t *argSet = (uint8_t*)args;
  745. // Process all args
  746. for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
  747. {
  748. curArgs = arg2Ptr;
  749. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  750. // Stop processing args if no more are found
  751. // Extra arguments are ignored
  752. if ( *arg1Ptr == '\0' )
  753. break;
  754. // For the first argument, choose the capability
  755. if ( c == 0 ) switch ( arg1Ptr[0] )
  756. {
  757. // Keyboard Capability
  758. case 'K':
  759. // Determine capability index
  760. cap = numToInt( &arg1Ptr[1] );
  761. // Lookup the number of args
  762. totalArgs += CapabilitiesList[ cap ].argCount;
  763. continue;
  764. }
  765. // Because allocating memory isn't doable, and the argument count is arbitrary
  766. // The argument pointer is repurposed as the argument list (much smaller anyways)
  767. argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
  768. // Once all the arguments are prepared, call the keyboard capability function
  769. if ( argSetCount == totalArgs )
  770. {
  771. // Indicate that the capability was called
  772. print( NL );
  773. info_msg("K");
  774. printInt8( cap );
  775. print(" - ");
  776. printHex( argSet[0] );
  777. print(" - ");
  778. printHex( argSet[1] );
  779. print(" - ");
  780. printHex( argSet[2] );
  781. print( "..." NL );
  782. // Make sure this isn't the reload capability
  783. // If it is, and the remote reflash define is not set, ignore
  784. if ( flashModeEnabled_define == 0 ) for ( uint32_t cap = 0; cap < CapabilitiesNum; cap++ )
  785. {
  786. if ( CapabilitiesList[ cap ].func == (const void*)Output_flashMode_capability )
  787. {
  788. print( NL );
  789. warn_print("flashModeEnabled not set, cancelling firmware reload...");
  790. info_msg("Set flashModeEnabled to 1 in your kll configuration.");
  791. return;
  792. }
  793. }
  794. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
  795. capability( argSet[0], argSet[1], &argSet[2] );
  796. }
  797. }
  798. }
  799. void cliFunc_keyHold( char* args )
  800. {
  801. // Parse codes from arguments
  802. char* curArgs;
  803. char* arg1Ptr;
  804. char* arg2Ptr = args;
  805. // Process all args
  806. for ( ;; )
  807. {
  808. curArgs = arg2Ptr;
  809. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  810. // Stop processing args if no more are found
  811. if ( *arg1Ptr == '\0' )
  812. break;
  813. // Ignore non-Scancode numbers
  814. switch ( arg1Ptr[0] )
  815. {
  816. // Scancode
  817. case 'S':
  818. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
  819. break;
  820. }
  821. }
  822. }
  823. void cliFunc_keyPress( char* args )
  824. {
  825. // Parse codes from arguments
  826. char* curArgs;
  827. char* arg1Ptr;
  828. char* arg2Ptr = args;
  829. // Process all args
  830. for ( ;; )
  831. {
  832. curArgs = arg2Ptr;
  833. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  834. // Stop processing args if no more are found
  835. if ( *arg1Ptr == '\0' )
  836. break;
  837. // Ignore non-Scancode numbers
  838. switch ( arg1Ptr[0] )
  839. {
  840. // Scancode
  841. case 'S':
  842. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
  843. break;
  844. }
  845. }
  846. }
  847. void cliFunc_keyRelease( char* args )
  848. {
  849. // Parse codes from arguments
  850. char* curArgs;
  851. char* arg1Ptr;
  852. char* arg2Ptr = args;
  853. // Process all args
  854. for ( ;; )
  855. {
  856. curArgs = arg2Ptr;
  857. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  858. // Stop processing args if no more are found
  859. if ( *arg1Ptr == '\0' )
  860. break;
  861. // Ignore non-Scancode numbers
  862. switch ( arg1Ptr[0] )
  863. {
  864. // Scancode
  865. case 'S':
  866. Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
  867. break;
  868. }
  869. }
  870. }
  871. void cliFunc_layerDebug( char *args )
  872. {
  873. // Toggle layer debug mode
  874. layerDebugMode = layerDebugMode ? 0 : 1;
  875. print( NL );
  876. info_msg("Layer Debug Mode: ");
  877. printInt8( layerDebugMode );
  878. }
  879. void cliFunc_layerList( char* args )
  880. {
  881. print( NL );
  882. info_msg("Layer List");
  883. // Iterate through all of the layers and display them
  884. for ( uint16_t layer = 0; layer < LayerNum; layer++ )
  885. {
  886. print( NL "\t" );
  887. printHex( layer );
  888. print(" - ");
  889. // Display layer name
  890. dPrint( (char*)LayerIndex[ layer ].name );
  891. // Default map
  892. if ( layer == 0 )
  893. print(" \033[1m(default)\033[0m");
  894. // Layer State
  895. print( NL "\t\t Layer State: " );
  896. printHex( LayerState[ layer ] );
  897. // First -> Last Indices
  898. print(" First -> Last Indices: ");
  899. printHex( LayerIndex[ layer ].first );
  900. print(" -> ");
  901. printHex( LayerIndex[ layer ].last );
  902. }
  903. }
  904. void cliFunc_layerState( char* args )
  905. {
  906. // Parse codes from arguments
  907. char* curArgs;
  908. char* arg1Ptr;
  909. char* arg2Ptr = args;
  910. uint8_t arg1 = 0;
  911. uint8_t arg2 = 0;
  912. // Process first two args
  913. for ( uint8_t c = 0; c < 2; c++ )
  914. {
  915. curArgs = arg2Ptr;
  916. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  917. // Stop processing args if no more are found
  918. if ( *arg1Ptr == '\0' )
  919. break;
  920. switch ( c )
  921. {
  922. // First argument (e.g. L1)
  923. case 0:
  924. if ( arg1Ptr[0] != 'L' )
  925. return;
  926. arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
  927. break;
  928. // Second argument (e.g. 4)
  929. case 1:
  930. arg2 = (uint8_t)numToInt( arg1Ptr );
  931. // Display operation (to indicate that it worked)
  932. print( NL );
  933. info_msg("Setting Layer L");
  934. printInt8( arg1 );
  935. print(" to - ");
  936. printHex( arg2 );
  937. // Set the layer state
  938. LayerState[ arg1 ] = arg2;
  939. break;
  940. }
  941. }
  942. }
  943. void cliFunc_macroDebug( char* args )
  944. {
  945. // Toggle macro debug mode
  946. macroDebugMode = macroDebugMode ? 0 : 1;
  947. print( NL );
  948. info_msg("Macro Debug Mode: ");
  949. printInt8( macroDebugMode );
  950. }
  951. void cliFunc_macroList( char* args )
  952. {
  953. // Show pending key events
  954. print( NL );
  955. info_msg("Pending Key Events: ");
  956. printInt16( (uint16_t)macroTriggerListBufferSize );
  957. print(" : ");
  958. for ( var_uint_t key = 0; key < macroTriggerListBufferSize; key++ )
  959. {
  960. printHex( macroTriggerListBuffer[ key ].scanCode );
  961. print(" ");
  962. }
  963. // Show pending trigger macros
  964. print( NL );
  965. info_msg("Pending Trigger Macros: ");
  966. printInt16( (uint16_t)macroTriggerMacroPendingListSize );
  967. print(" : ");
  968. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  969. {
  970. printHex( macroTriggerMacroPendingList[ macro ] );
  971. print(" ");
  972. }
  973. // Show pending result macros
  974. print( NL );
  975. info_msg("Pending Result Macros: ");
  976. printInt16( (uint16_t)macroResultMacroPendingListSize );
  977. print(" : ");
  978. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  979. {
  980. printHex( macroResultMacroPendingList[ macro ] );
  981. print(" ");
  982. }
  983. // Show available trigger macro indices
  984. print( NL );
  985. info_msg("Trigger Macros Range: T0 -> T");
  986. printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  987. // Show available result macro indices
  988. print( NL );
  989. info_msg("Result Macros Range: R0 -> R");
  990. printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
  991. // Show Trigger to Result Macro Links
  992. print( NL );
  993. info_msg("Trigger : Result Macro Pairs");
  994. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  995. {
  996. print( NL );
  997. print("\tT");
  998. printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
  999. print(" : R");
  1000. printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
  1001. }
  1002. }
  1003. void cliFunc_macroProc( char* args )
  1004. {
  1005. // Toggle macro pause mode
  1006. macroPauseMode = macroPauseMode ? 0 : 1;
  1007. print( NL );
  1008. info_msg("Macro Processing Mode: ");
  1009. printInt8( macroPauseMode );
  1010. }
  1011. void macroDebugShowTrigger( var_uint_t index )
  1012. {
  1013. // Only proceed if the macro exists
  1014. if ( index >= TriggerMacroNum )
  1015. return;
  1016. // Trigger Macro Show
  1017. const TriggerMacro *macro = &TriggerMacroList[ index ];
  1018. TriggerMacroRecord *record = &TriggerMacroRecordList[ index ];
  1019. print( NL );
  1020. info_msg("Trigger Macro Index: ");
  1021. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1022. print( NL );
  1023. // Read the comboLength for combo in the sequence (sequence of combos)
  1024. var_uint_t pos = 0;
  1025. uint8_t comboLength = macro->guide[ pos ];
  1026. // Iterate through and interpret the guide
  1027. while ( comboLength != 0 )
  1028. {
  1029. // Initial position of the combo
  1030. var_uint_t comboPos = ++pos;
  1031. // Iterate through the combo
  1032. while ( pos < comboLength * TriggerGuideSize + comboPos )
  1033. {
  1034. // Assign TriggerGuide element (key type, state and scancode)
  1035. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
  1036. // Display guide information about trigger key
  1037. printHex( guide->scanCode );
  1038. print("|");
  1039. printHex( guide->type );
  1040. print("|");
  1041. printHex( guide->state );
  1042. // Increment position
  1043. pos += TriggerGuideSize;
  1044. // Only show combo separator if there are combos left in the sequence element
  1045. if ( pos < comboLength * TriggerGuideSize + comboPos )
  1046. print("+");
  1047. }
  1048. // Read the next comboLength
  1049. comboLength = macro->guide[ pos ];
  1050. // Only show sequence separator if there is another combo to process
  1051. if ( comboLength != 0 )
  1052. print(";");
  1053. }
  1054. // Display current position
  1055. print( NL "Position: " );
  1056. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1057. // Display result macro index
  1058. print( NL "Result Macro Index: " );
  1059. printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
  1060. // Display trigger macro state
  1061. print( NL "Trigger Macro State: " );
  1062. switch ( record->state )
  1063. {
  1064. case TriggerMacro_Press: print("Press"); break;
  1065. case TriggerMacro_Release: print("Release"); break;
  1066. case TriggerMacro_Waiting: print("Waiting"); break;
  1067. }
  1068. }
  1069. void macroDebugShowResult( var_uint_t index )
  1070. {
  1071. // Only proceed if the macro exists
  1072. if ( index >= ResultMacroNum )
  1073. return;
  1074. // Trigger Macro Show
  1075. const ResultMacro *macro = &ResultMacroList[ index ];
  1076. ResultMacroRecord *record = &ResultMacroRecordList[ index ];
  1077. print( NL );
  1078. info_msg("Result Macro Index: ");
  1079. printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
  1080. print( NL );
  1081. // Read the comboLength for combo in the sequence (sequence of combos)
  1082. var_uint_t pos = 0;
  1083. uint8_t comboLength = macro->guide[ pos++ ];
  1084. // Iterate through and interpret the guide
  1085. while ( comboLength != 0 )
  1086. {
  1087. // Function Counter, used to keep track of the combos processed
  1088. var_uint_t funcCount = 0;
  1089. // Iterate through the combo
  1090. while ( funcCount < comboLength )
  1091. {
  1092. // Assign TriggerGuide element (key type, state and scancode)
  1093. ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
  1094. // Display Function Index
  1095. printHex( guide->index );
  1096. print("|");
  1097. // Display Function Ptr Address
  1098. printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
  1099. print("|");
  1100. // Display/Lookup Capability Name (utilize debug mode of capability)
  1101. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  1102. capability( 0xFF, 0xFF, 0 );
  1103. // Display Argument(s)
  1104. print("(");
  1105. for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
  1106. {
  1107. // Arguments are only 8 bit values
  1108. printHex( (&guide->args)[ arg ] );
  1109. // Only show arg separator if there are args left
  1110. if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
  1111. print(",");
  1112. }
  1113. print(")");
  1114. // Increment position
  1115. pos += ResultGuideSize( guide );
  1116. // Increment function count
  1117. funcCount++;
  1118. // Only show combo separator if there are combos left in the sequence element
  1119. if ( funcCount < comboLength )
  1120. print("+");
  1121. }
  1122. // Read the next comboLength
  1123. comboLength = macro->guide[ pos++ ];
  1124. // Only show sequence separator if there is another combo to process
  1125. if ( comboLength != 0 )
  1126. print(";");
  1127. }
  1128. // Display current position
  1129. print( NL "Position: " );
  1130. printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
  1131. // Display final trigger state/type
  1132. print( NL "Final Trigger State (State/Type): " );
  1133. printHex( record->state );
  1134. print("/");
  1135. printHex( record->stateType );
  1136. }
  1137. void cliFunc_macroShow( char* args )
  1138. {
  1139. // Parse codes from arguments
  1140. char* curArgs;
  1141. char* arg1Ptr;
  1142. char* arg2Ptr = args;
  1143. // Process all args
  1144. for ( ;; )
  1145. {
  1146. curArgs = arg2Ptr;
  1147. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  1148. // Stop processing args if no more are found
  1149. if ( *arg1Ptr == '\0' )
  1150. break;
  1151. // Ignore invalid codes
  1152. switch ( arg1Ptr[0] )
  1153. {
  1154. // Indexed Trigger Macro
  1155. case 'T':
  1156. macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
  1157. break;
  1158. // Indexed Result Macro
  1159. case 'R':
  1160. macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. void cliFunc_macroStep( char* args )
  1166. {
  1167. // Parse number from argument
  1168. // NOTE: Only first argument is used
  1169. char* arg1Ptr;
  1170. char* arg2Ptr;
  1171. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  1172. // Default to 1, if no argument given
  1173. var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
  1174. if ( count == 0 )
  1175. count = 1;
  1176. // Set the macro step counter, negative int's are cast to uint
  1177. macroStepCounter = count;
  1178. }