Kiibohd Controller
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <led.h>
  21. #include <print.h>
  22. // Local Includes
  23. #include "trigger.h"
  24. #include "kll.h"
  25. // ----- Enums -----
  26. // Bit positions are important, passes (correct key) always trump incorrect key votes
  27. typedef enum TriggerMacroVote {
  28. TriggerMacroVote_Release = 0x10, // Correct key
  29. TriggerMacroVote_PassRelease = 0x18, // Correct key (both pass and release)
  30. TriggerMacroVote_Pass = 0x8, // Correct key
  31. TriggerMacroVote_DoNothingRelease = 0x4, // Incorrect key
  32. TriggerMacroVote_DoNothing = 0x2, // Incorrect key
  33. TriggerMacroVote_Fail = 0x1, // Incorrect key
  34. TriggerMacroVote_Invalid = 0x0, // Invalid state
  35. } TriggerMacroVote;
  36. typedef enum TriggerMacroEval {
  37. TriggerMacroEval_DoNothing,
  38. TriggerMacroEval_DoResult,
  39. TriggerMacroEval_DoResultAndRemove,
  40. TriggerMacroEval_Remove,
  41. } TriggerMacroEval;
  42. // ----- Generated KLL Variables -----
  43. extern const Capability CapabilitiesList[];
  44. extern const TriggerMacro TriggerMacroList[];
  45. extern TriggerMacroRecord TriggerMacroRecordList[];
  46. extern const ResultMacro ResultMacroList[];
  47. // ----- Variables -----
  48. // Key Trigger List Buffer and Layer Cache
  49. // The layer cache is set on press only, hold and release events refer to the value set on press
  50. extern TriggerGuide macroTriggerListBuffer[];
  51. extern var_uint_t macroTriggerListBufferSize;
  52. extern var_uint_t macroTriggerListLayerCache[];
  53. // Pending Trigger Macro Index List
  54. // * Any trigger macros that need processing from a previous macro processing loop
  55. // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
  56. // Possibly could be calculated by the KLL compiler
  57. // XXX It may be possible to calculate the worst case using the KLL compiler
  58. index_uint_t macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
  59. index_uint_t macroTriggerMacroPendingListSize = 0;
  60. // ----- Protected Macro Functions -----
  61. extern nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire );
  62. extern void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMacro );
  63. // ----- Functions -----
  64. // Determine if long ResultMacro (more than 1 seqence element)
  65. inline uint8_t Macro_isLongResultMacro( const ResultMacro *macro )
  66. {
  67. // Check the second sequence combo length
  68. // If non-zero return non-zero (long sequence)
  69. // 0 otherwise (short sequence)
  70. var_uint_t position = 1;
  71. for ( var_uint_t result = 0; result < macro->guide[0]; result++ )
  72. position += ResultGuideSize( (ResultGuide*)&macro->guide[ position ] );
  73. return macro->guide[ position ];
  74. }
  75. // Determine if long TriggerMacro (more than 1 sequence element)
  76. inline uint8_t Macro_isLongTriggerMacro( const TriggerMacro *macro )
  77. {
  78. // Check the second sequence combo length
  79. // If non-zero return non-zero (long sequence)
  80. // 0 otherwise (short sequence)
  81. return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ];
  82. }
  83. // Votes on the given key vs. guide, short macros
  84. inline TriggerMacroVote Macro_evalShortTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  85. {
  86. // Depending on key type
  87. switch ( guide->type )
  88. {
  89. // Normal State Type
  90. case 0x00:
  91. // For short TriggerMacros completely ignore incorrect keys
  92. if ( guide->scanCode == key->scanCode )
  93. {
  94. switch ( key->state )
  95. {
  96. // Correct key, pressed, possible passing
  97. case 0x01:
  98. return TriggerMacroVote_Pass;
  99. // Correct key, held, possible passing or release
  100. case 0x02:
  101. return TriggerMacroVote_PassRelease;
  102. // Correct key, released, possible release
  103. case 0x03:
  104. return TriggerMacroVote_Release;
  105. }
  106. }
  107. return TriggerMacroVote_DoNothing;
  108. // LED State Type
  109. case 0x01:
  110. erro_print("LED State Type - Not implemented...");
  111. break;
  112. // Analog State Type
  113. case 0x02:
  114. erro_print("Analog State Type - Not implemented...");
  115. break;
  116. // Invalid State Type
  117. default:
  118. erro_print("Invalid State Type. This is a bug.");
  119. break;
  120. }
  121. // XXX Shouldn't reach here
  122. return TriggerMacroVote_Invalid;
  123. }
  124. // Votes on the given key vs. guide, long macros
  125. // A long macro is defined as a guide with more than 1 combo
  126. inline TriggerMacroVote Macro_evalLongTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
  127. {
  128. // Depending on key type
  129. switch ( guide->type )
  130. {
  131. // Normal State Type
  132. case 0x00:
  133. // Depending on the state of the buffered key, make voting decision
  134. // Incorrect key
  135. if ( guide->scanCode != key->scanCode )
  136. {
  137. switch ( key->state )
  138. {
  139. // Wrong key, pressed, fail
  140. case 0x01:
  141. return TriggerMacroVote_Fail;
  142. // Wrong key, held, do not pass (no effect)
  143. case 0x02:
  144. return TriggerMacroVote_DoNothing;
  145. // Wrong key released, fail out if pos == 0
  146. case 0x03:
  147. return TriggerMacroVote_DoNothing | TriggerMacroVote_DoNothingRelease;
  148. }
  149. }
  150. // Correct key
  151. else
  152. {
  153. switch ( key->state )
  154. {
  155. // Correct key, pressed, possible passing
  156. case 0x01:
  157. return TriggerMacroVote_Pass;
  158. // Correct key, held, possible passing or release
  159. case 0x02:
  160. return TriggerMacroVote_PassRelease;
  161. // Correct key, released, possible release
  162. case 0x03:
  163. return TriggerMacroVote_Release;
  164. }
  165. }
  166. break;
  167. // LED State Type
  168. case 0x01:
  169. erro_print("LED State Type - Not implemented...");
  170. break;
  171. // Analog State Type
  172. case 0x02:
  173. erro_print("Analog State Type - Not implemented...");
  174. break;
  175. // Invalid State Type
  176. default:
  177. erro_print("Invalid State Type. This is a bug.");
  178. break;
  179. }
  180. // XXX Shouldn't reach here
  181. return TriggerMacroVote_Invalid;
  182. }
  183. // Evaluate/Update TriggerMacro
  184. TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
  185. {
  186. // Lookup TriggerMacro
  187. const TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
  188. TriggerMacroRecord *record = &TriggerMacroRecordList[ triggerMacroIndex ];
  189. // Check if macro has finished and should be incremented sequence elements
  190. if ( record->state == TriggerMacro_Release )
  191. {
  192. record->state = TriggerMacro_Waiting;
  193. record->pos = record->pos + macro->guide[ record->pos ] * TriggerGuideSize + 1;
  194. }
  195. // Current Macro position
  196. var_uint_t pos = record->pos;
  197. // Length of the combo being processed
  198. uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
  199. // If no combo items are left, remove the TriggerMacro from the pending list
  200. if ( comboLength == 0 )
  201. {
  202. return TriggerMacroEval_Remove;
  203. }
  204. // Check if this is a long Trigger Macro
  205. uint8_t longMacro = Macro_isLongTriggerMacro( macro );
  206. // Iterate through the items in the combo, voting the on the key state
  207. // If any of the pressed keys do not match, fail the macro
  208. //
  209. // The macro is waiting for input when in the TriggerMacro_Waiting state
  210. // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
  211. // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
  212. // On scan after position increment, change to TriggerMacro_Waiting state
  213. // TODO Add support for system LED states (NumLock, CapsLock, etc.)
  214. // TODO Add support for analog key states
  215. // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
  216. // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
  217. TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
  218. for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
  219. {
  220. // Assign TriggerGuide element (key type, state and scancode)
  221. TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
  222. TriggerMacroVote vote = TriggerMacroVote_Invalid;
  223. // Iterate through the key buffer, comparing to each key in the combo
  224. for ( var_uint_t key = 0; key < macroTriggerListBufferSize; key++ )
  225. {
  226. // Lookup key information
  227. TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
  228. // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
  229. // Also mask all of the non-passing votes
  230. vote |= longMacro
  231. ? Macro_evalLongTriggerMacroVote( keyInfo, guide )
  232. : Macro_evalShortTriggerMacroVote( keyInfo, guide );
  233. if ( vote >= TriggerMacroVote_Pass )
  234. {
  235. vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
  236. break;
  237. }
  238. }
  239. // If no pass vote was found after scanning all of the keys
  240. // Fail the combo, if this is a short macro (long macros already will have a fail vote)
  241. if ( !longMacro && vote < TriggerMacroVote_Pass )
  242. vote |= TriggerMacroVote_Fail;
  243. // After voting, append to overall vote
  244. overallVote |= vote;
  245. }
  246. // If no pass vote was found after scanning the entire combo
  247. // And this is the first position in the combo, just remove it (nothing important happened)
  248. if ( longMacro && overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
  249. overallVote |= TriggerMacroVote_Fail;
  250. // Decide new state of macro after voting
  251. // Fail macro, remove from pending list
  252. if ( overallVote & TriggerMacroVote_Fail )
  253. {
  254. return TriggerMacroEval_Remove;
  255. }
  256. // Do nothing, incorrect key is being held or released
  257. else if ( overallVote & TriggerMacroVote_DoNothing && longMacro )
  258. {
  259. // Just doing nothing :)
  260. }
  261. // If ready for transition and in Press state, set to Waiting and increment combo position
  262. // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
  263. else if ( overallVote & TriggerMacroVote_Release && record->state == TriggerMacro_Press )
  264. {
  265. record->state = TriggerMacro_Release;
  266. // If this is the last combo in the sequence, remove from the pending list
  267. if ( macro->guide[ record->pos + macro->guide[ record->pos ] * TriggerGuideSize + 1 ] == 0 )
  268. return TriggerMacroEval_DoResultAndRemove;
  269. }
  270. // If passing and in Waiting state, set macro state to Press
  271. else if ( overallVote & TriggerMacroVote_Pass
  272. && ( record->state == TriggerMacro_Waiting || record->state == TriggerMacro_Press ) )
  273. {
  274. record->state = TriggerMacro_Press;
  275. // If in press state, and this is the final combo, send request for ResultMacro
  276. // Check to see if the result macro only has a single element
  277. // If this result macro has more than 1 key, only send once
  278. // TODO Add option to have long macro repeat rate
  279. if ( macro->guide[ pos + comboLength + 1 ] == 0 )
  280. {
  281. // Long result macro (more than 1 combo)
  282. if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
  283. {
  284. // Only ever trigger result once, on press
  285. if ( overallVote == TriggerMacroVote_Pass )
  286. {
  287. return TriggerMacroEval_DoResultAndRemove;
  288. }
  289. }
  290. // Short result macro
  291. else
  292. {
  293. // Only trigger result once, on press, if long trigger (more than 1 combo)
  294. if ( Macro_isLongTriggerMacro( macro ) )
  295. {
  296. return TriggerMacroEval_DoResultAndRemove;
  297. }
  298. // Otherwise, trigger result continuously
  299. else
  300. {
  301. return TriggerMacroEval_DoResult;
  302. }
  303. }
  304. }
  305. }
  306. // Otherwise, just remove the macro on key release
  307. // One more result has to be called to indicate to the ResultMacro that the key transitioned to the release state
  308. else if ( overallVote & TriggerMacroVote_Release )
  309. {
  310. return TriggerMacroEval_DoResultAndRemove;
  311. }
  312. // If this is a short macro, just remove it
  313. // The state can be rebuilt on the next iteration
  314. if ( !longMacro )
  315. return TriggerMacroEval_Remove;
  316. return TriggerMacroEval_DoNothing;
  317. }
  318. // Update pending trigger list
  319. inline void Macro_updateTriggerMacroPendingList()
  320. {
  321. // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
  322. for ( var_uint_t key = 0; key < macroTriggerListBufferSize; key++ )
  323. {
  324. // TODO LED States
  325. // TODO Analog Switches
  326. // Only add TriggerMacro to pending list if key was pressed (not held, released or off)
  327. if ( macroTriggerListBuffer[ key ].state == 0x00 && macroTriggerListBuffer[ key ].state != 0x01 )
  328. continue;
  329. // TODO Analog
  330. // If this is a release case, indicate to layer lookup for possible latch expiry
  331. uint8_t latch_expire = macroTriggerListBuffer[ key ].state == 0x03;
  332. // Lookup Trigger List
  333. nat_ptr_t *triggerList = Macro_layerLookup( &macroTriggerListBuffer[ key ], latch_expire );
  334. // If there was an error during lookup, skip
  335. if ( triggerList == 0 )
  336. continue;
  337. // Number of Triggers in list
  338. nat_ptr_t triggerListSize = triggerList[0];
  339. // Iterate over triggerList to see if any TriggerMacros need to be added
  340. // First item is the number of items in the TriggerList
  341. for ( var_uint_t macro = 1; macro < triggerListSize + 1; macro++ )
  342. {
  343. // Lookup trigger macro index
  344. var_uint_t triggerMacroIndex = triggerList[ macro ];
  345. // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
  346. // triggerList needs to be added
  347. var_uint_t pending = 0;
  348. for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
  349. {
  350. // Stop scanning if the trigger macro index is found in the pending list
  351. if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
  352. break;
  353. }
  354. // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
  355. // Add it to the list
  356. if ( pending == macroTriggerMacroPendingListSize )
  357. {
  358. macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
  359. // Reset macro position
  360. TriggerMacroRecordList[ triggerMacroIndex ].pos = 0;
  361. TriggerMacroRecordList[ triggerMacroIndex ].state = TriggerMacro_Waiting;
  362. }
  363. }
  364. }
  365. }
  366. void Trigger_state( uint8_t type, uint8_t state, uint8_t index )
  367. {
  368. }
  369. uint8_t Trigger_update( uint8_t type, uint8_t state, uint8_t index )
  370. {
  371. return 0;
  372. }
  373. void Trigger_setup()
  374. {
  375. // Initialize TriggerMacro states
  376. for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
  377. {
  378. TriggerMacroRecordList[ macro ].pos = 0;
  379. TriggerMacroRecordList[ macro ].state = TriggerMacro_Waiting;
  380. }
  381. }
  382. void Trigger_process()
  383. {
  384. // Update pending trigger list, before processing TriggerMacros
  385. Macro_updateTriggerMacroPendingList();
  386. // Tail pointer for macroTriggerMacroPendingList
  387. // Macros must be explicitly re-added
  388. var_uint_t macroTriggerMacroPendingListTail = 0;
  389. // Iterate through the pending TriggerMacros, processing each of them
  390. for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
  391. {
  392. switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
  393. {
  394. // Trigger Result Macro (purposely falling through)
  395. case TriggerMacroEval_DoResult:
  396. // Append ResultMacro to PendingList
  397. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  398. default:
  399. macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
  400. break;
  401. // Trigger Result Macro and Remove (purposely falling through)
  402. case TriggerMacroEval_DoResultAndRemove:
  403. // Append ResultMacro to PendingList
  404. Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
  405. // Remove Macro from Pending List, nothing to do, removing by default
  406. case TriggerMacroEval_Remove:
  407. break;
  408. }
  409. }
  410. // Update the macroTriggerMacroPendingListSize with the tail pointer
  411. macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
  412. }