Use Y axis as buttons
This commit is contained in:
parent
a5ccce2277
commit
f0c8d47bd9
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@
|
||||||
BOARD_TAG = leonardo
|
BOARD_TAG = leonardo
|
||||||
MONITOR_PORT = /dev/ttyACM0
|
MONITOR_PORT = /dev/ttyACM0
|
||||||
ARDUINO_DIR = /home/dan/projects/arduino-1.8.9
|
ARDUINO_DIR = /home/dan/projects/arduino-1.8.9
|
||||||
ARDUINO_LIBS = HID Mouse Joystick
|
ARDUINO_LIBS = HID Joystick
|
||||||
CTAGS_EXEC = /usr/bin/ctags
|
CTAGS_EXEC = /usr/bin/ctags
|
||||||
CTAGS_OPTS = -e
|
CTAGS_OPTS = -e
|
||||||
ARDMK_DIR = /home/dan/projects/avr/Arduino-Makefile
|
ARDMK_DIR = /home/dan/projects/avr/Arduino-Makefile
|
||||||
|
|
12
README.org
12
README.org
|
@ -7,11 +7,23 @@ trivial.
|
||||||
|
|
||||||
* Compiling
|
* Compiling
|
||||||
Install the dependencies:
|
Install the dependencies:
|
||||||
|
|
||||||
- [[https://github.com/sudar/Arduino-Makefile][Arduino Makefile]] (available as arduino-mk in Ubuntu/Debiab)
|
- [[https://github.com/sudar/Arduino-Makefile][Arduino Makefile]] (available as arduino-mk in Ubuntu/Debiab)
|
||||||
- [[https://github.com/MHeironimus/ArduinoJoystickLibrary][Arduino Joystick Library]]
|
- [[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.
|
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
|
* Wiring
|
||||||
There is nothing to wire -- just pop the shield onto your board and
|
There is nothing to wire -- just pop the shield onto your board and
|
||||||
plug it in!
|
plug it in!
|
||||||
|
|
||||||
|
* Debugging
|
||||||
|
You can use the ~jstest~ tool on Linux.
|
||||||
|
|
46
joystick.ino
46
joystick.ino
|
@ -1,8 +1,12 @@
|
||||||
/* We use the Joystick library from:
|
/* We use the Joystick library from:
|
||||||
<https://github.com/MHeironimus/ArduinoJoystickLibrary> */
|
<https://github.com/MHeironimus/ArduinoJoystickLibrary> */
|
||||||
#include <Joystick.h>
|
#include <Joystick.h>
|
||||||
#include <Mouse.h>
|
|
||||||
#define DELAY 5
|
#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 sw_pin = 2;
|
||||||
const int vrx_pin = A0;
|
const int vrx_pin = A0;
|
||||||
|
@ -18,21 +22,24 @@ const int base_y = 519;
|
||||||
const int max_x = 1023;
|
const int max_x = 1023;
|
||||||
const int max_y = 1023;
|
const int max_y = 1023;
|
||||||
|
|
||||||
|
/* joystick info will be read from -rng to +rng */
|
||||||
|
|
||||||
const int rng = 500;
|
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];
|
int prev_readings[btn_count];
|
||||||
|
|
||||||
|
|
||||||
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
|
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
|
||||||
JOYSTICK_TYPE_GAMEPAD,
|
JOYSTICK_TYPE_GAMEPAD,
|
||||||
btn_count, // buttons
|
btn_count + USE_Y_AS_BUTTONS, // buttons
|
||||||
1, // joystick hat switches
|
0, // joystick hat switches
|
||||||
true, // X axis
|
true, // X axis
|
||||||
|
#if USE_Y_AS_BUTTONS == 0
|
||||||
true, // Y axis
|
true, // Y axis
|
||||||
|
#else
|
||||||
|
false, // Y axis
|
||||||
|
#endif
|
||||||
false, // Z axis
|
false, // Z axis
|
||||||
false, false, false, false, false, false, false, false);
|
false, false, false, false, false, false, false, false);
|
||||||
|
|
||||||
|
@ -42,10 +49,10 @@ void setup() {
|
||||||
pinMode(btn_pins[i], INPUT);
|
pinMode(btn_pins[i], INPUT);
|
||||||
prev_readings[i] = 0;
|
prev_readings[i] = 0;
|
||||||
}
|
}
|
||||||
Mouse.begin();
|
|
||||||
Joystick.begin();
|
Joystick.begin();
|
||||||
Joystick.setXAxisRange(-rng, rng);
|
Joystick.setXAxisRange(-rng, rng);
|
||||||
Joystick.setYAxisRange(-rng, rng);
|
if (!USE_Y_AS_BUTTONS)
|
||||||
|
Joystick.setYAxisRange(-rng, rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
int prev_clicked = 0;
|
int prev_clicked = 0;
|
||||||
|
@ -66,9 +73,6 @@ int read_direction(int analog_pin, int base, int max, int rng) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
/*int click = get_clicked();
|
|
||||||
if (click) {
|
|
||||||
Mouse.click();}*/
|
|
||||||
|
|
||||||
for (int i = 0; i < btn_count; i++) {
|
for (int i = 0; i < btn_count; i++) {
|
||||||
int pin = btn_pins[i];
|
int pin = btn_pins[i];
|
||||||
|
@ -82,15 +86,21 @@ void loop() {
|
||||||
// Analogue input
|
// Analogue input
|
||||||
int dirx = -read_direction(vrx_pin, base_x, max_x, rng);
|
int dirx = -read_direction(vrx_pin, base_x, max_x, rng);
|
||||||
int diry = read_direction(vry_pin, base_y, max_y, rng);
|
int diry = read_direction(vry_pin, base_y, max_y, rng);
|
||||||
if (mouse_mode) {
|
Joystick.setXAxis (dirx);
|
||||||
if (diry != 0) {
|
if (USE_Y_AS_BUTTONS) {
|
||||||
Mouse.move(0, diry, 0);
|
// first button
|
||||||
|
if (diry >= threshold) {
|
||||||
|
Joystick.setButton(btn_count, HIGH);
|
||||||
|
} else {
|
||||||
|
Joystick.setButton(btn_count, LOW);
|
||||||
}
|
}
|
||||||
if (dirx != 0) {
|
// second button
|
||||||
Mouse.move(dirx, 0, 0);
|
if (diry <= -threshold) {
|
||||||
|
Joystick.setButton(btn_count+1, HIGH);
|
||||||
|
} else {
|
||||||
|
Joystick.setButton(btn_count+1, LOW);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Joystick.setXAxis (dirx);
|
|
||||||
Joystick.setYAxis (diry);
|
Joystick.setYAxis (diry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue