-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoyStick.cpp
More file actions
38 lines (30 loc) · 746 Bytes
/
JoyStick.cpp
File metadata and controls
38 lines (30 loc) · 746 Bytes
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
#include "JoyStick.h";
#include "Arduino.h"
JoyStick::JoyStick(uint8_t pinX, uint8_t pinY, uint8_t pinS) {
this->pinX = pinX;
this->pinY = pinY;
this->pinS = pinS;
pinMode(pinS, INPUT_PULLUP);
}
bool JoyStick::ButtonDown() {
if (!isButtonHeld) {
isButtonHeld = !digitalRead(pinS);
return isButtonHeld;
}
isButtonHeld = !digitalRead(pinS);
return false;
}
bool JoyStick::GetInput(Point *direction) {
int x = 511 - analogRead(pinX);
int y = 511 - analogRead(pinY);
if (abs(x) < tapOffset && abs(y) < tapOffset)
return false;
if (abs(x) > abs(y)) {
direction->x = constrain(x, -1, 1);
direction->y = 0;
} else {
direction->x = 0;
direction->y = constrain(-y, -1, 1);
}
return true;
}