#include <Joystick.h>
#include <Mux.h>

Joystick joystick(false);

// These defines match the board schematic
#define ENC1   2, 3
#define ENC2   4, 5
#define BTN1   6
#define BTN2   7
#define BTN3   8
#define BTN4   9
#define BTN5   10
#define BTN6   11
#define BTN7   12
#define BTN8   13
#define BTN9   A0

// Buttons 10 and higher are all on A1 (the MUX signal pin), these are multiplexer channel IDs
#define BTN10  0
#define BTN11  1
#define BTN12  2
#define BTN13  3
#define BTN14  4
#define BTN15  5
#define BTN16  6
#define BTN17  7
#define BTN18  8
#define BTN19  9
#define BTN20  10
#define BTN21  11
#define BTN22  12
#define KBTN1   13
#define KBTN2   14
#define KBTN3   15

void setup() {
  // Encoders
  joystick.AddEncoder(ENC1, ENCODER_PULSED_SPLIT);
  joystick.AddEncoder(ENC2, ENCODER_PULSED_SPLIT);  
                  
  // // Power switches
  joystick.AddButton(BTN1, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);
  joystick.AddButton(BTN2, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);
  joystick.AddButton(BTN3, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);
  joystick.AddButton(BTN4, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);

  // // HUD modes
  joystick.AddButton(BTN5, BUTTON_PASSTHRU);
  joystick.AddButton(BTN6, BUTTON_PASSTHRU);
  joystick.AddButton(BTN7, BUTTON_PASSTHRU);

  // // Mining Modules
  joystick.AddButton(BTN8, BUTTON_PASSTHRU);
  joystick.AddButton(BTN9, BUTTON_PASSTHRU);
  joystick.AddButton(BTN10, BUTTON_PASSTHRU, true, mux);

  // // Flight Modes
  joystick.AddButton(BTN11, BUTTON_PASSTHRU, true, mux);
  joystick.AddButton(BTN12, BUTTON_PASSTHRU, true, mux);
  joystick.AddButton(BTN13, BUTTON_PASSTHRU, true, mux);
  joystick.AddButton(BTN14, BUTTON_PASSTHRU, true, mux);

  // // Doors & Lights
  joystick.AddButton(BTN15, BUTTON_PULSED_DOUBLE_ACTION, true, mux);
  joystick.AddButton(BTN16, BUTTON_PULSED_DOUBLE_ACTION_SPLIT, true, mux);
  joystick.AddButton(BTN17, BUTTON_PULSED_DOUBLE_ACTION_SPLIT, true, mux);
  
  // // Emergency Panel
  joystick.AddButton(KBTN1, BUTTON_PASSTHRU);
  joystick.AddButton(KBTN2, BUTTON_PASSTHRU);
  joystick.AddButton(KBTN3, BUTTON_PASSTHRU);

  joystick.Init();
}

void loop() {
  joystick.Update();
}
