From 66ee0040badc898d0ee15afc83461c29bb90f93b Mon Sep 17 00:00:00 2001 From: Avii Date: Tue, 15 Jul 2025 23:04:34 +0200 Subject: [PATCH] Portrait Rotation --- src/button.hpp | 1 + src/display.hpp | 4 ++-- src/input.hpp | 8 ++++---- src/keypad.hpp | 5 +++-- src/main.cpp | 26 ++++++++++++++++++-------- src/status.hpp | 4 ++-- src/touch.hpp | 15 +++++++++++++-- 7 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/button.hpp b/src/button.hpp index fe1a023..75ee708 100644 --- a/src/button.hpp +++ b/src/button.hpp @@ -120,6 +120,7 @@ public: { if (enabled && touch_x > x && touch_x < x + width && touch_y > y && touch_y < y + height) { + Serial.printf("pressed as %d, %d: %s\n", touch_x, touch_y, this->label); on_pressed(keypad, value); } } diff --git a/src/display.hpp b/src/display.hpp index d401afd..b268893 100644 --- a/src/display.hpp +++ b/src/display.hpp @@ -10,10 +10,10 @@ class Display : public LGFX { public: - void init() + void init(uint_fast8_t rotation) { LGFX::init(); - setRotation(1); + setRotation(rotation); setEpdMode(epd_mode_t::epd_quality); fillScreen(TFT_WHITE); diff --git a/src/input.hpp b/src/input.hpp index 52b60a3..e2580ae 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -7,9 +7,9 @@ class Input { public: - Input(String value) : value{value}, old_value{value} {} - Input(String value, float x, float y) : value{value}, x{x}, y{y} {} - Input(String value, float x, float y, int width, int height) : value{value}, x{x}, y{y}, width{width}, height{height} {} + Input(LGFX *canvas, String value) : canvas{canvas}, value{value}, old_value{value} {} + Input(LGFX *canvas, String value, float x, float y) : canvas{canvas}, value{value}, x{x}, y{y} {} + Input(LGFX *canvas, String value, float x, float y, int width, int height) : canvas{canvas}, value{value}, x{x}, y{y}, width{width}, height{height} {} ~Input() = default; @@ -72,7 +72,7 @@ public: y = value; } - void draw(LGFX *canvas) + void draw() { if (!dirty) { diff --git a/src/keypad.hpp b/src/keypad.hpp index eb769e2..1876a18 100644 --- a/src/keypad.hpp +++ b/src/keypad.hpp @@ -13,7 +13,7 @@ class Keypad : public EventEmitter public: Keypad(LGFX *canvas) : EventEmitter(), canvas{canvas} { - int BUTTON_SIZE = canvas->height() / BUTTON_ROWS; + int BUTTON_SIZE = canvas->width() / BUTTON_COLS; width = BUTTON_SIZE * BUTTON_COLS; for (int i = 0; i < BUTTON_COUNT; i++) @@ -38,7 +38,7 @@ public: enabled = false; } - buttons.push_back(new KeypadButton(lbl, i, enabled, x, y, BUTTON_SIZE, BUTTON_SIZE, Keypad::on_button_pressed)); + buttons.push_back(new KeypadButton(lbl, i, enabled, x, y + 120, BUTTON_SIZE, BUTTON_SIZE, Keypad::on_button_pressed)); } } @@ -51,6 +51,7 @@ public: void update(tp_finger_t touch) { + Serial.printf("touching: %d, %d\n", touch.x, touch.y); for (int i = 0; i < BUTTON_COUNT; i++) { buttons[i]->update(this, touch.x, touch.y); diff --git a/src/main.cpp b/src/main.cpp index c78b454..8acd717 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,7 +53,7 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length) state[0] = toupper(state[0]); Serial.println(state); status->set(state); - status->draw(&display); + status->draw(); keypad_state(!is_disarmed); } @@ -151,7 +151,7 @@ void p(int v) if (input->length() >= 6) { // verify - input->draw(&display); // force one more redraw for the last character + input->draw(); // force one more redraw for the last character submit(input->get_value()); input->clear(); } @@ -160,12 +160,13 @@ void p(int v) void setup() { M5.begin(); - display.init(); - touch = new Touch(M5.TP); - status = new Status("Loading"); + Serial.println("Device Startup"); + display.init(0); + touch = new Touch(M5.TP, 0); + status = new Status(&display, "Loading"); keypad = new Keypad(&display); keypad->addListener("pressed", p); - input = new Input("", keypad->get_width(), 0, display.width() - keypad->get_width(), 115); + input = new Input(&display, "", 0, 5, display.width(), 115); auto settings = new Settings(); @@ -187,13 +188,22 @@ void setup() void loop() { + M5.update(); mqtt->loop(); auto tap = touch->tap(); if (tap.has_value()) keypad->update(tap.value()); + input->draw(); keypad->draw(); - input->draw(&display); - status->draw(&display); + status->draw(); + + if (M5.BtnP.isPressed()) + { + status->set("Restarting"); + status->draw(); + delay(10); + abort(); + } } diff --git a/src/status.hpp b/src/status.hpp index 9e5af26..28983dc 100644 --- a/src/status.hpp +++ b/src/status.hpp @@ -7,7 +7,7 @@ class Status { public: - Status(String value) : value{value} + Status(LGFX *canvas, String value) : canvas{canvas}, value{value} { old_length = value.length(); } @@ -21,7 +21,7 @@ public: dirty = true; } - void draw(LGFX *canvas) + void draw() { if (!dirty) { diff --git a/src/touch.hpp b/src/touch.hpp index 3fc52e4..3eeb669 100644 --- a/src/touch.hpp +++ b/src/touch.hpp @@ -7,7 +7,10 @@ class Touch { public: - Touch(GT911 TP) : TP{TP} {} + Touch(GT911 TP, uint_fast8_t rotation) : TP{TP} + { + TP.SetRotation(rotation); + } std::optional tap() { @@ -30,7 +33,15 @@ public: } touching = true; - return TP.readFinger(0); + + auto f = TP.readFinger(0); + auto x = 540 - f.y; + auto y = f.x; + + f.x = x; + f.y = y; + + return f; } private: