#include <Wire.h>
#include <Arduino.h>
#include <EasyButton.h>
#include <SimpleEncoder.h>
#include <TomStick.h>
//#include <HID-Project.h>
// Undefine conflicting macro before including Keypad
#undef IDLE
#include <Keypad.h>
const int BTN = 12;
const int encA = 10;
const int encB = 11;
const int mousebutton = 9;
long startValue = 0;
long lowerValue = -1;
long upperValue = 1;
#define JOYSTICK_X_PIN A0
#define JOYSTICK_Y_PIN A1
// Duration.
int duration = 500;
// Button.
EasyButton mediabutton(BTN);
EasyButton mouseclick(mousebutton);
SimpleEncoder encoder(BTN, encA, encB, startValue, lowerValue, upperValue);
TomStick joystick(JOYSTICK_X_PIN, JOYSTICK_Y_PIN);
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A2, A3, A4, A5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Callback.
// Media control button
void playpausebutton() {
Serial.println("Play/Pause");
}
void mutebutton() {
Serial.println("Mute");
}
void fwdrevbutton() {
Serial.println("Forward/Reverse");
}
// Mouse functions
void leftclick() {
Serial.println("Mouse left click!");
}
void rightclick() {
Serial.println("Mouse right click!");
}
void setup() {
Serial.begin(9600);
mediabutton.begin();
mouseclick.begin();
// Attach callback.
mediabutton.onPressed(playpausebutton);
mediabutton.onPressedFor(duration, mutebutton);
mediabutton.onSequence(2, 200, fwdrevbutton);
mouseclick.onPressed(leftclick);
mouseclick.onPressedFor(duration, rightclick);
// Initialize the joystick values
joystick.begin();
// Calibreate the joystick center position
joystick.calibrate();
// Set the dead zone threshold of the joystick between 0 - 100 (default: 50)
joystick.setDeadZoneThreshold(10);
}
void loop() {
mediabutton.read(); // Important! Update the button state
mouseclick.read();
// Store previous joystick values
static int prevX = 0;
static int prevY = 0;
// Update joystick movement
joystick.onMove();
// Check if there is a change in X or Y axis
if (joystick.joystickInfo.AxisX != prevX || joystick.joystickInfo.AxisY != prevY) {
Serial.print("X: ");
Serial.print(joystick.joystickInfo.AxisX);
Serial.print(" ,Y: ");
Serial.print(joystick.joystickInfo.AxisY);
Serial.println();
// Update the previous values to the current ones
prevX = joystick.joystickInfo.AxisX;
prevY = joystick.joystickInfo.AxisY;
}
// Keypad handling
char key = keypad.getKey();
if (key){
Serial.println(key);
}
if (encoder.CHANGING) {
long value = encoder.VALUE;
if (value > 0) {
Serial.println("Volume Up");
} else if (value < 0) {
Serial.println("Volume Down");
} else {
Serial.println("Neutral");
}
}
}