testing button hardware+event handling library
This commit is contained in:
parent
baf657a5f4
commit
ed7a3e151f
|
@ -12,3 +12,6 @@
|
||||||
platform = renesas-ra
|
platform = renesas-ra
|
||||||
board = uno_r4_wifi
|
board = uno_r4_wifi
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
monitor_speed = 115200
|
||||||
|
lib_deps =
|
||||||
|
mathertel/OneButton@^2.6.1
|
||||||
|
|
114
src/main.cpp
114
src/main.cpp
|
@ -3,15 +3,41 @@
|
||||||
#include "Arduino_LED_Matrix.h"
|
#include "Arduino_LED_Matrix.h"
|
||||||
#include "NvmEeprom.hpp"
|
#include "NvmEeprom.hpp"
|
||||||
#include "SingleAxis.hpp"
|
#include "SingleAxis.hpp"
|
||||||
|
#include "OneButton.h"
|
||||||
|
|
||||||
// joystick calibration
|
// joystick calibration
|
||||||
#define TIME_CAL_1 2000
|
#define TIME_CAL_1 2000
|
||||||
#define TIME_CAL_2 3000
|
#define TIME_CAL_2 3000
|
||||||
|
|
||||||
// static objects
|
// static objects
|
||||||
AlignedJoy joystick_1(0, 1); /// X on A0, Y on A1
|
AlignedJoy joystick_1(PIN_A0, PIN_A1); /// X on A0, Y on A1
|
||||||
ArduinoLEDMatrix ledMatrix;
|
ArduinoLEDMatrix ledMatrix;
|
||||||
LinearSingleAxis lever0(2);
|
LinearSingleAxis lever0(PIN_A2);
|
||||||
|
|
||||||
|
enum Button : size_t {
|
||||||
|
RED_BUTTON = 0,
|
||||||
|
// WHITE_BUTTON,
|
||||||
|
|
||||||
|
NUM_BUTTONS,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
OneButton s_buttons[Button::NUM_BUTTONS] = {
|
||||||
|
[Button::RED_BUTTON] = OneButton(PIN_D2, true, false),
|
||||||
|
// [Button::WHITE_BUTTON] = OneButton(PIN_D3),
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static_assert(sizeof(void*) == sizeof(size_t));
|
||||||
|
void buttonClickHandler(void* ctx) {
|
||||||
|
// void* theButton = ctx;
|
||||||
|
|
||||||
|
Serial.println("Clicky clack");
|
||||||
|
|
||||||
|
// char buf[64] = {};
|
||||||
|
// snprintf(buf, 64, "Button %u pressed!", theButton);
|
||||||
|
// Serial.println(buf);
|
||||||
|
}
|
||||||
|
|
||||||
/// Run calibration on the provided joystick. Save the results to the provided calibration
|
/// Run calibration on the provided joystick. Save the results to the provided calibration
|
||||||
bool runJoystickCalibrationRoutine(AlignedJoy& joy, JoystickCalibration& cal) {
|
bool runJoystickCalibrationRoutine(AlignedJoy& joy, JoystickCalibration& cal) {
|
||||||
|
@ -116,7 +142,7 @@ bool runLeverCalibration(LinearSingleAxis& lever, LinearAnalogCalibration& cal)
|
||||||
void setup() {
|
void setup() {
|
||||||
// SERIAL INITIALIZE
|
// SERIAL INITIALIZE
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
ledMatrix.loadSequence(LEDMATRIX_ANIMATION_STARTUP);
|
ledMatrix.loadSequence(LEDMATRIX_ANIMATION_HEARTBEAT_LINE);
|
||||||
ledMatrix.begin();
|
ledMatrix.begin();
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
}
|
}
|
||||||
|
@ -125,47 +151,67 @@ void setup() {
|
||||||
// retrieve current calibration values
|
// retrieve current calibration values
|
||||||
NonVolatileMemory nvm = {};
|
NonVolatileMemory nvm = {};
|
||||||
readNvm(nvm);
|
readNvm(nvm);
|
||||||
|
Serial.println("Wait for serial connection...");
|
||||||
|
delay(5000);
|
||||||
|
|
||||||
// run joystick calibration if needed
|
// run joystick calibration if needed
|
||||||
bool calibrationInvalid = !nvm.valid();
|
// bool calibrationInvalid = !nvm.valid();
|
||||||
do {
|
// do {
|
||||||
calibrationInvalid |=
|
// calibrationInvalid |=
|
||||||
!nvm.calData.joystickCal.x.valid() || !nvm.calData.joystickCal.y.valid();
|
// !nvm.calData.joystickCal.x.valid() || !nvm.calData.joystickCal.y.valid();
|
||||||
if (calibrationInvalid) {
|
// if (calibrationInvalid) {
|
||||||
Serial.println("Joystick1 calibration required\n========================");
|
// Serial.println("Joystick1 calibration required\n========================");
|
||||||
calibrationInvalid |=
|
// calibrationInvalid =
|
||||||
!runJoystickCalibrationRoutine(joystick_1, nvm.calData.joystickCal);
|
// !runJoystickCalibrationRoutine(joystick_1, nvm.calData.joystickCal);
|
||||||
}
|
// }
|
||||||
} while (calibrationInvalid);
|
// } while (calibrationInvalid);
|
||||||
// ensure calibration has been applied
|
// // ensure calibration has been applied
|
||||||
// TODO: API doesnt allow setting configured midpoint...
|
// // TODO: API doesnt allow setting configured midpoint...
|
||||||
joystick_1.setCalibratedPoint(axis_t::X, point_t::MIN, nvm.calData.joystickCal.x.min);
|
// joystick_1.setCalibratedPoint(axis_t::X, point_t::MIN, nvm.calData.joystickCal.x.min);
|
||||||
joystick_1.setCalibratedPoint(axis_t::X, point_t::MID, nvm.calData.joystickCal.x.mid);
|
// joystick_1.setCalibratedPoint(axis_t::X, point_t::MID, nvm.calData.joystickCal.x.mid);
|
||||||
joystick_1.setCalibratedPoint(axis_t::X, point_t::MAX, nvm.calData.joystickCal.x.max);
|
// joystick_1.setCalibratedPoint(axis_t::X, point_t::MAX, nvm.calData.joystickCal.x.max);
|
||||||
joystick_1.setCalibratedPoint(axis_t::Y, point_t::MIN, nvm.calData.joystickCal.y.min);
|
// joystick_1.setCalibratedPoint(axis_t::Y, point_t::MIN, nvm.calData.joystickCal.y.min);
|
||||||
joystick_1.setCalibratedPoint(axis_t::Y, point_t::MID, nvm.calData.joystickCal.y.mid);
|
// joystick_1.setCalibratedPoint(axis_t::Y, point_t::MID, nvm.calData.joystickCal.y.mid);
|
||||||
joystick_1.setCalibratedPoint(axis_t::Y, point_t::MAX, nvm.calData.joystickCal.y.max);
|
// joystick_1.setCalibratedPoint(axis_t::Y, point_t::MAX, nvm.calData.joystickCal.y.max);
|
||||||
|
|
||||||
// run analog lever calibration if needed
|
// // run analog lever calibration if needed
|
||||||
calibrationInvalid = !nvm.valid();
|
// calibrationInvalid = !nvm.valid();
|
||||||
do {
|
// do {
|
||||||
calibrationInvalid |= !nvm.calData.lever0.valid();
|
// calibrationInvalid |= !nvm.calData.lever0.valid();
|
||||||
if (calibrationInvalid) {
|
// if (calibrationInvalid) {
|
||||||
Serial.println("Lever0 calibration required\n===========================");
|
// Serial.println("Lever0 calibration required\n===========================");
|
||||||
calibrationInvalid |= runLeverCalibration(lever0, nvm.calData.lever0);
|
// calibrationInvalid = !runLeverCalibration(lever0, nvm.calData.lever0);
|
||||||
}
|
// }
|
||||||
} while (calibrationInvalid);
|
// } while (calibrationInvalid);
|
||||||
lever0.applyCalibration(nvm.calData.lever0);
|
// lever0.applyCalibration(nvm.calData.lever0);
|
||||||
|
|
||||||
// write NVM in the event any values were updated
|
// // write NVM in the event any values were updated
|
||||||
writeNvm(nvm);
|
// writeNvm(nvm);
|
||||||
|
|
||||||
|
|
||||||
|
// configure buttons
|
||||||
|
for (size_t ii = 0; ii < NUM_BUTTONS; ii++) {
|
||||||
|
s_buttons[ii].attachClick(buttonClickHandler, (void*)ii);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int loopCount = 0;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
loopCount++;
|
||||||
|
if (loopCount >= 50) {
|
||||||
|
loopCount = 0;
|
||||||
// print joystick axes value
|
// print joystick axes value
|
||||||
Serial.print("joystick_1 X -> ");
|
Serial.print("joystick_1 X -> ");
|
||||||
Serial.print(joystick_1.read(X));
|
Serial.print(joystick_1.read(X));
|
||||||
Serial.print(" | Y -> ");
|
Serial.print(" | Y -> ");
|
||||||
Serial.println(joystick_1.read(Y));
|
Serial.println(joystick_1.read(Y));
|
||||||
delay(500);
|
Serial.print("lever0: ");
|
||||||
|
Serial.println(lever0.readRaw());
|
||||||
|
}
|
||||||
|
for (size_t ii = 0; ii < NUM_BUTTONS; ii++) {
|
||||||
|
s_buttons[ii].tick();
|
||||||
|
}
|
||||||
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue