160 lines
2.9 KiB
C++
160 lines
2.9 KiB
C++
//#define UseMQTT
|
|
#ifdef UseMQTT
|
|
#include <WiFi.h>
|
|
#include <WebSocketsClient.h> // include before MQTTPubSubClient.h
|
|
#include <MQTTPubSubClient.h>
|
|
|
|
const char *ssid = "AIRPORT-FREE-WIFI-NEW";
|
|
const char *pass = "alexalex";
|
|
|
|
WebSocketsClient client;
|
|
MQTTPubSubClient mqtt;
|
|
#endif
|
|
|
|
#include <M5StickCPlus2.h>
|
|
#include "InputSystem.h"
|
|
#include "MathSystem.h"
|
|
|
|
#define extenderPin 32
|
|
#define buttonPin 25
|
|
|
|
#define UIRefreshDelay 250
|
|
|
|
String mathExpression = "";
|
|
bool hasWon = false;
|
|
|
|
void connect()
|
|
{
|
|
#ifdef UseMQTT
|
|
connect_to_wifi:
|
|
Serial.print("connecting to wifi...");
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
Serial.print(".");
|
|
delay(1000);
|
|
}
|
|
Serial.println(" connected!");
|
|
|
|
connect_to_host:
|
|
Serial.println("connecting to host...");
|
|
client.disconnect();
|
|
client.begin("gitea.deprived.dev", 6667, "/", "mqtt"); // "mqtt" is required
|
|
client.setReconnectInterval(2000);
|
|
|
|
Serial.print("connecting to mqtt broker...");
|
|
while (!mqtt.connect("Module-Math", "alex", "1111"))
|
|
{
|
|
Serial.print(".");
|
|
delay(1000);
|
|
if (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
Serial.println("WiFi disconnected");
|
|
goto connect_to_wifi;
|
|
}
|
|
if (!client.isConnected())
|
|
{
|
|
Serial.println("WebSocketsClient disconnected");
|
|
goto connect_to_host;
|
|
}
|
|
}
|
|
Serial.println(" connected!");
|
|
#endif
|
|
}
|
|
|
|
void onInputDone(int inputNum){
|
|
Serial.println("Reveiced: " + String(inputNum));
|
|
Serial.println("Target: " + String(targetResult));
|
|
|
|
if (inputNum == targetResult){
|
|
hasWon = true;
|
|
#ifdef UseMQTT
|
|
mqtt.publish("/DDUStatusPing", "winMath");
|
|
#endif
|
|
}
|
|
|
|
if (!hasWon){
|
|
M5.Lcd.fillScreen(RED);
|
|
delay(1000);
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
#ifdef UseMQTT
|
|
WiFi.begin(ssid, pass);
|
|
|
|
// initialize mqtt client
|
|
mqtt.begin(client);
|
|
|
|
// connect to wifi, host and mqtt broker
|
|
connect();
|
|
#endif
|
|
|
|
M5.begin();
|
|
M5.Lcd.setRotation(3);
|
|
|
|
initRandomSeed();
|
|
|
|
|
|
pinMode(extenderPin, OUTPUT_OPEN_DRAIN);
|
|
pinMode(buttonPin, INPUT_PULLUP);
|
|
|
|
digitalWrite(extenderPin, HIGH); // Push out
|
|
|
|
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();
|
|
#ifdef UseMQTT
|
|
mqtt.update();
|
|
#endif
|
|
|
|
if (isInputMode || millis() % UIRefreshDelay < 100)
|
|
drawUI();
|
|
|
|
delay(25);
|
|
}
|