From 7534c5143fe89c21331b0e9c92b953ba62c7c67a Mon Sep 17 00:00:00 2001 From: Florent de Lamotte Date: Thu, 10 Apr 2025 16:24:17 +0200 Subject: [PATCH] display and btn handling starts working ... --- examples/companion_radio/UITask.cpp | 10 +++- examples/companion_radio/UITask.h | 1 + examples/companion_radio/main.cpp | 9 ++- src/helpers/ui/ST7789Display.cpp | 88 +++++++++++++++++++++++++++++ src/helpers/ui/ST7789Display.h | 33 +++++++++++ variants/t114/platformio.ini | 6 ++ variants/t114/variant.h | 19 ++++++- 7 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 src/helpers/ui/ST7789Display.cpp create mode 100644 src/helpers/ui/ST7789Display.h diff --git a/examples/companion_radio/UITask.cpp b/examples/companion_radio/UITask.cpp index 2d2e95a6..401284fb 100644 --- a/examples/companion_radio/UITask.cpp +++ b/examples/companion_radio/UITask.cpp @@ -43,6 +43,10 @@ void UITask::begin(DisplayDriver* display, const char* node_name, const char* bu *dash = 0; } + #ifdef PIN_USER_BTN + pinMode(PIN_USER_BTN, INPUT); + #endif + // v1.2.3 (1 Jan 2025) sprintf(_version_info, "%s (%s)", version, build_date); } @@ -57,6 +61,7 @@ void UITask::msgRead(int msgcount) { void UITask::clearMsgPreview() { _origin[0] = 0; _msg[0] = 0; + _need_refresh = true; } void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) { @@ -72,6 +77,7 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i if (_display != NULL) { if (!_display->isOn()) _display->turnOn(); _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer + _need_refresh = true; } } @@ -114,6 +120,7 @@ void UITask::renderCurrScreen() { _display->print(tmp); } } + _need_refresh = false; } void UITask::userLedHandler() { @@ -157,6 +164,7 @@ void UITask::buttonHandler() { clearMsgPreview(); } else { _display->turnOn(); + _need_refresh = true; } _auto_off = cur_time + AUTO_OFF_MILLIS; // extend auto-off timer } @@ -182,7 +190,7 @@ void UITask::loop() { userLedHandler(); if (_display != NULL && _display->isOn()) { - if (millis() >= _next_refresh) { + if (millis() >= _next_refresh && _need_refresh) { _display->startFrame(); renderCurrScreen(); _display->endFrame(); diff --git a/examples/companion_radio/UITask.h b/examples/companion_radio/UITask.h index 4cba1fca..9050bc42 100644 --- a/examples/companion_radio/UITask.h +++ b/examples/companion_radio/UITask.h @@ -15,6 +15,7 @@ class UITask { char _origin[62]; char _msg[80]; int _msgcount; + bool _need_refresh = true; void renderCurrScreen(); void buttonHandler(); diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 30e492ce..c68f827e 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -59,9 +59,12 @@ #ifdef DISPLAY_CLASS #include "UITask.h" - #include - - static DISPLAY_CLASS display; + #ifdef ST7789 + #include + #else + #include + #endif + static DISPLAY_CLASS display; #define HAS_UI #endif diff --git a/src/helpers/ui/ST7789Display.cpp b/src/helpers/ui/ST7789Display.cpp new file mode 100644 index 00000000..6f877230 --- /dev/null +++ b/src/helpers/ui/ST7789Display.cpp @@ -0,0 +1,88 @@ +#include "ST7789Display.h" + +bool ST7789Display::i2c_probe(TwoWire& wire, uint8_t addr) { + return true; +/* + wire.beginTransmission(addr); + uint8_t error = wire.endTransmission(); + return (error == 0); +*/ +} + +bool ST7789Display::begin() { + if(!_isOn) { + pinMode(PIN_TFT_VDD_CTL, OUTPUT); + pinMode(PIN_TFT_LEDA_CTL, OUTPUT); + digitalWrite(PIN_TFT_VDD_CTL, LOW); + digitalWrite(PIN_TFT_LEDA_CTL, LOW); + digitalWrite(PIN_TFT_RST, HIGH); + + display.init(135, 240); + display.setRotation(2); + display.setSPISpeed(40000000); + display.fillScreen(ST77XX_BLACK); + display.setTextColor(ST77XX_WHITE); + display.setTextSize(2); + display.cp437(true); // Use full 256 char 'Code Page 437' font + + _isOn = true; + } + return true; +} + +void ST7789Display::turnOn() { + ST7789Display::begin(); +} + +void ST7789Display::turnOff() { + digitalWrite(PIN_TFT_VDD_CTL, HIGH); + digitalWrite(PIN_TFT_LEDA_CTL, HIGH); + digitalWrite(PIN_TFT_RST, LOW); + // digitalWrite(PIN_TFT_VDD_CTL, LOW); + // digitalWrite(PIN_TFT_LEDA_CTL, LOW); + _isOn = false; +} + +void ST7789Display::clear() { + display.fillScreen(ST77XX_BLACK); +} + +void ST7789Display::startFrame(Color bkg) { + display.fillScreen(0x00); + display.setTextColor(ST77XX_WHITE); + display.setTextSize(2); + display.cp437(true); // Use full 256 char 'Code Page 437' font +} + +void ST7789Display::setTextSize(int sz) { + display.setTextSize(sz); +} + +void ST7789Display::setColor(Color c) { + _color = (c == LIGHT) ? ST77XX_WHITE : ST77XX_BLACK; + display.setTextColor(_color); +} + +void ST7789Display::setCursor(int x, int y) { + display.setCursor(x, y); +} + +void ST7789Display::print(const char* str) { + display.print(str); +} + +void ST7789Display::fillRect(int x, int y, int w, int h) { + display.fillRect(x, y, w, h, _color); +} + +void ST7789Display::drawRect(int x, int y, int w, int h) { + display.drawRect(x, y, w, h, _color); +} + +void ST7789Display::drawXbm(int x, int y, const uint8_t* bits, int w, int h) { + display.drawBitmap(x, y, bits, w, h, ST77XX_BLUE); +} + +void ST7789Display::endFrame() { + // display.display(); +} diff --git a/src/helpers/ui/ST7789Display.h b/src/helpers/ui/ST7789Display.h new file mode 100644 index 00000000..0ddc7ae2 --- /dev/null +++ b/src/helpers/ui/ST7789Display.h @@ -0,0 +1,33 @@ +#pragma once + +#include "DisplayDriver.h" +#include +#include +#include +#include + +class ST7789Display : public DisplayDriver { + Adafruit_ST7789 display; + bool _isOn; + uint8_t _color; + + bool i2c_probe(TwoWire& wire, uint8_t addr); +public: + ST7789Display() : DisplayDriver(135, 240), display(&SPI1, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST) { _isOn = false; } +// ST7789Display() : DisplayDriver(135, 240), display(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_SDA, PIN_TFT_SCL, PIN_TFT_RST) { _isOn = false; } + bool begin(); + + bool isOn() override { return _isOn; } + void turnOn() override; + void turnOff() override; + void clear() override; + void startFrame(Color bkg = DARK) override; + void setTextSize(int sz) override; + void setColor(Color c) override; + void setCursor(int x, int y) override; + void print(const char* str) override; + void fillRect(int x, int y, int w, int h) override; + void drawRect(int x, int y, int w, int h) override; + void drawXbm(int x, int y, const uint8_t* bits, int w, int h) override; + void endFrame() override; +}; diff --git a/variants/t114/platformio.ini b/variants/t114/platformio.ini index 61a722d0..d1be32e5 100644 --- a/variants/t114/platformio.ini +++ b/variants/t114/platformio.ini @@ -60,6 +60,9 @@ build_flags = extends = Heltec_t114 build_flags = ${Heltec_t114.build_flags} + -I src/helpers/ui + -D ST7789 + -D DISPLAY_CLASS=ST7789Display -D MAX_CONTACTS=100 -D MAX_GROUP_CHANNELS=8 -D BLE_PIN_CODE=123456 @@ -71,9 +74,12 @@ build_flags = build_src_filter = ${Heltec_t114.build_src_filter} + +<../examples/companion_radio/main.cpp> + +<../examples/companion_radio/UITask.cpp> + + lib_deps = ${Heltec_t114.lib_deps} densaugeo/base64 @ ~1.4.0 + adafruit/Adafruit ST7735 and ST7789 Library @ ^1.11.0 [env:Heltec_t114_companion_radio_usb] extends = Heltec_t114 diff --git a/variants/t114/variant.h b/variants/t114/variant.h index ee04861e..56fc536d 100644 --- a/variants/t114/variant.h +++ b/variants/t114/variant.h @@ -80,11 +80,13 @@ //////////////////////////////////////////////////////////////////////////////// // Builtin buttons -#define PIN_BUTTON1 (5) +#define PIN_BUTTON1 (42) #define BUTTON_PIN PIN_BUTTON1 -#define PIN_BUTTON2 (11) -#define BUTTON_PIN2 PIN_BUTTON2 +// #define PIN_BUTTON2 (11) +// #define BUTTON_PIN2 PIN_BUTTON2 + +#define PIN_USER_BTN BUTTON_PIN #define EXTERNAL_FLASH_DEVICES MX25R1635F #define EXTERNAL_FLASH_USE_QSPI @@ -108,3 +110,14 @@ // Buzzer #define PIN_BUZZER (46) + + +//////////////////////////////////////////////////////////////////////////////// +// TFT +#define PIN_TFT_SCL (40) +#define PIN_TFT_SDA (41) +#define PIN_TFT_RST (2) +#define PIN_TFT_VDD_CTL (3) +#define PIN_TFT_LEDA_CTL (15) +#define PIN_TFT_CS (11) +#define PIN_TFT_DC (12)