comparison Macro/PartialMap/result.c @ 433:0f7a6b593dc4

Initial refactoring of PartialMap for supporting custom Triggers - Requires a recent KLL - Functionality wise, nothing has changed
author Jacob Alexander <haata@kiibohd.com>
date Sun, 08 May 2016 18:50:28 -0700
parents
children
comparison
equal deleted inserted replaced
432:1a2fb67b0237 433:0f7a6b593dc4
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
17 // ----- Includes -----
18
19 // Compiler Includes
20 #include <Lib/MacroLib.h>
21
22 // Project Includes
23 #include <led.h>
24 #include <print.h>
25
26 // Local Includes
27 #include "result.h"
28 #include "kll.h"
29
30
31
32 // ----- Enums -----
33
34 typedef enum ResultMacroEval {
35 ResultMacroEval_DoNothing,
36 ResultMacroEval_Remove,
37 } ResultMacroEval;
38
39
40
41
42 // ----- KLL Generated Variables -----
43
44 extern const Capability CapabilitiesList[];
45
46 extern const ResultMacro ResultMacroList[];
47 extern ResultMacroRecord ResultMacroRecordList[];
48
49
50
51 // ----- Variables -----
52
53 // Pending Result Macro Index List
54 // * Any result macro that needs processing from a previous macro processing loop
55 index_uint_t macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
56 index_uint_t macroResultMacroPendingListSize = 0;
57
58
59
60 // ----- Functions -----
61
62 // Evaluate/Update ResultMacro
63 inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex )
64 {
65 // Lookup ResultMacro
66 const ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
67 ResultMacroRecord *record = &ResultMacroRecordList[ resultMacroIndex ];
68
69 // Current Macro position
70 var_uint_t pos = record->pos;
71
72 // Length of combo being processed
73 uint8_t comboLength = macro->guide[ pos ];
74
75 // Function Counter, used to keep track of the combo items processed
76 var_uint_t funcCount = 0;
77
78 // Combo Item Position within the guide
79 var_uint_t comboItem = pos + 1;
80
81 // Iterate through the Result Combo
82 while ( funcCount < comboLength )
83 {
84 // Assign TriggerGuide element (key type, state and scancode)
85 ResultGuide *guide = (ResultGuide*)(&macro->guide[ comboItem ]);
86
87 // Do lookup on capability function
88 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
89
90 // Call capability
91 capability( record->state, record->stateType, &guide->args );
92
93 // Increment counters
94 funcCount++;
95 comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
96 }
97
98 // Move to next item in the sequence
99 record->pos = comboItem;
100
101 // If the ResultMacro is finished, remove
102 if ( macro->guide[ comboItem ] == 0 )
103 {
104 record->pos = 0;
105 return ResultMacroEval_Remove;
106 }
107
108 // Otherwise leave the macro in the list
109 return ResultMacroEval_DoNothing;
110 }
111
112
113 void Result_add( uint32_t index )
114 {
115 }
116
117
118 void Result_setup()
119 {
120 // Initialize ResultMacro states
121 for ( var_uint_t macro = 0; macro < ResultMacroNum; macro++ )
122 {
123 ResultMacroRecordList[ macro ].pos = 0;
124 ResultMacroRecordList[ macro ].state = 0;
125 ResultMacroRecordList[ macro ].stateType = 0;
126 }
127 }
128
129
130 void Result_process()
131 {
132 // Tail pointer for macroResultMacroPendingList
133 // Macros must be explicitly re-added
134 var_uint_t macroResultMacroPendingListTail = 0;
135
136 // Iterate through the pending ResultMacros, processing each of them
137 for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
138 {
139 switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
140 {
141 // Re-add macros to pending list
142 case ResultMacroEval_DoNothing:
143 default:
144 macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
145 break;
146
147 // Remove Macro from Pending List, nothing to do, removing by default
148 case ResultMacroEval_Remove:
149 break;
150 }
151 }
152
153 // Update the macroResultMacroPendingListSize with the tail pointer
154 macroResultMacroPendingListSize = macroResultMacroPendingListTail;
155 }
156