-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
executable file
·82 lines (72 loc) · 1.4 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "util.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "common.h"
/*
* arbitry list of all button names
*/
static const char * const buttons[] = {
"y", "b", "a", "x",
"l", "r", "zl", "zr",
"plus", "minus", "sleft", "sright", "home", "capture",
"dup", "dright", "ddown", "dleft", NULL
};
int buttonToIndex(char* buff) {
for (char* p = buff; *p; ++p) *p = tolower(*p);
return prefixStrSelect(buttons, buff);
}
const char * indexToButtonStr(int i) {
if (i < 0 || i > sizeof(buttons) / sizeof(buttons[0]))
return NULL;
else
return buttons[i];
}
uint8_t fillHat(uint8_t h) {
h &= 0xF;
return (h << 4) | h;
}
uint8_t buttonToHatMatrix[16] = {
0x8,
0x6,
0x4,
0x5,
0x2,
0xF,
0x3,
0xF,
0x0,
0x7,
0xF,
0xF,
0x1,
0xF,
0xF,
0xF
};
uint8_t hatToButtonMatrix[16] = {
0b1000,
0b1100,
0b0100,
0b0110,
0b0010,
0b0011,
0b0001,
0b1001,
0b0000,
0b0000,
0b0000,
0b0000,
0b0000,
0b0000,
0b0000,
0b0000
};
void addInputs(USB_JoystickReport_Input_t* a, const USB_JoystickReport_Input_t* b) {
a->Button |= b->Button;
a->HAT = fillHat(buttonToHat(hatToButton(a->HAT) | hatToButton(b->HAT)));
a->LX = clamp(mapRange(a->LX + b->LX, 0, 255+255, 0, 255), 0, 255);
a->LY = clamp(mapRange(a->LY + b->LY, 0, 255+255, 0, 255), 0, 255);
a->RX = clamp(mapRange(a->RX + b->RX, 0, 255+255, 0, 255), 0, 255);
a->RY = clamp(mapRange(a->RY + b->RY, 0, 255+255, 0, 255), 0, 255);
}