From f0c8d47bd96fafbae982b185d0bbabe8e6140ff5 Mon Sep 17 00:00:00 2001 From: Dan Frumin Date: Mon, 5 Aug 2019 20:30:38 +0200 Subject: [PATCH] Use Y axis as buttons --- Makefile | 2 +- README.org | 12 ++++++++++++ joystick.ino | 48 +++++++++++++++++++++++++++++------------------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 2db2392..e104ecf 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ BOARD_TAG = leonardo MONITOR_PORT = /dev/ttyACM0 ARDUINO_DIR = /home/dan/projects/arduino-1.8.9 -ARDUINO_LIBS = HID Mouse Joystick +ARDUINO_LIBS = HID Joystick CTAGS_EXEC = /usr/bin/ctags CTAGS_OPTS = -e ARDMK_DIR = /home/dan/projects/avr/Arduino-Makefile diff --git a/README.org b/README.org index f691011..d510793 100644 --- a/README.org +++ b/README.org @@ -7,11 +7,23 @@ trivial. * Compiling Install the dependencies: + - [[https://github.com/sudar/Arduino-Makefile][Arduino Makefile]] (available as arduino-mk in Ubuntu/Debiab) - [[https://github.com/MHeironimus/ArduinoJoystickLibrary][Arduino Joystick Library]] +Tweak ~joystick.ino~ to suit your needs. In particular, enable or +disable using the Y axis as additional buttons. + Then just run ~make~ and ~make upload~ to flash the board. +** Y axis as buttons +Using the Y axis on the joystick as additional buttons is useful for +playing e.g. SuperTuxKart, where you only need the joystick for +steering. + * Wiring There is nothing to wire -- just pop the shield onto your board and plug it in! + +* Debugging +You can use the ~jstest~ tool on Linux. diff --git a/joystick.ino b/joystick.ino index 9ffe2dd..9234e9e 100644 --- a/joystick.ino +++ b/joystick.ino @@ -1,8 +1,12 @@ /* We use the Joystick library from: */ #include -#include #define DELAY 5 +/* Whether to use the Y axis as two buttons. This is useful for + playing SuperTuxKart. Set this to 2 if you want to enable this + feature and to 0 otherwise. */ +#define USE_Y_AS_BUTTONS 2 + const int sw_pin = 2; const int vrx_pin = A0; @@ -18,34 +22,37 @@ const int base_y = 519; const int max_x = 1023; const int max_y = 1023; - - +/* joystick info will be read from -rng to +rng */ const int rng = 500; +/* If USE_Y_AS_BUTTONS is set up: joystick data above this will count as a click */ +const int threshold = 50; - -int mouse_mode = 0; int prev_readings[btn_count]; Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, - btn_count, // buttons - 1, // joystick hat switches + btn_count + USE_Y_AS_BUTTONS, // buttons + 0, // joystick hat switches true, // X axis +#if USE_Y_AS_BUTTONS == 0 true, // Y axis +#else + false, // Y axis +#endif false, // Z axis false, false, false, false, false, false, false, false); -void setup() { +void setup() { pinMode(sw_pin, INPUT); for (int i = 0; i < btn_count; i++) { pinMode(btn_pins[i], INPUT); prev_readings[i] = 0; } - Mouse.begin(); Joystick.begin(); Joystick.setXAxisRange(-rng, rng); - Joystick.setYAxisRange(-rng, rng); + if (!USE_Y_AS_BUTTONS) + Joystick.setYAxisRange(-rng, rng); } int prev_clicked = 0; @@ -66,9 +73,6 @@ int read_direction(int analog_pin, int base, int max, int rng) { } void loop() { - /*int click = get_clicked(); - if (click) { - Mouse.click();}*/ for (int i = 0; i < btn_count; i++) { int pin = btn_pins[i]; @@ -82,15 +86,21 @@ void loop() { // Analogue input int dirx = -read_direction(vrx_pin, base_x, max_x, rng); int diry = read_direction(vry_pin, base_y, max_y, rng); - if (mouse_mode) { - if (diry != 0) { - Mouse.move(0, diry, 0); + Joystick.setXAxis (dirx); + if (USE_Y_AS_BUTTONS) { + // first button + if (diry >= threshold) { + Joystick.setButton(btn_count, HIGH); + } else { + Joystick.setButton(btn_count, LOW); } - if (dirx != 0) { - Mouse.move(dirx, 0, 0); + // second button + if (diry <= -threshold) { + Joystick.setButton(btn_count+1, HIGH); + } else { + Joystick.setButton(btn_count+1, LOW); } } else { - Joystick.setXAxis (dirx); Joystick.setYAxis (diry); }