69 lines
1.3 KiB
C++
69 lines
1.3 KiB
C++
#import "Arduino.h"
|
|
|
|
class JoystickController {
|
|
|
|
private:
|
|
int pin_x;
|
|
int pin_y;
|
|
int x = 1024 / 2;
|
|
int y = 1024 / 2;
|
|
|
|
public:
|
|
JoystickController(int pin_x, int pin_y) {
|
|
this->set_pin_x(pin_x);
|
|
this->set_pin_y(pin_y);
|
|
}
|
|
|
|
void configure_pins() {
|
|
pinMode(this->pin_x, INPUT);
|
|
pinMode(this->pin_y, INPUT);
|
|
}
|
|
|
|
int get_x() {
|
|
return this->x;
|
|
}
|
|
|
|
int get_y() {
|
|
return this->y;
|
|
}
|
|
|
|
void set_x(int x) {
|
|
this->x = x;
|
|
}
|
|
|
|
void set_y(int y) {
|
|
this->y = y;
|
|
}
|
|
|
|
int get_pin_x() {
|
|
return this->pin_x;
|
|
}
|
|
|
|
void set_pin_x(int pin_x) {
|
|
this->pin_x = pin_x;
|
|
}
|
|
|
|
int get_pin_y() {
|
|
return this->pin_y;
|
|
}
|
|
|
|
void set_pin_y(int pin_y) {
|
|
this->pin_y = pin_y;
|
|
}
|
|
|
|
void read(){
|
|
this->set_y(analogRead(this->pin_y));
|
|
this->set_x(analogRead(this->pin_x));
|
|
}
|
|
|
|
bool is_moving_right(){
|
|
this->read();
|
|
return this->get_x() < 512;
|
|
}
|
|
|
|
bool is_moving_left(){
|
|
this->read();
|
|
return this->get_x() > 512;
|
|
}
|
|
|
|
}; |