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.

result.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "result.h"
  24. #include "kll.h"
  25. // ----- Enums -----
  26. typedef enum ResultMacroEval {
  27. ResultMacroEval_DoNothing,
  28. ResultMacroEval_Remove,
  29. } ResultMacroEval;
  30. // ----- KLL Generated Variables -----
  31. extern const Capability CapabilitiesList[];
  32. extern const ResultMacro ResultMacroList[];
  33. extern ResultMacroRecord ResultMacroRecordList[];
  34. // ----- Variables -----
  35. // Pending Result Macro Index List
  36. // * Any result macro that needs processing from a previous macro processing loop
  37. index_uint_t macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
  38. index_uint_t macroResultMacroPendingListSize = 0;
  39. // ----- Functions -----
  40. // Evaluate/Update ResultMacro
  41. inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex )
  42. {
  43. // Lookup ResultMacro
  44. const ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
  45. ResultMacroRecord *record = &ResultMacroRecordList[ resultMacroIndex ];
  46. // Current Macro position
  47. var_uint_t pos = record->pos;
  48. // Length of combo being processed
  49. uint8_t comboLength = macro->guide[ pos ];
  50. // Function Counter, used to keep track of the combo items processed
  51. var_uint_t funcCount = 0;
  52. // Combo Item Position within the guide
  53. var_uint_t comboItem = pos + 1;
  54. // Iterate through the Result Combo
  55. while ( funcCount < comboLength )
  56. {
  57. // Assign TriggerGuide element (key type, state and scancode)
  58. ResultGuide *guide = (ResultGuide*)(&macro->guide[ comboItem ]);
  59. // Do lookup on capability function
  60. void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
  61. // Call capability
  62. capability( record->state, record->stateType, &guide->args );
  63. // Increment counters
  64. funcCount++;
  65. comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
  66. }
  67. // Move to next item in the sequence
  68. record->pos = comboItem;
  69. // If the ResultMacro is finished, remove
  70. if ( macro->guide[ comboItem ] == 0 )
  71. {
  72. record->pos = 0;
  73. return ResultMacroEval_Remove;
  74. }
  75. // Otherwise leave the macro in the list
  76. return ResultMacroEval_DoNothing;
  77. }
  78. void Result_add( uint32_t index )
  79. {
  80. }
  81. void Result_setup()
  82. {
  83. // Initialize ResultMacro states
  84. for ( var_uint_t macro = 0; macro < ResultMacroNum; macro++ )
  85. {
  86. ResultMacroRecordList[ macro ].pos = 0;
  87. ResultMacroRecordList[ macro ].state = 0;
  88. ResultMacroRecordList[ macro ].stateType = 0;
  89. }
  90. }
  91. void Result_process()
  92. {
  93. // Tail pointer for macroResultMacroPendingList
  94. // Macros must be explicitly re-added
  95. var_uint_t macroResultMacroPendingListTail = 0;
  96. // Iterate through the pending ResultMacros, processing each of them
  97. for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
  98. {
  99. switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
  100. {
  101. // Re-add macros to pending list
  102. case ResultMacroEval_DoNothing:
  103. default:
  104. macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
  105. break;
  106. // Remove Macro from Pending List, nothing to do, removing by default
  107. case ResultMacroEval_Remove:
  108. break;
  109. }
  110. }
  111. // Update the macroResultMacroPendingListSize with the tail pointer
  112. macroResultMacroPendingListSize = macroResultMacroPendingListTail;
  113. }