feat: integrate R8 observer and companion updates

This commit is contained in:
mikecarper committed 2026-08-28 18:39:26 -07:00
1 parent 89c12d416e
commit da61c48e7b
54 files changed
+5471 -158

No files matched your search

+172 -3
View File
@@ -1,5 +1,9 @@
#include "ST7789LCDDisplay.h"
#ifdef ST7789_PORTRAIT_PROFILE
#include "DisplayViewport.h"
#endif
#ifndef PIN_TFT_MISO
#define PIN_TFT_MISO -1
#endif
@@ -19,6 +23,29 @@
#define DISPLAY_WIDTH 240
#define DISPLAY_HEIGHT 320
#ifdef ST7789_PORTRAIT_PROFILE
#ifndef ST7789_PORTRAIT_TEXT_SCALE
#define ST7789_PORTRAIT_TEXT_SCALE 2
#endif
static DisplayViewport::Geometry portraitViewport(int16_t physical_width, int16_t physical_height) {
return {128, 64, physical_width, physical_height};
}
#endif
// The compiled orientation, optionally turned 180 degrees by `display.flip`.
// Adding 2 keeps portrait portrait and landscape landscape, so the viewport
// geometry below never has to change with it.
uint8_t ST7789LCDDisplay::effectiveRotation() const {
return (uint8_t)((DISPLAY_ROTATION + (_flipped ? 2 : 0)) & 3);
}
void ST7789LCDDisplay::setFlipped(bool flipped) {
if (_flipped == flipped) return;
_flipped = flipped;
if (_panel_ready) display.setRotation(effectiveRotation());
}
bool ST7789LCDDisplay::i2c_probe(TwoWire& wire, uint8_t addr) {
return true;
}
@@ -36,6 +63,22 @@ ColorVal UIColor::corp_blue = 0x001A;
bool ST7789LCDDisplay::begin() {
if (!_isOn) {
#ifdef HELTEC_V4_R8_TFT
// turnOff() leaves this panel configured and powered - its reset line is
// shared with the touch controller, so it is never parked low - which makes
// waking just a backlight switch. Re-running the init below would re-enter
// SPI setup and pulse GPIO 21, resetting the touch controller on every wake
// and stalling the UI loop for ~500 ms of reset delays.
if (_panel_ready) {
if (_peripher_power) _peripher_power->claim();
if (PIN_TFT_LEDA_CTL != -1) {
digitalWrite(PIN_TFT_LEDA_CTL, PIN_TFT_LEDA_CTL_ACTIVE);
}
_isOn = true;
return true;
}
#endif
if (_peripher_power) {
_peripher_power->claim();
#ifdef HELTEC_V4_R8_TFT
@@ -58,13 +101,19 @@ bool ST7789LCDDisplay::begin() {
#endif
display.init(DISPLAY_WIDTH, DISPLAY_HEIGHT);
display.setRotation(DISPLAY_ROTATION);
display.setRotation(effectiveRotation());
display.setSPISpeed(40e6);
display.fillScreen(ST77XX_BLACK);
display.setTextColor(ST77XX_WHITE);
display.setTextSize(2 * DISPLAY_SCALE_X);
#ifdef ST7789_PORTRAIT_PROFILE
_logical_text_size = 1;
display.setTextSize(ST7789_PORTRAIT_TEXT_SCALE);
display.setTextWrap(false);
#else
display.setTextSize(2 * DISPLAY_SCALE_X);
#endif
display.cp437(true); // Use full 256 char 'Code Page 437' font
#ifdef HELTEC_V4_R8_TFT
@@ -73,6 +122,7 @@ bool ST7789LCDDisplay::begin() {
}
#endif
_panel_ready = true;
_isOn = true;
}
@@ -92,13 +142,18 @@ void ST7789LCDDisplay::turnOff() {
digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
#endif
}
#ifndef HELTEC_V4_R8_TFT
if (PIN_TFT_RST != -1) {
digitalWrite(PIN_TFT_RST, LOW);
}
#ifndef HELTEC_V4_R8_TFT
if (PIN_TFT_LEDA_CTL != -1) {
digitalWrite(PIN_TFT_LEDA_CTL, LOW);
}
#else
// On the V4 R8 Expansion Kit this reset line is shared with the touch
// panel's TP_RST, so parking it low would hold the touch controller in
// reset for as long as the display is off. Killing the backlight is what
// "off" means for this LCD anyway.
#endif
_isOn = false;
@@ -113,12 +168,22 @@ void ST7789LCDDisplay::clear() {
void ST7789LCDDisplay::startFrame(ColorVal bkg) {
display.fillScreen(bkg);
display.setTextColor(_color = UIColor::primary_txt);
#ifdef ST7789_PORTRAIT_PROFILE
_logical_text_size = 1;
display.setTextSize(ST7789_PORTRAIT_TEXT_SCALE);
#else
display.setTextSize(1 * DISPLAY_SCALE_X); // This one affects size of Please wait... message
#endif
display.cp437(true); // Use full 256 char 'Code Page 437' font
}
void ST7789LCDDisplay::setTextSize(int sz) {
#ifdef ST7789_PORTRAIT_PROFILE
_logical_text_size = sz > 0 ? static_cast<uint8_t>(sz) : 1;
display.setTextSize(_logical_text_size * ST7789_PORTRAIT_TEXT_SCALE);
#else
display.setTextSize(sz * DISPLAY_SCALE_X);
#endif
}
void ST7789LCDDisplay::setColor(ColorVal c) {
@@ -126,24 +191,70 @@ void ST7789LCDDisplay::setColor(ColorVal c) {
}
void ST7789LCDDisplay::setCursor(int x, int y) {
#ifdef ST7789_PORTRAIT_PROFILE
DisplayViewport::Geometry viewport = portraitViewport(display.width(), display.height());
display.setCursor(viewport.mapX(x), viewport.mapY(y));
#else
display.setCursor(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y);
#endif
}
void ST7789LCDDisplay::print(const char* str) {
#ifdef ST7789_PORTRAIT_PROFILE
int16_t cursor_x = display.getCursorX();
if (cursor_x < 0) {
cursor_x = 0;
display.setCursor(cursor_x, display.getCursorY());
}
if (cursor_x >= display.width()) return;
printFitted(str, static_cast<uint16_t>(display.width() - cursor_x));
#else
display.print(str);
#endif
}
void ST7789LCDDisplay::fillRect(int x, int y, int w, int h) {
#ifdef ST7789_PORTRAIT_PROFILE
DisplayViewport::Geometry viewport = portraitViewport(display.width(), display.height());
display.fillRect(viewport.mapX(x), viewport.mapY(y), viewport.spanX(x, w), viewport.spanY(y, h), _color);
#else
display.fillRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
#endif
}
void ST7789LCDDisplay::drawRect(int x, int y, int w, int h) {
#ifdef ST7789_PORTRAIT_PROFILE
DisplayViewport::Geometry viewport = portraitViewport(display.width(), display.height());
display.drawRect(viewport.mapX(x), viewport.mapY(y), viewport.spanX(x, w), viewport.spanY(y, h), _color);
#else
display.drawRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
#endif
}
void ST7789LCDDisplay::drawXbm(int x, int y, const uint8_t* bits, int w, int h) {
uint8_t byteWidth = (w + 7) / 8;
#ifdef ST7789_PORTRAIT_PROFILE
DisplayViewport::Geometry viewport = portraitViewport(display.width(), display.height());
int16_t physical_y = viewport.mapY(y);
for (int j = 0; j < h; j++) {
// Scale both bitmap axes from the logical X ratio so logo pixels stay square.
int16_t y0 = physical_y + viewport.mapX(j);
int16_t y1 = physical_y + viewport.mapX(j + 1);
for (int i = 0; i < w; i++) {
uint8_t byte = bits[j * byteWidth + i / 8];
bool pixelOn = byte & (0x80 >> (i & 7));
if (pixelOn) {
int16_t x0 = viewport.mapX(x + i);
int16_t x1 = viewport.mapX(x + i + 1);
display.fillRect(x0, y0, x1 - x0, y1 - y0, _color);
}
}
}
#else
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
uint8_t byte = bits[j * byteWidth + i / 8];
@@ -158,16 +269,74 @@ void ST7789LCDDisplay::drawXbm(int x, int y, const uint8_t* bits, int w, int h)
}
}
}
#endif
}
uint16_t ST7789LCDDisplay::getTextWidth(const char* str) {
#ifdef ST7789_PORTRAIT_PROFILE
uint8_t physical_scale = selectTextScale(str, display.width());
uint16_t physical_width = measureTextWidth(str, physical_scale);
if (physical_width > display.width()) physical_width = display.width();
DisplayViewport::Geometry viewport = portraitViewport(display.width(), display.height());
return viewport.logicalWidthForPhysical(physical_width);
#else
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
return w / DISPLAY_SCALE_X;
#endif
}
#ifdef ST7789_PORTRAIT_PROFILE
uint16_t ST7789LCDDisplay::measureTextWidth(const char* str, uint8_t physical_scale) {
int16_t x1, y1;
uint16_t w, h;
display.setTextSize(physical_scale);
display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
return w;
}
uint8_t ST7789LCDDisplay::selectTextScale(const char* str, uint16_t available_width) {
uint16_t width_at_scale_one = measureTextWidth(str, 1);
uint8_t preferred_scale = _logical_text_size * ST7789_PORTRAIT_TEXT_SCALE;
return DisplayViewport::selectTextScale(width_at_scale_one, preferred_scale, _logical_text_size,
available_width);
}
void ST7789LCDDisplay::printFitted(const char* str, uint16_t available_width) {
if (!str || available_width == 0) return;
uint8_t physical_scale = selectTextScale(str, available_width);
if (measureTextWidth(str, physical_scale) <= available_width) {
display.print(str);
return;
}
static const char* ellipsis = "...";
uint16_t ellipsis_width = measureTextWidth(ellipsis, physical_scale);
if (ellipsis_width > available_width) return;
char fitted[256];
size_t len = strlen(str);
if (len > sizeof(fitted) - 4) len = sizeof(fitted) - 4;
memcpy(fitted, str, len);
fitted[len] = 0;
while (len > 0 && measureTextWidth(fitted, physical_scale) + ellipsis_width > available_width) {
--len;
while (len > 0 && (static_cast<uint8_t>(fitted[len]) & 0xC0) == 0x80)
--len;
fitted[len] = 0;
}
memcpy(fitted + len, ellipsis, 4);
display.setTextSize(physical_scale);
display.print(fitted);
}
#endif
void ST7789LCDDisplay::endFrame() {
// display.display();
}