Files
JackBotEsp32/DisplayManager.cpp

133 lines
3.6 KiB
C++

#include "DisplayManager.h"
#include "img/image_cards_hearts_bits.h"
#include "img/image_paint_3_bits.h"
#include "img/tenor.h"
#include <string.h>
DisplayManager::DisplayManager(int8_t cs, int8_t dc, int8_t rst)
: _tft(cs, dc, rst) {}
static void drawAnimationFrame(Adafruit_ST7789& tft, int16_t x, uint8_t frame) {
uint16_t pixels[240];
const uint8_t* bitmap = tenor_frames[frame];
tft.startWrite();
tft.setAddrWindow(x, 0, 240, 240);
for (int16_t y = 0; y < 240; y++) {
for (int16_t byteIndex = 0; byteIndex < 30; byteIndex++) {
uint8_t bits = pgm_read_byte(&bitmap[y * 30 + byteIndex]);
for (uint8_t bit = 0; bit < 8; bit++) {
pixels[byteIndex * 8 + bit] = (bits & (0x80 >> bit)) ? ST77XX_WHITE : ST77XX_BLACK;
}
}
tft.writePixels(pixels, 240);
}
tft.endWrite();
}
void DisplayManager::begin() {
_tft.init(240, 320);
_tft.setRotation(1);
clear();
drawFaceScreen();
}
void DisplayManager::clear() {
_tft.fillScreen(ST77XX_BLACK);
}
void DisplayManager::setState(DisplayState state) {
_currentState = state;
// Force a redraw when state changes
if (_currentState == STATE_FACE) {
drawFaceScreen();
} else {
drawMenuScreen();
}
}
void DisplayManager::toggleState() {
if (_currentState == STATE_FACE) {
setState(STATE_MENU);
} else {
setState(STATE_FACE);
}
}
void DisplayManager::update() {
if (_currentState != STATE_FACE) return;
unsigned long now = millis();
if (_showingText) {
if (now - _textShownAt < 2000) return;
drawFaceScreen();
}
if (now - _lastScreenUpdate < 71) return;
_lastScreenUpdate = now;
int16_t x = (_tft.width() - 240) / 2;
drawAnimationFrame(_tft, x, _animationFrame);
_tft.drawBitmap(x + 105, 14, image_cards_hearts_bits, 30, 32, 0xE8EC);
_animationFrame = (_animationFrame + 1) % 8;
}
// --- DESIGNED VIA LOPAKA.APP ---
void DisplayManager::drawFaceScreen() {
_showingText = false;
_animationFrame = 0;
clear();
int16_t x = (_tft.width() - 240) / 2;
_tft.drawBitmap(x, 0, image_paint_3_bits, 240, 240, ST77XX_WHITE);
_tft.drawBitmap(x + 105, 14, image_cards_hearts_bits, 30, 32, 0xE8EC);
}
void DisplayManager::drawMenuScreen() {
_showingText = false;
clear();
// Example Menu/Stats UI layout adapted from Lopaka export
_tft.drawRect(10, 10, 300, 220, ST77XX_BLUE); // Outer box
_tft.setTextColor(ST77XX_WHITE);
_tft.setTextSize(2);
_tft.setCursor(30, 30);
_tft.print("-- JACKBOT CONFIG --");
_tft.setTextSize(1);
_tft.setCursor(30, 80);
_tft.print("Status: Connected (UDP)");
_tft.setCursor(30, 110);
_tft.print("Servo Profile: Standard");
_tft.setCursor(30, 140);
_tft.print("Press button to exit menu");
}
void DisplayManager::drawCenteredText(const char* text, uint16_t color) {
if (_currentState != STATE_FACE || text == nullptr) return;
if (strcmp(text, _lastText) == 0) return;
size_t textLength = strlen(text);
if (textLength >= sizeof(_lastText)) textLength = sizeof(_lastText) - 1;
memcpy(_lastText, text, textLength);
_lastText[textLength] = '\0';
_showingText = true;
_textShownAt = millis();
clear();
_tft.setTextSize(2);
_tft.setTextColor(color);
int16_t x1, y1;
uint16_t w, h;
_tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
int16_t cx = (_tft.width() - w) / 2;
int16_t cy = (_tft.height() - h) / 2;
_tft.setCursor(cx, cy);
_tft.print(text);
}