forked from LucidVR/lucidgloves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoding.ino
74 lines (67 loc) · 2.38 KB
/
Encoding.ino
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
/*struct inputData {
int* flexion;
int joyX;
int joyY;
bool joyClick;
bool triggerButton;
bool aButton;
bool bButton;
bool grab;
bool pinch;
};
struct outputData{
int* hapticLimits;
};
*/
#if ENCODING == ENCODING_LEGACY
//legacy encoding
char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){
static char stringToEncode[75];
sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n",
flexion[0], flexion[1], flexion[2], flexion[3], flexion[4],
joyX, joyY, joyClick,
triggerButton, aButton, bButton, grab, pinch
);
return stringToEncode;
}
//legacy decoding
void decodeData(char* stringToDecode, int* hapticLimits){
byte index = 0;
char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters
while(ptr != NULL)
{
hapticLimits[index] = atoi(ptr);
index++;
ptr = strtok(NULL, "&"); // takes a list of delimiters
}
}
#endif
#if ENCODING == ENCODE_ALPHA
//alphabetic encoding
char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){
static char stringToEncode[75];
int trigger = (flexion[1] > ANALOG_MAX/2) ? (flexion[1] - ANALOG_MAX/2) * 2:0;
sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n",
flexion[0], flexion[1], flexion[2], flexion[3], flexion[4],
joyX, joyY, trigger, joyClick?"H":"",
triggerButton?"I":"", aButton?"J":"", bButton?"K":"", grab?"L":"", pinch?"M":"", menu?"N":"", calib?"O":""
);
return stringToEncode;
}
//legacy decoding
void decodeData(char* stringToDecode, int* hapticLimits){
hapticLimits[0] = getArgument(stringToDecode, 'A'); //thumb
hapticLimits[1] = getArgument(stringToDecode, 'B'); //index
hapticLimits[2] = getArgument(stringToDecode, 'C'); //middle
hapticLimits[3] = getArgument(stringToDecode, 'D'); //ring
hapticLimits[4] = getArgument(stringToDecode, 'E'); //pinky
//Serial.println("Haptic: "+ (String)hapticLimits[0] + " " + (String)hapticLimits[1] + " " + (String)hapticLimits[2] + " " + (String)hapticLimits[3] + " " + (String)hapticLimits[4] + " ");
}
int getArgument(char* stringToDecode, char command){
char* start = strchr(stringToDecode, command);
if (start == NULL)
return -1;
else
return atoi(start + 1);
}
#endif