keypad class

This commit is contained in:
2025-07-03 23:25:28 +02:00
parent 077000e583
commit 1b31e0c790
3 changed files with 84 additions and 80 deletions

View File

@@ -8,17 +8,10 @@
static LGFX gfx; 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_WIDTH = 115;
static const float_t BUTTON_HEIGHT = 115; static const float_t BUTTON_HEIGHT = 115;
static int last_index = -1;
static String oldinput = ""; int Keypad::get_button_index_from_touch(int x, int y)
static String input = "";
int index(int x, int y)
{ {
for (int index = 0; index <= 11; index++) for (int index = 0; index <= 11; index++)
{ {
@@ -34,26 +27,10 @@ int index(int x, int y)
return -1; 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.setCursor(x + (115 / 2) - (gfx.fontWidth() / 2), y + (115 / 2) - (gfx.fontHeight() / 2));
gfx.print(label);
gfx.setColor(TFT_BLACK);
gfx.drawFastHLine(x, y, BUTTON_WIDTH);
gfx.drawFastHLine(x, y + BUTTON_HEIGHT, BUTTON_WIDTH);
gfx.drawFastVLine(x, y, BUTTON_HEIGHT);
gfx.drawFastVLine(x + BUTTON_WIDTH, y, BUTTON_HEIGHT);
}
void drawButtons()
{ {
gfx.setTextSize(FONT_SIZE_NORMAL); gfx.setTextSize(FONT_SIZE_NORMAL);
for (int i = 0; i <= 11; i++)
{
String label = String(i + 1); String label = String(i + 1);
if (i == 9) if (i == 9)
{ {
@@ -68,11 +45,20 @@ void drawButtons()
label = String(">"); label = String(">");
} }
drawButton(i, label); 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);
gfx.setColor(TFT_BLACK);
gfx.drawFastHLine(x, y, BUTTON_WIDTH);
gfx.drawFastHLine(x, y + BUTTON_HEIGHT, BUTTON_WIDTH);
gfx.drawFastVLine(x, y, BUTTON_HEIGHT);
gfx.drawFastVLine(x + BUTTON_WIDTH, y, BUTTON_HEIGHT);
} }
void drawInputField() void Keypad::draw_input_field()
{ {
int x = 405; int x = 405;
int y = 20; int y = 20;
@@ -84,7 +70,7 @@ void drawInputField()
gfx.drawFastVLine(x + 520, y, 115); gfx.drawFastVLine(x + 520, y, 115);
} }
void drawInput() void Keypad::draw_input()
{ {
int x = 405; int x = 405;
int y = 20; int y = 20;
@@ -110,11 +96,8 @@ void drawInput()
gfx.setEpdMode(epd_mode_t::epd_fast); 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.setEpdMode(epd_mode_t::epd_quality);
gfx.fillScreen(TFT_WHITE); gfx.fillScreen(TFT_WHITE);
gfx.waitDisplay(); gfx.waitDisplay();
@@ -124,12 +107,24 @@ void keypad_init()
gfx.setColor(TFT_BLACK); gfx.setColor(TFT_BLACK);
gfx.waitDisplay(); gfx.waitDisplay();
drawButtons(); for (int i = 0; i <= 11; i++)
drawInputField(); {
drawInput(); this->draw_button(i);
} }
void keypad_write(String payload) draw_input_field();
draw_input();
}
Keypad::Keypad()
{
gfx.init();
gfx.setRotation(1);
draw();
}
void Keypad::write(String payload)
{ {
payload.replace("_", " "); payload.replace("_", " ");
@@ -144,7 +139,7 @@ void keypad_write(String payload)
gfx.printf("%s", payload); gfx.printf("%s", payload);
} }
std::optional<String> checkForInput() std::optional<String> Keypad::check_input()
{ {
if (M5.TP.available()) if (M5.TP.available())
{ {
@@ -157,7 +152,7 @@ std::optional<String> checkForInput()
tp_finger_t fingerItem = M5.TP.readFinger(0); 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) if (last_index != i)
{ {
last_index = i; last_index = i;
@@ -190,12 +185,12 @@ std::optional<String> checkForInput()
return std::nullopt; return std::nullopt;
} }
std::optional<String> keypad_loop() std::optional<String> Keypad::get_input()
{ {
auto value = checkForInput(); auto value = check_input();
if (oldinput != input) if (oldinput != input)
{ {
drawInput(); draw_input();
oldinput = input; oldinput = input;
} }

View File

@@ -4,8 +4,28 @@
#include <WString.h> #include <WString.h>
#include <optional> #include <optional>
void keypad_init(); constexpr float FONT_SIZE_NORMAL = 4.0;
void keypad_write(String); constexpr float FONT_SIZE_LARGE = 8.0;
std::optional<String> keypad_loop();
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 #endif

View File

@@ -17,6 +17,7 @@
static WiFiUDP ntpUDP; static WiFiUDP ntpUDP;
static NTPClient timeClient(ntpUDP); static NTPClient timeClient(ntpUDP);
static MQTT *mqtt; static MQTT *mqtt;
static Keypad *keypad;
static bool is_disarmed = false; static bool is_disarmed = false;
@@ -28,13 +29,13 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length)
String state((const char *)payload); String state((const char *)payload);
is_disarmed = state == "disarmed"; is_disarmed = state == "disarmed";
Serial.println(state); if (state == "armed_away")
keypad_write(state); {
keypad->draw();
} }
void setupTime() Serial.println(state);
{ keypad->write(state);
timeClient.begin();
} }
void lock() void lock()
@@ -55,18 +56,18 @@ void initWifi(Settings *settings)
{ {
if (!settings) if (!settings)
{ {
keypad_write("no settings"); keypad->write("no settings");
return; return;
} }
if (isConnecting) if (isConnecting)
{ {
keypad_write("already connecting"); keypad->write("already connecting");
return; return;
} }
isConnecting = true; isConnecting = true;
keypad_write("connecting"); keypad->write("connecting");
WiFi.begin(settings->gettext("wifi:ssid"), settings->gettext("wifi:password")); WiFi.begin(settings->gettext("wifi:ssid"), settings->gettext("wifi:password"));
@@ -88,11 +89,11 @@ void initWifi(Settings *settings)
Serial.print("Local IP: "); Serial.print("Local IP: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
isConnecting = false; isConnecting = false;
keypad_write("connected"); keypad->write("connected");
} }
else else
{ {
keypad_write("failed to connect"); keypad->write("failed to connect");
isConnecting = false; isConnecting = false;
return; return;
} }
@@ -115,14 +116,14 @@ void setup()
{ {
M5.begin(); M5.begin();
keypad_init(); keypad = new Keypad();
keypad_write("loading"); keypad->write("loading");
auto settings = new Settings(); auto settings = new Settings();
if (!settings) if (!settings)
{ {
keypad_write("unable to load settings"); keypad->write("unable to load settings");
return; return;
} }
@@ -130,7 +131,7 @@ void setup()
initWifi(settings); initWifi(settings);
mqtt->connect(); mqtt->connect();
setupTime(); timeClient.begin();
initTOTP(settings); initTOTP(settings);
} }
@@ -148,24 +149,12 @@ void submit(String code)
lock(); 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() void loop()
{ {
timeClient.update(); timeClient.update();
mqtt->loop(); mqtt->loop();
// checkForButton(); auto value = keypad->get_input();
auto value = keypad_loop();
if (value.has_value()) if (value.has_value())
submit(value.value()); submit(value.value());
} }