Skip to content

Commit 17ba174

Browse files
authored
Merge pull request #22 from bcmi-labs/joystick-api
Add simplified joystick API
2 parents ecd5bb9 + e1acc82 commit 17ba174

File tree

4 files changed

+40
-48
lines changed

4 files changed

+40
-48
lines changed

examples/Tools/Check_Inputs/Check_Inputs.ino

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* This example allows the user to read the
2+
* inputs of the Braccio++ shield UI devices:
3+
* - joystick (up,down,left,right,select)
4+
* - button (enter)
5+
*/
6+
7+
#include <Braccio++.h>
8+
9+
void setup()
10+
{
11+
Serial.begin(115200);
12+
while(!Serial){}
13+
14+
Braccio.begin();
15+
Serial.println("Press any button or move the joystick.");
16+
}
17+
18+
void loop()
19+
{
20+
if(Braccio.isJoystickPressed_LEFT()) Serial.println("LEFT (Joystick)");
21+
if(Braccio.isJoystickPressed_RIGHT()) Serial.println("RIGHT (Joystick)");
22+
if(Braccio.isJoystickPressed_SELECT())Serial.println("SELECT (Joystick)");
23+
if(Braccio.isJoystickPressed_UP()) Serial.println("UP (Joystick)");
24+
if(Braccio.isJoystickPressed_DOWN()) Serial.println("DOWN (Joystick)");
25+
if(Braccio.isButtonPressed_ENTER()) Serial.println("ENTER (Button)");
26+
delay(100);
27+
}

src/Braccio++.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,22 @@ void BraccioClass::motors_connected_thread() {
272272
}
273273

274274
int BraccioClass::getKey() {
275-
if (digitalRead(BTN_LEFT) == LOW) {
275+
if (isJoystickPressed_LEFT()) {
276276
return 1;
277277
}
278-
if (digitalRead(BTN_RIGHT) == LOW) {
278+
if (isJoystickPressed_RIGHT()) {
279279
return 2;
280280
}
281-
if (digitalRead(BTN_SEL) == LOW) {
281+
if (isJoystickPressed_SELECT()) {
282282
return 3;
283283
}
284-
if (digitalRead(BTN_UP) == LOW) {
284+
if (isJoystickPressed_UP()) {
285285
return 4;
286286
}
287-
if (digitalRead(BTN_DOWN) == LOW) {
287+
if (isJoystickPressed_DOWN()) {
288288
return 5;
289289
}
290-
if (digitalRead(BTN_ENTER) == LOW) {
290+
if (isButtonPressed_ENTER()) {
291291
return 6;
292292
}
293293
return 0;

src/Braccio++.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ class BraccioClass
146146
int getKey();
147147
void connectJoystickTo(lv_obj_t* obj);
148148

149+
inline bool isJoystickPressed_LEFT() { return (digitalRead(BTN_LEFT) == LOW); }
150+
inline bool isJoystickPressed_RIGHT() { return (digitalRead(BTN_RIGHT) == LOW); }
151+
inline bool isJoystickPressed_SELECT() { return (digitalRead(BTN_SEL) == LOW); }
152+
inline bool isJoystickPressed_UP() { return (digitalRead(BTN_UP) == LOW); }
153+
inline bool isJoystickPressed_DOWN() { return (digitalRead(BTN_DOWN) == LOW); }
154+
inline bool isButtonPressed_ENTER() { return (digitalRead(BTN_ENTER) == LOW); }
155+
149156
TFT_eSPI gfx = TFT_eSPI();
150157

151158
bool ping_allowed = true;

0 commit comments

Comments
 (0)