DDU_Exammen/ModuleMath/InputSystem.h

65 lines
1.3 KiB
C

#include <M5StickCPlus.h>
#define buttonPin 25
#define inputPauseRegisterMills 1500
typedef void (*OnFinishInputMode)(int);
OnFinishInputMode onFinishInputMode = nullptr;
int pressedTimes = 0;
bool isInputMode = false;
long lastInputTime = 0;
void onPressed(){
if (!isInputMode)
{
isInputMode = true;
}
lastInputTime = millis();
pressedTimes++;
}
// void m_onFinishInputMode(){
// M5.Lcd.fillScreen(WHITE);
// M5.Lcd.setTextPadding(10);
// M5.Lcd.setCursor(10, 10);
// M5.Lcd.setTextSize(6);
// M5.Lcd.print(pressedTimes);
// Serial.println("Registed: " + pressedTimes);
// // Reset count
// pressedTimes = 0;
// }
bool buttonDowned = false;
void updateInput()
{
// Detect button down
if (!buttonDowned && digitalRead(buttonPin) == LOW)
{
buttonDowned = true;
onPressed();
}
else if (buttonDowned && digitalRead(buttonPin) == HIGH)
{
buttonDowned = false;
}
// Detect if no input for 3 seconds
if (isInputMode && millis() - lastInputTime > inputPauseRegisterMills)
{
isInputMode = false;
if (onFinishInputMode != nullptr)
onFinishInputMode(pressedTimes);
pressedTimes = false;
}
}
void initInputSystem(OnFinishInputMode callback)
{
onFinishInputMode = callback;
}