large refactor
This commit is contained in:
176
src/main.cpp
176
src/main.cpp
@@ -3,21 +3,42 @@
|
||||
|
||||
#define YAML_DISABLE_CJSON
|
||||
|
||||
#include <WiFi.h>
|
||||
#define LGFX_M5PAPER
|
||||
#define LGFX_USE_V1
|
||||
#define LGFX_AUTODETECT
|
||||
|
||||
#include <M5EPD.h>
|
||||
#include <WiFi.h>
|
||||
#include <NTPClient.h>
|
||||
#include <HTTPClient.h>
|
||||
|
||||
#include <totp.hpp>
|
||||
|
||||
#include "display.hpp"
|
||||
#include "keypad.hpp"
|
||||
#include "input.hpp"
|
||||
#include "status.hpp"
|
||||
#include "touch.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "mqtt.hpp"
|
||||
#include "keypad.hpp"
|
||||
|
||||
static WiFiUDP ntpUDP;
|
||||
static NTPClient timeClient(ntpUDP);
|
||||
static MQTT *mqtt;
|
||||
|
||||
static Display display;
|
||||
static Keypad *keypad;
|
||||
static Status *status;
|
||||
static Input *input;
|
||||
static Touch *touch;
|
||||
|
||||
void keypad_state(bool enabled)
|
||||
{
|
||||
for (int i = 0; i < 11; i++)
|
||||
{
|
||||
keypad->set_enabled(i, enabled);
|
||||
}
|
||||
keypad->set_enabled(11, !enabled);
|
||||
}
|
||||
|
||||
static bool is_disarmed = false;
|
||||
|
||||
@@ -29,13 +50,13 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length)
|
||||
String state((const char *)payload);
|
||||
is_disarmed = state == "disarmed";
|
||||
|
||||
if (state == "armed_away")
|
||||
{
|
||||
keypad->draw();
|
||||
}
|
||||
|
||||
state.replace("_", " ");
|
||||
state[0] = toupper(state[0]);
|
||||
Serial.println(state);
|
||||
keypad->write(state);
|
||||
status->set(state);
|
||||
status->draw(&display);
|
||||
|
||||
keypad_state(!is_disarmed);
|
||||
}
|
||||
|
||||
void lock()
|
||||
@@ -51,23 +72,37 @@ void unlock()
|
||||
mqtt->send("DISARM");
|
||||
}
|
||||
|
||||
void submit(String code)
|
||||
{
|
||||
uint32_t newCode = getCodeFromTimestamp(timeClient.getEpochTime());
|
||||
uint32_t input = code.toInt();
|
||||
|
||||
if (newCode != input)
|
||||
{
|
||||
status->set("Invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
bool isConnecting = false;
|
||||
void initWifi(Settings *settings)
|
||||
{
|
||||
if (!settings)
|
||||
{
|
||||
keypad->write("no settings");
|
||||
status->set("No settings");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isConnecting)
|
||||
{
|
||||
keypad->write("already connecting");
|
||||
status->set("Already connecting");
|
||||
return;
|
||||
}
|
||||
|
||||
isConnecting = true;
|
||||
keypad->write("connecting");
|
||||
status->set("Connecting");
|
||||
|
||||
WiFi.begin(settings->gettext("wifi:ssid"), settings->gettext("wifi:password"));
|
||||
|
||||
@@ -89,11 +124,11 @@ void initWifi(Settings *settings)
|
||||
Serial.print("Local IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
isConnecting = false;
|
||||
keypad->write("connected");
|
||||
status->set("Connected");
|
||||
}
|
||||
else
|
||||
{
|
||||
keypad->write("failed to connect");
|
||||
status->set("Failed to connect");
|
||||
isConnecting = false;
|
||||
return;
|
||||
}
|
||||
@@ -112,49 +147,116 @@ void initTOTP(Settings *settings)
|
||||
TOTP(base32_key, 20, 30);
|
||||
}
|
||||
|
||||
void p(int v)
|
||||
{
|
||||
switch (v)
|
||||
{
|
||||
case 9:
|
||||
input->clear();
|
||||
break;
|
||||
case 10:
|
||||
input->append("0");
|
||||
break;
|
||||
case 11:
|
||||
lock();
|
||||
break;
|
||||
default:
|
||||
input->append(v + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
if (input->length() >= 6)
|
||||
{
|
||||
// verify
|
||||
input->draw(&display); // force one more redraw for the last character
|
||||
submit(input->get_value());
|
||||
input->clear();
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
M5.begin();
|
||||
|
||||
keypad = new Keypad();
|
||||
keypad->write("loading");
|
||||
display.init();
|
||||
touch = new Touch(M5.TP);
|
||||
status = new Status("Loading");
|
||||
keypad = new Keypad(&display);
|
||||
keypad->addListener("pressed", p);
|
||||
input = new Input("", keypad->get_width(), 0, display.width() - keypad->get_width(), 115);
|
||||
|
||||
auto settings = new Settings();
|
||||
|
||||
if (!settings)
|
||||
{
|
||||
keypad->write("unable to load settings");
|
||||
status->set("Settings Error");
|
||||
return;
|
||||
}
|
||||
|
||||
mqtt = new MQTT(settings, mqtt_callback);
|
||||
|
||||
initWifi(settings);
|
||||
mqtt->connect();
|
||||
timeClient.begin();
|
||||
initTOTP(settings);
|
||||
}
|
||||
|
||||
void submit(String code)
|
||||
{
|
||||
uint32_t newCode = getCodeFromTimestamp(timeClient.getEpochTime());
|
||||
uint32_t input = code.toInt();
|
||||
|
||||
if (newCode == input)
|
||||
{
|
||||
unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
lock();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
timeClient.update();
|
||||
mqtt->loop();
|
||||
|
||||
auto value = keypad->get_input();
|
||||
if (value.has_value())
|
||||
submit(value.value());
|
||||
auto tap = touch->tap();
|
||||
if (tap.has_value())
|
||||
keypad->update(tap.value());
|
||||
|
||||
keypad->draw();
|
||||
input->draw(&display);
|
||||
status->draw(&display);
|
||||
}
|
||||
|
||||
// static Keypad *keypad;
|
||||
|
||||
// void setup()
|
||||
// {
|
||||
// M5.begin();
|
||||
|
||||
// keypad = new Keypad();
|
||||
// status->set("loading");
|
||||
|
||||
// auto settings = new Settings();
|
||||
|
||||
// if (!settings)
|
||||
// {
|
||||
// status->set("unable to load settings");
|
||||
// return;
|
||||
// }
|
||||
|
||||
// mqtt = new MQTT(settings, mqtt_callback);
|
||||
|
||||
// initWifi(settings);
|
||||
// mqtt->connect();
|
||||
// timeClient.begin();
|
||||
// initTOTP(settings);
|
||||
// }
|
||||
|
||||
// void submit(String code)
|
||||
// {
|
||||
// uint32_t newCode = getCodeFromTimestamp(timeClient.getEpochTime());
|
||||
// uint32_t input = code.toInt();
|
||||
|
||||
// if (newCode == input)
|
||||
// {
|
||||
// unlock();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// lock();
|
||||
// }
|
||||
|
||||
// void loop()
|
||||
// {
|
||||
// timeClient.update();
|
||||
// mqtt->loop();
|
||||
|
||||
// auto value = keypad->get_input();
|
||||
// if (value.has_value())
|
||||
// submit(value.value());
|
||||
// }
|
||||
|
Reference in New Issue
Block a user