testing button hardware+event handling library
This commit is contained in:
parent
baf657a5f4
commit
ed7a3e151f
|
@ -12,3 +12,6 @@
|
|||
platform = renesas-ra
|
||||
board = uno_r4_wifi
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
mathertel/OneButton@^2.6.1
|
||||
|
|
126
src/main.cpp
126
src/main.cpp
|
@ -3,15 +3,41 @@
|
|||
#include "Arduino_LED_Matrix.h"
|
||||
#include "NvmEeprom.hpp"
|
||||
#include "SingleAxis.hpp"
|
||||
#include "OneButton.h"
|
||||
|
||||
// joystick calibration
|
||||
#define TIME_CAL_1 2000
|
||||
#define TIME_CAL_2 3000
|
||||
|
||||
// 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;
|
||||
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
|
||||
bool runJoystickCalibrationRoutine(AlignedJoy& joy, JoystickCalibration& cal) {
|
||||
|
@ -80,7 +106,7 @@ bool runJoystickCalibrationRoutine(AlignedJoy& joy, JoystickCalibration& cal) {
|
|||
Serial.print(joy.getCalibratedPoint(X, MID));
|
||||
Serial.print(" | max -> ");
|
||||
Serial.println(joy.getCalibratedPoint(X, MAX));
|
||||
Serial.print("Y min -> ");
|
||||
Serial.print(" Y min -> ");
|
||||
Serial.print(joy.getCalibratedPoint(Y, MIN));
|
||||
Serial.print(" | center -> ");
|
||||
Serial.print(joy.getCalibratedPoint(Y, MID));
|
||||
|
@ -116,7 +142,7 @@ bool runLeverCalibration(LinearSingleAxis& lever, LinearAnalogCalibration& cal)
|
|||
void setup() {
|
||||
// SERIAL INITIALIZE
|
||||
Serial.begin(115200);
|
||||
ledMatrix.loadSequence(LEDMATRIX_ANIMATION_STARTUP);
|
||||
ledMatrix.loadSequence(LEDMATRIX_ANIMATION_HEARTBEAT_LINE);
|
||||
ledMatrix.begin();
|
||||
while (!Serial) {
|
||||
}
|
||||
|
@ -125,47 +151,67 @@ void setup() {
|
|||
// retrieve current calibration values
|
||||
NonVolatileMemory nvm = {};
|
||||
readNvm(nvm);
|
||||
Serial.println("Wait for serial connection...");
|
||||
delay(5000);
|
||||
|
||||
// run joystick calibration if needed
|
||||
bool calibrationInvalid = !nvm.valid();
|
||||
do {
|
||||
calibrationInvalid |=
|
||||
!nvm.calData.joystickCal.x.valid() || !nvm.calData.joystickCal.y.valid();
|
||||
if (calibrationInvalid) {
|
||||
Serial.println("Joystick1 calibration required\n========================");
|
||||
calibrationInvalid |=
|
||||
!runJoystickCalibrationRoutine(joystick_1, nvm.calData.joystickCal);
|
||||
}
|
||||
} while (calibrationInvalid);
|
||||
// ensure calibration has been applied
|
||||
// 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::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::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::MAX, nvm.calData.joystickCal.y.max);
|
||||
// bool calibrationInvalid = !nvm.valid();
|
||||
// do {
|
||||
// calibrationInvalid |=
|
||||
// !nvm.calData.joystickCal.x.valid() || !nvm.calData.joystickCal.y.valid();
|
||||
// if (calibrationInvalid) {
|
||||
// Serial.println("Joystick1 calibration required\n========================");
|
||||
// calibrationInvalid =
|
||||
// !runJoystickCalibrationRoutine(joystick_1, nvm.calData.joystickCal);
|
||||
// }
|
||||
// } while (calibrationInvalid);
|
||||
// // ensure calibration has been applied
|
||||
// // 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::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::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::MAX, nvm.calData.joystickCal.y.max);
|
||||
|
||||
// run analog lever calibration if needed
|
||||
calibrationInvalid = !nvm.valid();
|
||||
do {
|
||||
calibrationInvalid |= !nvm.calData.lever0.valid();
|
||||
if (calibrationInvalid) {
|
||||
Serial.println("Lever0 calibration required\n===========================");
|
||||
calibrationInvalid |= runLeverCalibration(lever0, nvm.calData.lever0);
|
||||
}
|
||||
} while (calibrationInvalid);
|
||||
lever0.applyCalibration(nvm.calData.lever0);
|
||||
// // run analog lever calibration if needed
|
||||
// calibrationInvalid = !nvm.valid();
|
||||
// do {
|
||||
// calibrationInvalid |= !nvm.calData.lever0.valid();
|
||||
// if (calibrationInvalid) {
|
||||
// Serial.println("Lever0 calibration required\n===========================");
|
||||
// calibrationInvalid = !runLeverCalibration(lever0, nvm.calData.lever0);
|
||||
// }
|
||||
// } while (calibrationInvalid);
|
||||
// lever0.applyCalibration(nvm.calData.lever0);
|
||||
|
||||
// // write NVM in the event any values were updated
|
||||
// writeNvm(nvm);
|
||||
|
||||
|
||||
// configure buttons
|
||||
for (size_t ii = 0; ii < NUM_BUTTONS; ii++) {
|
||||
s_buttons[ii].attachClick(buttonClickHandler, (void*)ii);
|
||||
}
|
||||
|
||||
// write NVM in the event any values were updated
|
||||
writeNvm(nvm);
|
||||
}
|
||||
|
||||
int loopCount = 0;
|
||||
|
||||
void loop() {
|
||||
// print joystick axes value
|
||||
Serial.print("joystick_1 X -> ");
|
||||
Serial.print(joystick_1.read(X));
|
||||
Serial.print(" | Y -> ");
|
||||
Serial.println(joystick_1.read(Y));
|
||||
delay(500);
|
||||
loopCount++;
|
||||
if (loopCount >= 50) {
|
||||
loopCount = 0;
|
||||
// print joystick axes value
|
||||
Serial.print("joystick_1 X -> ");
|
||||
Serial.print(joystick_1.read(X));
|
||||
Serial.print(" | Y -> ");
|
||||
Serial.println(joystick_1.read(Y));
|
||||
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