clean up input

main
radex 2024-02-21 22:35:38 +01:00
parent d006b7a457
commit 1914c026d2
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
7 changed files with 70 additions and 33 deletions

View File

@ -19,4 +19,4 @@ lib_deps =
robtillaart/PCF8574@^0.4.1
robtillaart/AS5600@^0.6.0
monitor_speed = 2000000
monitor_speed = 1500000

View File

@ -3,6 +3,7 @@
// --- debug config
#define DEBUG_I2C 0
#define DEBUG_PRINT_EVENTS 1
// --- pinout

View File

@ -8,11 +8,12 @@ I2CScanner scanner;
#include "screen.h"
#include "keyboard.h"
#include "stepper.h"
#include "input.h"
void setupDebug() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(2000000);
Serial.begin(1500000);
Serial.println("it's cewk-o-mator, hello!");
#if DEBUG_I2C
scanner.Init();
@ -79,7 +80,7 @@ void handleDebugFunctions(char key) {
Serial.println(micros2 - micros1);
micros1 = micros();
auto key = handleKeyboard();
handleKeyboard();
micros2 = micros();
Serial.print("Keyboard scan time (microseconds): ");
Serial.println(micros2 - micros1);
@ -91,3 +92,32 @@ void handleDebugFunctions(char key) {
setTimerEnabled(!isTimerEnabled());
}
}
void debugHandleInput(Input input) {
handleDebugFunctions(input.debugKey);
#if DEBUG_PRINT_EVENTS
if (input.key) {
Serial.print("Key pressed: ");
Serial.print(allKeys[input.key]);
auto digit = getPressedDigit(input.key);
if (digit != KEY_NONE) {
Serial.print(", digit: ");
Serial.print(digit);
} else {
Serial.print(" (Not a digit)");
}
Serial.println();
}
if (input.switchEvent) {
Serial.println(input.switchEvent == SWITCH_PRESSED ? "Pressed" : "Released");
}
if (input.encoderEvent) {
Serial.println(input.encoderEvent == ENCODER_CLOCKWISE ? "Clockwise" : "Counter-clockwise");
}
#endif
}

View File

@ -1,4 +1,5 @@
#pragma once
struct Input;
void setupDebug();
void debugScanI2C();
@ -18,4 +19,4 @@ void fatal();
*/
char getDebugKey();
void handleDebugFunctions(char key);
void debugHandleInput(Input input);

12
firmware/src/input.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "debug.h"
#include "input.h"
Input getInput() {
auto debugKey = getDebugKey();
auto key = handleDebugKeyboard(debugKey);
auto switchEvent = handleDebugSwitch(debugKey);
auto encoderEvent = handleDebugEncoder(debugKey);
bool hasInput = debugKey || key || switchEvent || encoderEvent;
return {debugKey, key, switchEvent, encoderEvent, hasInput};
}

15
firmware/src/input.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <Arduino.h>
#include "keyboard.h"
#include "encoder.h"
struct Input {
char debugKey;
Key key;
SwitchEvent switchEvent;
EncoderEvent encoderEvent;
bool hasInput;
};
Input getInput();

View File

@ -9,6 +9,7 @@
#include "debug.h"
#include "stepper.h"
#include "memes.h"
#include "input.h"
void setup() {
setupDebug();
@ -30,10 +31,12 @@ void setup() {
splashScreen();
}
/*
bool stepperDemo = false;
bool stepperDemoSlowing = false;
bool stepperDemoLongStretch = false;
unsigned long lastStepperDemoMillis = 0;
*/
void loop() {
debugScanI2C();
@ -76,35 +79,10 @@ void loop() {
}
*/
auto debugKey = getDebugKey();
auto key = handleDebugKeyboard(debugKey);
auto switchEvent = handleDebugSwitch(debugKey);
auto encoderEvent = handleDebugEncoder(debugKey);
if (debugKey || key || switchEvent || encoderEvent) {
auto input = getInput();
if (input.hasInput) {
tick();
}
handleDebugFunctions(debugKey);
if (key) {
Serial.print("Key pressed: ");
Serial.print(allKeys[key]);
auto digit = getPressedDigit(key);
if (digit != KEY_NONE) {
Serial.print(", digit: ");
Serial.print(digit);
} else {
Serial.print(" (Not a digit)");
}
Serial.println();
}
if (switchEvent) {
Serial.println(switchEvent == SWITCH_PRESSED ? "Pressed" : "Released");
}
if (encoderEvent) {
Serial.println(encoderEvent == ENCODER_CLOCKWISE ? "Clockwise" : "Counter-clockwise");
}
debugHandleInput(input);
}