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.

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