arduino-joystick/joystick.ino

109 lines
2.7 KiB
Arduino
Raw Normal View History

2019-08-02 13:14:09 +02:00
/* We use the Joystick library from:
<https://github.com/MHeironimus/ArduinoJoystickLibrary> */
#include <Joystick.h>
2019-07-31 17:38:24 +02:00
#define DELAY 5
2019-08-05 20:30:38 +02:00
/* 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
2019-07-31 17:38:24 +02:00
2019-08-02 13:14:09 +02:00
const int sw_pin = 2;
2019-07-31 17:38:24 +02:00
const int vrx_pin = A0;
const int vry_pin = A1;
2019-08-02 13:14:09 +02:00
// UP, RIGHT, DOWN, LEFT, additional left, additional right
const int btn_count = 6;
const int btn_pins[] = { 3,4,5,6,7,8 };
2019-07-31 17:38:24 +02:00
const int base_x = 512;
const int base_y = 519;
const int max_x = 1023;
const int max_y = 1023;
2019-08-05 20:30:38 +02:00
/* joystick info will be read from -rng to +rng */
2019-08-02 13:14:09 +02:00
const int rng = 500;
2019-08-05 20:30:38 +02:00
/* If USE_Y_AS_BUTTONS is set up: joystick data above this will count as a click */
const int threshold = 50;
2019-08-02 13:14:09 +02:00
int prev_readings[btn_count];
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_GAMEPAD,
2019-08-05 20:30:38 +02:00
btn_count + USE_Y_AS_BUTTONS, // buttons
0, // joystick hat switches
2019-08-02 13:14:09 +02:00
true, // X axis
2019-08-05 20:30:38 +02:00
#if USE_Y_AS_BUTTONS == 0
2019-08-02 13:14:09 +02:00
true, // Y axis
2019-08-05 20:30:38 +02:00
#else
false, // Y axis
#endif
2019-08-02 13:14:09 +02:00
false, // Z axis
false, false, false, false, false, false, false, false);
2019-07-31 17:38:24 +02:00
2019-08-05 20:30:38 +02:00
void setup() {
2019-08-02 13:14:09 +02:00
pinMode(sw_pin, INPUT);
for (int i = 0; i < btn_count; i++) {
pinMode(btn_pins[i], INPUT);
prev_readings[i] = 0;
}
Joystick.begin();
Joystick.setXAxisRange(-rng, rng);
2019-08-05 20:30:38 +02:00
if (!USE_Y_AS_BUTTONS)
Joystick.setYAxisRange(-rng, rng);
2019-07-31 17:38:24 +02:00
}
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() {
2019-08-02 13:14:09 +02:00
for (int i = 0; i < btn_count; i++) {
int pin = btn_pins[i];
int signal = digitalRead(pin);
if (signal != prev_readings[i]) {
Joystick.setButton(i, signal);
}
prev_readings[i] = signal;
2019-07-31 17:38:24 +02:00
}
2019-08-02 13:14:09 +02:00
// Analogue input
int dirx = -read_direction(vrx_pin, base_x, max_x, rng);
2019-07-31 17:38:24 +02:00
int diry = read_direction(vry_pin, base_y, max_y, rng);
2019-08-05 20:30:38 +02:00
Joystick.setXAxis (dirx);
if (USE_Y_AS_BUTTONS) {
// first button
if (diry >= threshold) {
Joystick.setButton(btn_count, HIGH);
} else {
Joystick.setButton(btn_count, LOW);
2019-08-02 13:14:09 +02:00
}
2019-08-05 20:30:38 +02:00
// second button
if (diry <= -threshold) {
Joystick.setButton(btn_count+1, HIGH);
} else {
Joystick.setButton(btn_count+1, LOW);
2019-08-02 13:14:09 +02:00
}
} else {
Joystick.setYAxis (diry);
2019-07-31 17:38:24 +02:00
}
delay(DELAY);
}