-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_src.cpp
98 lines (83 loc) · 2.72 KB
/
arduino_src.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* This is a very simple mockup and could probably be written better. idk, i dont use c++ */
// borrowed from https://stackoverflow.com/a/14824108.
String split_str(String data, char separator, int index) {
int found = 0;
int str_index[] = {0, -1};
int max_index = data.length()-1;
for (int i = 0; i <= max_index && found <= index; i++) {
if (data.charAt(i) == separator || i == max_index) {
found++;
str_index[0] = str_index[1] + 1;
str_index[1] = (i == max_index) ? i + 1 : i;
}
}
return found > index ? data.substring(str_index[0], str_index[1]) : "";
}
bool prev_clock = false;
String full_line = "";
String current_line = "";
//------------------------------------------------------------------------------
//
// rbx_read
//
// Executes the update loop for rbx_arduino and returns the next byte, if found.
//
//------------------------------------------------------------------------------
int rbx_read() {
bool curr_clock = XInput.getRumbleLeft() == 255;
if (curr_clock != prev_clock) {
prev_clock = curr_clock;
if (curr_clock == true) {
int byte = XInput.getRumbleRight();
char character = byte;
return character;
}
}
}
//------------------------------------------------------------------------------
//
// rbx_send_byte
//
// Sends a byte to a roblox game.
//
//------------------------------------------------------------------------------
void rbx_send_byte(int val) {
XInput.setTrigger(16, val);
XInput.send();
}
//------------------------------------------------------------------------------
//
// rbx_parse_cmd
//
// Parses and executes the command from rbx_arduino.
//
//------------------------------------------------------------------------------
void rbx_parse_cmd(String cmd) {
if (cmd != "") {
Serial1.println(cmd);
String command = split_str(cmd, ';', 0);
if (command == "PM") { // PIN MODE
int pin = split_str(cmd, ';', 1).toInt();
int mode = split_str(cmd, ';', 2).toInt();
pinMode(pin, mode);
} else if (command == "DW") { // DIGITAL WRITE
int pin = split_str(cmd, ';', 1).toInt();
int val = split_str(cmd, ';', 2).toInt();
digitalWrite(pin, val);
} else if (command == "EC") { // ECHO
int val = split_str(cmd, ';', 1).toInt();
rbx_send_byte(val);
}
}
}
//------------------------------------------------------------------------------
//
// rbx_begin
//
// Starts XInput.
//
//------------------------------------------------------------------------------
void rbx_begin() {
XInput.setAutoSend(false);
XInput.begin();
}