/**
* Sketch for testing all types of inouts for the EventInputs library.
* https://github.com/Stutchbury/InputEvents
*
* Based on the physical test rig I used.
*
*/
#include <Arduino.h>
#include <Encoder.h>
#include "EventEncoderButton.h"
#include "EventButton.h"
#include "EventJoystick.h"
#include "EventSwitch.h"
constexpr auto ENCODER_PIN1=2;
constexpr auto ENCODER_PIN2=3;
constexpr auto ENCODER_BUTTON_PIN=7;
constexpr auto ANALOG_PIN=A0;
constexpr auto JOYSTICK_X_PIN=A1;
constexpr auto JOYSTICK_Y_PIN=A2;
constexpr auto JOYSTICK_BUTTON_PIN=6;
constexpr auto BUTTON_PIN=4;
constexpr auto SWITCH_PIN=5;
EventEncoderButton eeb(ENCODER_PIN1, ENCODER_PIN2, ENCODER_BUTTON_PIN);
EventJoystick ej(JOYSTICK_X_PIN,JOYSTICK_Y_PIN);
EventButton ejb(JOYSTICK_BUTTON_PIN);
EventAnalog ea(ANALOG_PIN);
EventButton eb(BUTTON_PIN);
EventSwitch es(SWITCH_PIN);
void printEvent(InputEventType iet) {
switch (iet) {
case InputEventType::ENABLED :
Serial.print("ENABLED");
break;
case InputEventType::DISABLED :
Serial.print("DISABLED");
break;
case InputEventType::IDLE :
Serial.print("IDLE");
break;
case InputEventType::PRESSED :
Serial.print("PRESSED");
break;
case InputEventType::RELEASED :
Serial.print("RELEASED");
break;
case InputEventType::CLICKED :
Serial.print("CLICKED");
break;
case InputEventType::LONG_CLICKED :
Serial.print("LONG_CLICKED");
break;
case InputEventType::LONG_PRESS :
Serial.print("LONG_PRESS");
break;
case InputEventType::CHANGED :
Serial.print("CHANGED");
break;
case InputEventType::CHANGED_PRESSED :
Serial.print("CHANGED_PRESSED");
//Serial.printf(", spindle pressed position: %i ", hal.spindle.pressedPosition());
break;
case InputEventType::CHANGED_RELEASED :
Serial.print("CHANGED_RELEASED");
break;
case InputEventType::CHANGED_X :
Serial.print("CHANGED_X");
break;
case InputEventType::CHANGED_Y :
Serial.print("CHANGED_Y");
break;
case InputEventType::ON :
Serial.print("ON");
break;
case InputEventType::OFF :
Serial.print("OFF");
break;
default:
//Serial.printf("%i ??", iet);
break;
}
}
void onEncoderButtonEvent(InputEventType et, EventEncoderButton & eeb) {
Serial.print("onEncoderButtonEvent: ");
printEvent(et);
if ( et == InputEventType::CHANGED || et == InputEventType::CHANGED_PRESSED ) {
Serial.print(" increment: ");
Serial.print(eeb.increment());
Serial.print(" position: ");
Serial.print(eeb.position());
Serial.print(" pressed position: ");
Serial.print(eeb.pressedPosition());
}
if ( et == InputEventType::CLICKED ) {
eeb.resetPosition(0);
Serial.print(" resetPosition : position: ");
Serial.print(eeb.position());
Serial.print(" pressed position: ");
Serial.print(eeb.pressedPosition());
}
if ( et == InputEventType::LONG_CLICKED ) {
eeb.resetPressedPosition(0);
Serial.print(" resetPressedPosition : position: ");
Serial.print(eeb.position());
Serial.print(" pressed position: ");
Serial.print(eeb.pressedPosition());
}
Serial.println();
}
void onJoystickEvent(InputEventType et, EventJoystick & ej) {
Serial.print("onJoystickEvent: ");
printEvent(et);
Serial.print(" x: ");
Serial.print(ej.x.position());
Serial.print(" y: ");
Serial.print(ej.y.position());
Serial.println();
}
void onAnalogEvent(InputEventType et, EventAnalog & ea) {
Serial.print("onAnalogEvent: ");
printEvent(et);
Serial.print(" pos: ");
Serial.print(ea.position());
Serial.println();
}
void onButtonEvent(InputEventType et, EventButton & eb) {
Serial.print("onButtonEvent: ");
printEvent(et);
#ifndef ESP8266
if ( eb.getInputId() == 42 && et == InputEventType::CLICKED ) {
Serial.print(" Enabling/Disabling joystick");
Serial.println();
ej.enable((ej.isEnabled() ? false : true));
}
#endif
if ( et == InputEventType::LONG_CLICKED || et == InputEventType::LONG_PRESS ) {
Serial.print(" longPressCount: ");
Serial.print(eb.longPressCount());
}
Serial.println();
}
void onSwitchEvent(InputEventType et, EventSwitch & eb) {
Serial.print("onSwitchEvent: ");
printEvent(et);
Serial.print(" isOn(): ");
Serial.print(eb.isOn() ? "true" : "false");
Serial.print(" isOff(): ");
Serial.print(eb.isOff() ? "true" : "false");
Serial.println();
}
void setup() {
//Serial.begin(115200);
Serial.begin(9600);
delay(200);
Serial.println("Starting Test Rig....");
eeb.setCallback(onEncoderButtonEvent);
//set maxAdcResolution for 3v3 into 5v ADC
//ej.x.setAdcResolution(675);
//ej.y.setAdcResolution(675);
ej.setCallback(onJoystickEvent);
ej.setStartValues();
ej.setNumIncrements(10);
ejb.setCallback(onButtonEvent);
ejb.setInputId(42);
ea.setCallback(onAnalogEvent);
ea.setStartValue(0);
eb.setCallback(onButtonEvent);
es.setCallback(onSwitchEvent);
}
void loop() {
eeb.update();
ej.update();
ejb.update();
ea.update();
es.update();
eb.update();
}