keypad class
This commit is contained in:
@@ -8,17 +8,10 @@
|
||||
|
||||
static LGFX gfx;
|
||||
|
||||
constexpr float FONT_SIZE_NORMAL = 4.0;
|
||||
constexpr float FONT_SIZE_LARGE = 8.0;
|
||||
|
||||
static const float_t BUTTON_WIDTH = 115;
|
||||
static const float_t BUTTON_HEIGHT = 115;
|
||||
static int last_index = -1;
|
||||
|
||||
static String oldinput = "";
|
||||
static String input = "";
|
||||
|
||||
int index(int x, int y)
|
||||
int Keypad::get_button_index_from_touch(int x, int y)
|
||||
{
|
||||
for (int index = 0; index <= 11; index++)
|
||||
{
|
||||
@@ -34,10 +27,26 @@ int index(int x, int y)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void drawButton(int index, String label)
|
||||
void Keypad::draw_button(int i)
|
||||
{
|
||||
float_t x = (125 * (index % 3)) + 15;
|
||||
float_t y = (125 * (index / 3)) + 20;
|
||||
gfx.setTextSize(FONT_SIZE_NORMAL);
|
||||
|
||||
String label = String(i + 1);
|
||||
if (i == 9)
|
||||
{
|
||||
label = String("<");
|
||||
}
|
||||
if (i == 10)
|
||||
{
|
||||
label = String("0");
|
||||
}
|
||||
if (i == 11)
|
||||
{
|
||||
label = String(">");
|
||||
}
|
||||
|
||||
float_t x = (125 * (i % 3)) + 15;
|
||||
float_t y = (125 * (i / 3)) + 20;
|
||||
|
||||
gfx.setCursor(x + (115 / 2) - (gfx.fontWidth() / 2), y + (115 / 2) - (gfx.fontHeight() / 2));
|
||||
gfx.print(label);
|
||||
@@ -49,30 +58,7 @@ void drawButton(int index, String label)
|
||||
gfx.drawFastVLine(x + BUTTON_WIDTH, y, BUTTON_HEIGHT);
|
||||
}
|
||||
|
||||
void drawButtons()
|
||||
{
|
||||
gfx.setTextSize(FONT_SIZE_NORMAL);
|
||||
for (int i = 0; i <= 11; i++)
|
||||
{
|
||||
String label = String(i + 1);
|
||||
if (i == 9)
|
||||
{
|
||||
label = String("<");
|
||||
}
|
||||
if (i == 10)
|
||||
{
|
||||
label = String("0");
|
||||
}
|
||||
if (i == 11)
|
||||
{
|
||||
label = String(">");
|
||||
}
|
||||
|
||||
drawButton(i, label);
|
||||
}
|
||||
}
|
||||
|
||||
void drawInputField()
|
||||
void Keypad::draw_input_field()
|
||||
{
|
||||
int x = 405;
|
||||
int y = 20;
|
||||
@@ -84,7 +70,7 @@ void drawInputField()
|
||||
gfx.drawFastVLine(x + 520, y, 115);
|
||||
}
|
||||
|
||||
void drawInput()
|
||||
void Keypad::draw_input()
|
||||
{
|
||||
int x = 405;
|
||||
int y = 20;
|
||||
@@ -110,11 +96,8 @@ void drawInput()
|
||||
gfx.setEpdMode(epd_mode_t::epd_fast);
|
||||
}
|
||||
|
||||
void keypad_init()
|
||||
void Keypad::draw()
|
||||
{
|
||||
gfx.init();
|
||||
gfx.setRotation(1);
|
||||
|
||||
gfx.setEpdMode(epd_mode_t::epd_quality);
|
||||
gfx.fillScreen(TFT_WHITE);
|
||||
gfx.waitDisplay();
|
||||
@@ -124,12 +107,24 @@ void keypad_init()
|
||||
gfx.setColor(TFT_BLACK);
|
||||
gfx.waitDisplay();
|
||||
|
||||
drawButtons();
|
||||
drawInputField();
|
||||
drawInput();
|
||||
for (int i = 0; i <= 11; i++)
|
||||
{
|
||||
this->draw_button(i);
|
||||
}
|
||||
|
||||
draw_input_field();
|
||||
draw_input();
|
||||
}
|
||||
|
||||
void keypad_write(String payload)
|
||||
Keypad::Keypad()
|
||||
{
|
||||
gfx.init();
|
||||
gfx.setRotation(1);
|
||||
|
||||
draw();
|
||||
}
|
||||
|
||||
void Keypad::write(String payload)
|
||||
{
|
||||
payload.replace("_", " ");
|
||||
|
||||
@@ -144,7 +139,7 @@ void keypad_write(String payload)
|
||||
gfx.printf("%s", payload);
|
||||
}
|
||||
|
||||
std::optional<String> checkForInput()
|
||||
std::optional<String> Keypad::check_input()
|
||||
{
|
||||
if (M5.TP.available())
|
||||
{
|
||||
@@ -157,7 +152,7 @@ std::optional<String> checkForInput()
|
||||
|
||||
tp_finger_t fingerItem = M5.TP.readFinger(0);
|
||||
|
||||
int i = index(fingerItem.x, fingerItem.y);
|
||||
int i = get_button_index_from_touch(fingerItem.x, fingerItem.y);
|
||||
if (last_index != i)
|
||||
{
|
||||
last_index = i;
|
||||
@@ -190,12 +185,12 @@ std::optional<String> checkForInput()
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<String> keypad_loop()
|
||||
std::optional<String> Keypad::get_input()
|
||||
{
|
||||
auto value = checkForInput();
|
||||
auto value = check_input();
|
||||
if (oldinput != input)
|
||||
{
|
||||
drawInput();
|
||||
draw_input();
|
||||
oldinput = input;
|
||||
}
|
||||
|
||||
|
@@ -4,8 +4,28 @@
|
||||
#include <WString.h>
|
||||
#include <optional>
|
||||
|
||||
void keypad_init();
|
||||
void keypad_write(String);
|
||||
std::optional<String> keypad_loop();
|
||||
constexpr float FONT_SIZE_NORMAL = 4.0;
|
||||
constexpr float FONT_SIZE_LARGE = 8.0;
|
||||
|
||||
class Keypad
|
||||
{
|
||||
public:
|
||||
Keypad();
|
||||
int get_button_index_from_touch(int x, int y);
|
||||
void draw();
|
||||
void write(String);
|
||||
std::optional<String> get_input();
|
||||
|
||||
private:
|
||||
int last_index = -1;
|
||||
String oldinput = "";
|
||||
String input = "";
|
||||
|
||||
void draw_button(int index);
|
||||
void draw_input_field();
|
||||
void draw_input();
|
||||
|
||||
std::optional<String> check_input();
|
||||
};
|
||||
|
||||
#endif
|
45
src/main.cpp
45
src/main.cpp
@@ -17,6 +17,7 @@
|
||||
static WiFiUDP ntpUDP;
|
||||
static NTPClient timeClient(ntpUDP);
|
||||
static MQTT *mqtt;
|
||||
static Keypad *keypad;
|
||||
|
||||
static bool is_disarmed = false;
|
||||
|
||||
@@ -28,13 +29,13 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length)
|
||||
String state((const char *)payload);
|
||||
is_disarmed = state == "disarmed";
|
||||
|
||||
Serial.println(state);
|
||||
keypad_write(state);
|
||||
}
|
||||
if (state == "armed_away")
|
||||
{
|
||||
keypad->draw();
|
||||
}
|
||||
|
||||
void setupTime()
|
||||
{
|
||||
timeClient.begin();
|
||||
Serial.println(state);
|
||||
keypad->write(state);
|
||||
}
|
||||
|
||||
void lock()
|
||||
@@ -55,18 +56,18 @@ void initWifi(Settings *settings)
|
||||
{
|
||||
if (!settings)
|
||||
{
|
||||
keypad_write("no settings");
|
||||
keypad->write("no settings");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isConnecting)
|
||||
{
|
||||
keypad_write("already connecting");
|
||||
keypad->write("already connecting");
|
||||
return;
|
||||
}
|
||||
|
||||
isConnecting = true;
|
||||
keypad_write("connecting");
|
||||
keypad->write("connecting");
|
||||
|
||||
WiFi.begin(settings->gettext("wifi:ssid"), settings->gettext("wifi:password"));
|
||||
|
||||
@@ -88,11 +89,11 @@ void initWifi(Settings *settings)
|
||||
Serial.print("Local IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
isConnecting = false;
|
||||
keypad_write("connected");
|
||||
keypad->write("connected");
|
||||
}
|
||||
else
|
||||
{
|
||||
keypad_write("failed to connect");
|
||||
keypad->write("failed to connect");
|
||||
isConnecting = false;
|
||||
return;
|
||||
}
|
||||
@@ -115,14 +116,14 @@ void setup()
|
||||
{
|
||||
M5.begin();
|
||||
|
||||
keypad_init();
|
||||
keypad_write("loading");
|
||||
keypad = new Keypad();
|
||||
keypad->write("loading");
|
||||
|
||||
auto settings = new Settings();
|
||||
|
||||
if (!settings)
|
||||
{
|
||||
keypad_write("unable to load settings");
|
||||
keypad->write("unable to load settings");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -130,7 +131,7 @@ void setup()
|
||||
|
||||
initWifi(settings);
|
||||
mqtt->connect();
|
||||
setupTime();
|
||||
timeClient.begin();
|
||||
initTOTP(settings);
|
||||
}
|
||||
|
||||
@@ -148,24 +149,12 @@ void submit(String code)
|
||||
lock();
|
||||
}
|
||||
|
||||
// unsigned long lastBtnPressed = millis();
|
||||
// void checkForButton()
|
||||
// {
|
||||
// M5.BtnP.read();
|
||||
// if (M5.BtnP.isPressed() && millis() - lastBtnPressed > 1000)
|
||||
// {
|
||||
// lastBtnPressed = millis(); // try and debouce, not really working i guess
|
||||
// initWifi();
|
||||
// }
|
||||
// }
|
||||
|
||||
void loop()
|
||||
{
|
||||
timeClient.update();
|
||||
mqtt->loop();
|
||||
|
||||
// checkForButton();
|
||||
auto value = keypad_loop();
|
||||
auto value = keypad->get_input();
|
||||
if (value.has_value())
|
||||
submit(value.value());
|
||||
}
|
||||
|
Reference in New Issue
Block a user