diff --git a/platformio.ini b/platformio.ini index 46b068d..91e05e5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,4 +12,7 @@ platform = teensy board = teensy40 framework = arduino -build_flags = -D USB_FLIGHTSIM_JOYSTICK +build_flags = -D USB_HID +lib_deps = + contrem/arduino-timer@^3.0.1 + thomasfredericks/Bounce2@^2.72 diff --git a/src/defines.h b/src/defines.h new file mode 100644 index 0000000..89bdfb5 --- /dev/null +++ b/src/defines.h @@ -0,0 +1,9 @@ +//Define pins used for each gear +#define GEAR1 1 +#define GEAR2 2 +#define GEAR3 3 +#define GEAR4 4 +#define GEAR5 5 +#define GEAR6 6 + +#define NUM_BUTTONS 6 diff --git a/src/main.cpp b/src/main.cpp index b6d6c75..2ad7877 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,41 @@ -#include +#include "defines.h" -// put function declarations here: -int myFunction(int, int); +#include +#include +#include +#include + +Timer<1, micros> TxTimer; +Bounce2::Button * buttons = new Bounce2::Button[NUM_BUTTONS]; +const uint8_t BUTTON_PINS[NUM_BUTTONS] = {GEAR1, GEAR2, GEAR3, GEAR4, GEAR5, GEAR6}; + +bool TxJoystick(void *) { + Joystick.send_now(); + return true; +} void setup() { - // put your setup code here, to run once: - int result = myFunction(2, 3); + //Enable manual send of joystick packets + Joystick.useManualSend(true); + + //Attach buttons to debounce library. pullup, pressed state is low + for (int i = 0; i < NUM_BUTTONS; i++) { + buttons[i].attach( BUTTON_PINS[i] , INPUT_PULLUP ); //setup the bounce instance for the current button + buttons[i].interval(5); // interval in ms + buttons[i].setPressedState(LOW); + } + + TxTimer.every(1000, TxJoystick); } void loop() { - // put your main code here, to run repeatedly: -} + //Read buttons and update joystick state. + for (int i = 0; i < NUM_BUTTONS; i++) { + buttons[i].update(); + Joystick.button(i+1, buttons[i].isPressed()); + } + + //Transmit USB packet + TxTimer.tick(); -// put function definitions here: -int myFunction(int x, int y) { - return x + y; }