SE-TP2/code/JoyStickController.cpp
2025-05-24 02:52:11 +01:00

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;
}
};