92 lines
1.6 KiB
C++
92 lines
1.6 KiB
C++
#include <M5StickCPlus2.h>
|
|
#include "InputSystem.h"
|
|
#include "MathSystem.h"
|
|
|
|
#define extenderPin 26
|
|
#define buttonPin 25
|
|
|
|
#define UIRefreshDelay 250
|
|
|
|
String mathExpression = "";
|
|
bool hasWon = false;
|
|
|
|
void onInputDone(int inputNum){
|
|
Serial.println("Reveiced: " + String(inputNum));
|
|
Serial.println("Target: " + String(targetResult));
|
|
|
|
if (inputNum == targetResult){
|
|
hasWon = true;
|
|
}
|
|
|
|
if (!hasWon){
|
|
M5.Lcd.fillScreen(RED);
|
|
delay(1000);
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
M5.begin();
|
|
M5.Lcd.setRotation(1);
|
|
|
|
initRandomSeed();
|
|
|
|
digitalWrite(extenderPin, HIGH); // Push out
|
|
|
|
pinMode(extenderPin, OUTPUT_OPEN_DRAIN);
|
|
pinMode(buttonPin, INPUT_PULLUP);
|
|
|
|
Serial.begin(115200);
|
|
mathExpression = generateQuestion();
|
|
|
|
initInputSystem(*onInputDone);
|
|
}
|
|
|
|
void drawUI(){
|
|
if (!hasWon)
|
|
{
|
|
M5.Lcd.fillScreen(BLACK);
|
|
|
|
if (!isInputMode)
|
|
{
|
|
// Draw expression
|
|
M5.Lcd.setCursor(10, 50);
|
|
M5.Lcd.setTextSize(4);
|
|
M5.Lcd.print(mathExpression);
|
|
}
|
|
else
|
|
{
|
|
// Draw pressed times
|
|
M5.Lcd.setCursor(80, 40);
|
|
M5.Lcd.setTextSize(7);
|
|
M5.Lcd.print(pressedTimes);
|
|
}
|
|
}
|
|
else {
|
|
M5.Lcd.fillScreen(GREEN);
|
|
digitalWrite(extenderPin, LOW);
|
|
|
|
// Wait for reset click
|
|
while (digitalRead(buttonPin) == HIGH){
|
|
delay(10);
|
|
}
|
|
while (digitalRead(buttonPin) == LOW)
|
|
{
|
|
delay(10);
|
|
}
|
|
|
|
// Reset
|
|
digitalWrite(extenderPin, HIGH);
|
|
mathExpression = generateQuestion();
|
|
hasWon = false;
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
updateInput();
|
|
|
|
if (isInputMode || millis() % UIRefreshDelay < 100)
|
|
drawUI();
|
|
|
|
delay(25);
|
|
}
|