From 19cd517153cfed8c8aeab7b012b14a759c0573c1 Mon Sep 17 00:00:00 2001 From: rafa Date: Fri, 23 May 2025 22:07:45 +0100 Subject: [PATCH] create joystick controller --- code/JoyStickController.cpp | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 code/JoyStickController.cpp diff --git a/code/JoyStickController.cpp b/code/JoyStickController.cpp new file mode 100644 index 0000000..44428be --- /dev/null +++ b/code/JoyStickController.cpp @@ -0,0 +1,67 @@ +#import "Arduino.h" + +class JoySitkController { + + private: + int pin_x; + int pin_y; + int x = 1024 / 2; + int y = 1024 / 2; + + public: + JoySitkController(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(){ + return this->get_x() < 512; + } + + bool is_moving_left(){ + return this->get_x() > 512; + } + +}; \ No newline at end of file