Skip to content

Commit f01d606

Browse files
committed
Rework 'Check_inputs', removing superfluous code and including safeguards against memory access violations.
1 parent e9dd014 commit f01d606

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed
Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
#include "Braccio++.h"
1+
#include <Braccio++.h>
22

3-
String message = "";
3+
String toMessage(int const input)
4+
{
5+
static String const message[] =
6+
{ "",
7+
"LEFT (Joystick)",
8+
"RIGHT (Joystick)",
9+
"SELECT (Joystick)",
10+
"UP (Joystick)",
11+
"DOWN (Joystick)",
12+
"ENTER (Button)"
13+
};
414

5-
String checkInputs(int input){
6-
String check[] = { "",
7-
"Joystick left was moved!",
8-
"Joystick right was moved!",
9-
"Joystick select button was pressed!",
10-
"Joystick up was moved!",
11-
"Joystick down was moved!",
12-
"Enter button was pressed!"};
13-
return check[input];
15+
if (input < 7)
16+
return message[input];
17+
else
18+
return String("Error, invalid input value");
1419
}
1520

16-
void setup() {
21+
void setup()
22+
{
1723
Serial.begin(115200);
1824
while(!Serial){}
25+
1926
Braccio.begin();
2027
Serial.println("Press any button or move the joystick.");
2128
}
2229

23-
void loop() {
24-
message = checkInputs(Braccio.getKey());
25-
if(message != ""){
30+
void loop()
31+
{
32+
String const message = toMessage(Braccio.getKey());
33+
if(message != "")
2634
Serial.println(message);
27-
message = "";
28-
}
29-
delay(500);
35+
delay(100);
3036
}

0 commit comments

Comments
 (0)