fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
9.6 KiB402 linesraw
1#ifndef _UTIL_STATE_UTIL_HPP
2#define _UTIL_STATE_UTIL_HPP
3
4#include "stdlib.hpp"
5
6#include <config.pb.h>
7
8typedef struct _ButtonState {
9    bool lf1 : 1;
10    bool lf2 : 1;
11    bool lf3 : 1;
12    bool lf4 : 1;
13    bool lf5 : 1;
14    bool lf6 : 1;
15    bool lf7 : 1;
16    bool lf8 : 1;
17    bool lf9 : 1;
18    bool lf10 : 1;
19    bool lf11 : 1;
20    bool lf12 : 1;
21    bool lf13 : 1;
22    bool lf14 : 1;
23    bool lf15 : 1;
24    bool lf16 : 1;
25    bool rf1 : 1;
26    bool rf2 : 1;
27    bool rf3 : 1;
28    bool rf4 : 1;
29    bool rf5 : 1;
30    bool rf6 : 1;
31    bool rf7 : 1;
32    bool rf8 : 1;
33    bool rf9 : 1;
34    bool rf10 : 1;
35    bool rf11 : 1;
36    bool rf12 : 1;
37    bool rf13 : 1;
38    bool rf14 : 1;
39    bool rf15 : 1;
40    bool rf16 : 1;
41    bool lt1 : 1;
42    bool lt2 : 1;
43    bool lt3 : 1;
44    bool lt4 : 1;
45    bool lt5 : 1;
46    bool lt6 : 1;
47    bool lt7 : 1;
48    bool lt8 : 1;
49    bool rt1 : 1;
50    bool rt2 : 1;
51    bool rt3 : 1;
52    bool rt4 : 1;
53    bool rt5 : 1;
54    bool rt6 : 1;
55    bool rt7 : 1;
56    bool rt8 : 1;
57    bool mb1 : 1;
58    bool mb2 : 1;
59    bool mb3 : 1;
60    bool mb4 : 1;
61    bool mb5 : 1;
62    bool mb6 : 1;
63    bool mb7 : 1;
64    bool mb8 : 1;
65    bool mb9 : 1;
66    bool mb10 : 1;
67    bool mb11 : 1;
68    bool mb12 : 1;
69} ButtonState;
70
71inline void set_button(uint64_t &buttons, Button button_index, bool pressed) {
72    ButtonState &inputs = (ButtonState &)buttons;
73    if (button_index == BTN_UNSPECIFIED) {
74        return;
75    }
76    switch (button_index) {
77        case BTN_LF1:
78            inputs.lf1 = pressed;
79            break;
80        case BTN_LF2:
81            inputs.lf2 = pressed;
82            break;
83        case BTN_LF3:
84            inputs.lf3 = pressed;
85            break;
86        case BTN_LF4:
87            inputs.lf4 = pressed;
88            break;
89        case BTN_LF5:
90            inputs.lf5 = pressed;
91            break;
92        case BTN_LF6:
93            inputs.lf6 = pressed;
94            break;
95        case BTN_LF7:
96            inputs.lf7 = pressed;
97            break;
98        case BTN_LF8:
99            inputs.lf8 = pressed;
100            break;
101        case BTN_LF9:
102            inputs.lf9 = pressed;
103            break;
104        case BTN_LF10:
105            inputs.lf10 = pressed;
106            break;
107        case BTN_LF11:
108            inputs.lf11 = pressed;
109            break;
110        case BTN_LF12:
111            inputs.lf12 = pressed;
112            break;
113        case BTN_LF13:
114            inputs.lf13 = pressed;
115            break;
116        case BTN_LF14:
117            inputs.lf14 = pressed;
118            break;
119        case BTN_LF15:
120            inputs.lf15 = pressed;
121            break;
122        case BTN_LF16:
123            inputs.lf16 = pressed;
124            break;
125        case BTN_RF1:
126            inputs.rf1 = pressed;
127            break;
128        case BTN_RF2:
129            inputs.rf2 = pressed;
130            break;
131        case BTN_RF3:
132            inputs.rf3 = pressed;
133            break;
134        case BTN_RF4:
135            inputs.rf4 = pressed;
136            break;
137        case BTN_RF5:
138            inputs.rf5 = pressed;
139            break;
140        case BTN_RF6:
141            inputs.rf6 = pressed;
142            break;
143        case BTN_RF7:
144            inputs.rf7 = pressed;
145            break;
146        case BTN_RF8:
147            inputs.rf8 = pressed;
148            break;
149        case BTN_RF9:
150            inputs.rf9 = pressed;
151            break;
152        case BTN_RF10:
153            inputs.rf10 = pressed;
154            break;
155        case BTN_RF11:
156            inputs.rf11 = pressed;
157            break;
158        case BTN_RF12:
159            inputs.rf12 = pressed;
160            break;
161        case BTN_RF13:
162            inputs.rf13 = pressed;
163            break;
164        case BTN_RF14:
165            inputs.rf14 = pressed;
166            break;
167        case BTN_RF15:
168            inputs.rf15 = pressed;
169            break;
170        case BTN_RF16:
171            inputs.rf16 = pressed;
172            break;
173        case BTN_LT1:
174            inputs.lt1 = pressed;
175            break;
176        case BTN_LT2:
177            inputs.lt2 = pressed;
178            break;
179        case BTN_LT3:
180            inputs.lt3 = pressed;
181            break;
182        case BTN_LT4:
183            inputs.lt4 = pressed;
184            break;
185        case BTN_LT5:
186            inputs.lt5 = pressed;
187            break;
188        case BTN_LT6:
189            inputs.lt6 = pressed;
190            break;
191        case BTN_LT7:
192            inputs.lt7 = pressed;
193            break;
194        case BTN_LT8:
195            inputs.lt8 = pressed;
196            break;
197        case BTN_RT1:
198            inputs.rt1 = pressed;
199            break;
200        case BTN_RT2:
201            inputs.rt2 = pressed;
202            break;
203        case BTN_RT3:
204            inputs.rt3 = pressed;
205            break;
206        case BTN_RT4:
207            inputs.rt4 = pressed;
208            break;
209        case BTN_RT5:
210            inputs.rt5 = pressed;
211            break;
212        case BTN_RT6:
213            inputs.rt6 = pressed;
214            break;
215        case BTN_RT7:
216            inputs.rt7 = pressed;
217            break;
218        case BTN_RT8:
219            inputs.rt8 = pressed;
220            break;
221        case BTN_MB1:
222            inputs.mb1 = pressed;
223            break;
224        case BTN_MB2:
225            inputs.mb2 = pressed;
226            break;
227        case BTN_MB3:
228            inputs.mb3 = pressed;
229            break;
230        case BTN_MB4:
231            inputs.mb4 = pressed;
232            break;
233        case BTN_MB5:
234            inputs.mb5 = pressed;
235            break;
236        case BTN_MB6:
237            inputs.mb6 = pressed;
238            break;
239        case BTN_MB7:
240            inputs.mb7 = pressed;
241            break;
242        case BTN_MB8:
243            inputs.mb8 = pressed;
244            break;
245        case BTN_MB9:
246            inputs.mb9 = pressed;
247            break;
248        case BTN_MB10:
249            inputs.mb10 = pressed;
250            break;
251        case BTN_MB11:
252            inputs.mb11 = pressed;
253            break;
254        case BTN_MB12:
255            inputs.mb12 = pressed;
256            break;
257        default:
258            break;
259    }
260}
261
262inline bool get_button(const uint64_t &buttons, Button button_index) {
263    ButtonState &inputs = (ButtonState &)buttons;
264    switch (button_index) {
265        case BTN_LF1:
266            return inputs.lf1;
267        case BTN_LF2:
268            return inputs.lf2;
269        case BTN_LF3:
270            return inputs.lf3;
271        case BTN_LF4:
272            return inputs.lf4;
273        case BTN_LF5:
274            return inputs.lf5;
275        case BTN_LF6:
276            return inputs.lf6;
277        case BTN_LF7:
278            return inputs.lf7;
279        case BTN_LF8:
280            return inputs.lf8;
281        case BTN_LF9:
282            return inputs.lf9;
283        case BTN_LF10:
284            return inputs.lf10;
285        case BTN_LF11:
286            return inputs.lf11;
287        case BTN_LF12:
288            return inputs.lf12;
289        case BTN_LF13:
290            return inputs.lf13;
291        case BTN_LF14:
292            return inputs.lf14;
293        case BTN_LF15:
294            return inputs.lf15;
295        case BTN_LF16:
296            return inputs.lf16;
297        case BTN_RF1:
298            return inputs.rf1;
299        case BTN_RF2:
300            return inputs.rf2;
301        case BTN_RF3:
302            return inputs.rf3;
303        case BTN_RF4:
304            return inputs.rf4;
305        case BTN_RF5:
306            return inputs.rf5;
307        case BTN_RF6:
308            return inputs.rf6;
309        case BTN_RF7:
310            return inputs.rf7;
311        case BTN_RF8:
312            return inputs.rf8;
313        case BTN_RF9:
314            return inputs.rf9;
315        case BTN_RF10:
316            return inputs.rf10;
317        case BTN_RF11:
318            return inputs.rf11;
319        case BTN_RF12:
320            return inputs.rf12;
321        case BTN_RF13:
322            return inputs.rf13;
323        case BTN_RF14:
324            return inputs.rf14;
325        case BTN_RF15:
326            return inputs.rf15;
327        case BTN_RF16:
328            return inputs.rf16;
329        case BTN_LT1:
330            return inputs.lt1;
331        case BTN_LT2:
332            return inputs.lt2;
333        case BTN_LT3:
334            return inputs.lt3;
335        case BTN_LT4:
336            return inputs.lt4;
337        case BTN_LT5:
338            return inputs.lt5;
339        case BTN_LT6:
340            return inputs.lt6;
341        case BTN_LT7:
342            return inputs.lt7;
343        case BTN_LT8:
344            return inputs.lt8;
345        case BTN_RT1:
346            return inputs.rt1;
347        case BTN_RT2:
348            return inputs.rt2;
349        case BTN_RT3:
350            return inputs.rt3;
351        case BTN_RT4:
352            return inputs.rt4;
353        case BTN_RT5:
354            return inputs.rt5;
355        case BTN_RT6:
356            return inputs.rt6;
357        case BTN_RT7:
358            return inputs.rt7;
359        case BTN_RT8:
360            return inputs.rt8;
361        case BTN_MB1:
362            return inputs.mb1;
363        case BTN_MB2:
364            return inputs.mb2;
365        case BTN_MB3:
366            return inputs.mb3;
367        case BTN_MB4:
368            return inputs.mb4;
369        case BTN_MB5:
370            return inputs.mb5;
371        case BTN_MB6:
372            return inputs.mb6;
373        case BTN_MB7:
374            return inputs.mb7;
375        case BTN_MB8:
376            return inputs.mb8;
377        case BTN_MB9:
378            return inputs.mb9;
379        case BTN_MB10:
380            return inputs.mb10;
381        case BTN_MB11:
382            return inputs.mb11;
383        case BTN_MB12:
384            return inputs.mb12;
385        default:
386            return false;
387    }
388}
389
390inline uint64_t make_button_mask(const Button *buttons, size_t buttons_count) {
391    uint64_t button_mask = 0;
392    for (size_t j = 0; j < buttons_count; j++) {
393        button_mask |= (1ULL << (buttons[j] - 1));
394    }
395    return button_mask;
396}
397
398inline bool all_buttons_held(const uint64_t &buttons, uint64_t button_mask) {
399    return button_mask != 0 && (buttons & button_mask) == button_mask;
400}
401
402#endif