commit 14266a483bdd6c5d8c8165c9c7db195fa5d13848 Author: Dan Frumin Date: Wed Jul 31 17:38:24 2019 +0200 first commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..04e37ec --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +# Using Arduino Make file. + +BOARD_TAG = leonardo +MONITOR_PORT = /dev/ttyACM0 +ARDUINO_DIR = /home/dan/projects/arduino-1.8.9 +ARDUINO_LIBS = HID Mouse +include /home/dan/projects/avr/Arduino-Makefile/Arduino.mk + diff --git a/joystick.ino b/joystick.ino new file mode 100644 index 0000000..b127116 --- /dev/null +++ b/joystick.ino @@ -0,0 +1,53 @@ +#include +#define DELAY 5 + +const int sw_pin = 8; +const int vrx_pin = A0; +const int vry_pin = A1; + +const int base_x = 512; +const int base_y = 519; + +const int max_x = 1023; +const int max_y = 1023; + +const int rng = 14; + +void setup() { + pinMode(sw_pin, INPUT_PULLUP); + Mouse.begin(); +} + +int prev_clicked = 0; +int get_clicked() { + int signal = !digitalRead(sw_pin); + int ret = (signal && signal != prev_clicked) ? 1 : 0; + prev_clicked = signal; + return ret; +} + +int read_direction(int analog_pin, int base, int max, int rng) { + int signal = analogRead(analog_pin); + if (signal > base) { + return map (signal, base, max, 0, rng); + } else if (signal < base) { + return - map (signal, base, 0, 1, rng); + } else { return 0; } +} + +void loop() { + int click = get_clicked(); + if (click) { + Mouse.click(); + } + int dirx = read_direction(vrx_pin, base_x, max_x, rng); + int diry = read_direction(vry_pin, base_y, max_y, rng); + if (diry != 0) { + Mouse.move(0, diry, 0); + } + if (dirx != 0) { + Mouse.move(dirx, 0, 0); + } + + delay(DELAY); +}