Files
WaveShare_ESP32_BoomCast_FW/BoomCast_Waveshare_Arduino/boomcast_display.cpp
T

365 lines
13 KiB
C++

#include "boomcast_display.h"
#include "boomcast_config.h"
#include "user_config.h"
#include "esp_heap_caps.h"
#include "esp_timer.h"
#include "driver/gpio.h"
BoomDisplay boomDisplay;
BoomDisplay* BoomDisplay::instance_ = nullptr;
static lv_point_t fireworkRays[8][2] = {
{{23, 21}, {23, 2}}, {{23, 25}, {23, 44}},
{{21, 23}, {2, 23}}, {{25, 23}, {44, 23}},
{{21, 21}, {7, 7}}, {{25, 25}, {39, 39}},
{{25, 21}, {39, 7}}, {{21, 25}, {7, 39}}
};
static void stylePlainScreen(lv_obj_t* screen) {
lv_obj_set_style_bg_color(screen, lv_color_white(), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
lv_obj_clear_flag(screen, LV_OBJ_FLAG_SCROLLABLE);
}
bool BoomDisplay::begin() {
instance_ = this;
lvglMutex_ = xSemaphoreCreateMutex();
if (!lvglMutex_) return false;
// Power gates on this Waveshare board are active-low for EPD and audio.
gpio_set_direction(EPD_PWR_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(EPD_PWR_PIN, 0);
custom_lcd_spi_t cfg = {};
cfg.cs = EPD_CS_PIN;
cfg.dc = EPD_DC_PIN;
cfg.rst = EPD_RST_PIN;
cfg.busy = EPD_BUSY_PIN;
cfg.mosi = EPD_MOSI_PIN;
cfg.scl = EPD_SCK_PIN;
cfg.spi_host = EPD_SPI_NUM;
cfg.buffer_len = 5000; // 200*200/8
epd_ = new epaper_driver_display(EPD_WIDTH, EPD_HEIGHT, cfg);
if (!epd_) return false;
epd_->EPD_Init();
epd_->EPD_Clear();
epd_->EPD_DisplayPartBaseImage();
epd_->EPD_Init_Partial();
lv_init();
static lv_disp_draw_buf_t drawBuf;
lv_color_t* buffer = (lv_color_t*)heap_caps_malloc(
EPD_WIDTH * EPD_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_SPIRAM);
if (!buffer) return false;
lv_disp_draw_buf_init(&drawBuf, buffer, nullptr, EPD_WIDTH * EPD_HEIGHT);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = EPD_WIDTH;
dispDrv.ver_res = EPD_HEIGHT;
dispDrv.flush_cb = flushCb;
dispDrv.draw_buf = &drawBuf;
dispDrv.full_refresh = 1;
display_ = lv_disp_drv_register(&dispDrv);
esp_timer_create_args_t timerArgs = {};
timerArgs.callback = &tickCb;
timerArgs.name = "boom_lv_tick";
esp_timer_handle_t tickTimer = nullptr;
ESP_ERROR_CHECK(esp_timer_create(&timerArgs, &tickTimer));
ESP_ERROR_CHECK(esp_timer_start_periodic(tickTimer, 5 * 1000));
if (lock()) {
createScreens();
lv_scr_load(publicScreen_);
requestFullRefresh();
unlock();
}
xTaskCreatePinnedToCore(lvglTaskThunk, "boom_lvgl", 8192, this, 3, nullptr, 1);
return true;
}
bool BoomDisplay::lock(uint32_t timeoutMs) {
TickType_t ticks = timeoutMs == portMAX_DELAY ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMs);
return xSemaphoreTake(lvglMutex_, ticks) == pdTRUE;
}
void BoomDisplay::unlock() {
xSemaphoreGive(lvglMutex_);
}
void BoomDisplay::tickCb(void*) {
lv_tick_inc(5);
}
void BoomDisplay::lvglTaskThunk(void* arg) {
static_cast<BoomDisplay*>(arg)->lvglTask();
}
void BoomDisplay::lvglTask() {
while (true) {
uint32_t delayMs = 50;
if (lock()) {
delayMs = lv_timer_handler();
unlock();
}
delayMs = constrain(delayMs, 20U, 250U);
vTaskDelay(pdMS_TO_TICKS(delayMs));
}
}
void BoomDisplay::flushCb(lv_disp_drv_t* drv, const lv_area_t* area, lv_color_t* colorMap) {
if (instance_) instance_->flushPixels(area, colorMap);
lv_disp_flush_ready(drv);
}
void BoomDisplay::flushPixels(const lv_area_t* area, lv_color_t* colorMap) {
if (!epd_) return;
epd_->EPD_Clear();
uint16_t* buffer = reinterpret_cast<uint16_t*>(colorMap);
for (int y = area->y1; y <= area->y2; ++y) {
for (int x = area->x1; x <= area->x2; ++x) {
int px = x;
int py = y;
if (DISPLAY_ROTATION_DEG == 180) {
px = EPD_WIDTH - 1 - x;
py = EPD_HEIGHT - 1 - y;
}
uint8_t color = (*buffer < 0x7fff) ? DRIVER_COLOR_BLACK : DRIVER_COLOR_WHITE;
epd_->EPD_DrawColorPixel(px, py, color);
++buffer;
}
}
bool full = fullRefreshNext_ || partialCount_ >= FULL_REFRESH_AFTER_PARTIALS;
if (full) {
epd_->EPD_Init();
epd_->EPD_DisplayPartBaseImage();
epd_->EPD_Init_Partial();
fullRefreshNext_ = false;
partialCount_ = 0;
} else {
epd_->EPD_DisplayPart();
++partialCount_;
}
}
lv_obj_t* BoomDisplay::makeStatusChip(lv_obj_t* parent, int x, const char* initial, bool wifiGlyph) {
lv_obj_t* chip = lv_obj_create(parent);
lv_obj_set_size(chip, 82, 24);
lv_obj_set_pos(chip, x, 6);
lv_obj_set_style_radius(chip, 12, 0);
lv_obj_set_style_bg_color(chip, lv_color_black(), 0);
lv_obj_set_style_bg_opa(chip, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(chip, 0, 0);
lv_obj_set_style_pad_all(chip, 0, 0);
lv_obj_clear_flag(chip, LV_OBJ_FLAG_SCROLLABLE);
// Tiny built-in vector glyphs instead of external SVG/font dependencies.
// They remain crisp on a 1-bit panel and rotate with the whole framebuffer.
if (wifiGlyph) {
const int heights[3] = {4, 8, 12};
for (int i = 0; i < 3; ++i) {
lv_obj_t* bar = lv_obj_create(chip);
lv_obj_set_size(bar, 3, heights[i]);
lv_obj_set_pos(bar, 7 + i * 5, 16 - heights[i]);
lv_obj_set_style_bg_color(bar, lv_color_white(), 0);
lv_obj_set_style_bg_opa(bar, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(bar, 0, 0);
lv_obj_set_style_radius(bar, 1, 0);
lv_obj_set_style_pad_all(bar, 0, 0);
}
} else {
lv_obj_t* card = lv_obj_create(chip);
lv_obj_set_size(card, 13, 15);
lv_obj_set_pos(card, 7, 4);
lv_obj_set_style_bg_opa(card, LV_OPA_TRANSP, 0);
lv_obj_set_style_border_color(card, lv_color_white(), 0);
lv_obj_set_style_border_width(card, 1, 0);
lv_obj_set_style_radius(card, 1, 0);
lv_obj_set_style_pad_all(card, 0, 0);
lv_obj_t* notch = lv_obj_create(card);
lv_obj_set_size(notch, 4, 3);
lv_obj_set_pos(notch, 6, 1);
lv_obj_set_style_bg_color(notch, lv_color_white(), 0);
lv_obj_set_style_border_width(notch, 0, 0);
lv_obj_set_style_pad_all(notch, 0, 0);
}
lv_obj_t* label = lv_label_create(chip);
lv_label_set_text(label, initial);
lv_obj_set_style_text_color(label, lv_color_white(), 0);
lv_obj_set_width(label, 52);
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_pos(label, 25, 4);
return label;
}
void BoomDisplay::createFireworkMark(lv_obj_t* parent) {
lv_obj_t* mark = lv_obj_create(parent);
lv_obj_set_size(mark, 46, 46);
lv_obj_set_pos(mark, 77, 31);
lv_obj_set_style_bg_opa(mark, LV_OPA_TRANSP, 0);
lv_obj_set_style_border_width(mark, 0, 0);
lv_obj_set_style_pad_all(mark, 0, 0);
lv_obj_clear_flag(mark, LV_OBJ_FLAG_SCROLLABLE);
for (int i = 0; i < 8; ++i) {
lv_obj_t* line = lv_line_create(mark);
lv_line_set_points(line, fireworkRays[i], 2);
lv_obj_set_style_line_width(line, (i < 4) ? 2 : 1, 0);
lv_obj_set_style_line_color(line, lv_color_black(), 0);
}
lv_obj_t* centre = lv_obj_create(mark);
lv_obj_set_size(centre, 8, 8);
lv_obj_set_pos(centre, 19, 19);
lv_obj_set_style_radius(centre, LV_RADIUS_CIRCLE, 0);
lv_obj_set_style_bg_color(centre, lv_color_black(), 0);
lv_obj_set_style_border_width(centre, 0, 0);
}
void BoomDisplay::createScreens() {
// ---------------------------------------------------------------------------
// Screen 1: passerby-facing privacy / purpose screen
// ---------------------------------------------------------------------------
publicScreen_ = lv_obj_create(nullptr);
stylePlainScreen(publicScreen_);
publicWifi_ = makeStatusChip(publicScreen_, 8, "--", true);
publicSd_ = makeStatusChip(publicScreen_, 110, "--", false);
createFireworkMark(publicScreen_);
lv_obj_t* title = lv_label_create(publicScreen_);
lv_label_set_text(title, "Vuurwerkdetector");
lv_obj_set_width(title, 190);
lv_obj_set_style_text_align(title, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 81);
lv_obj_t* message = lv_label_create(publicScreen_);
lv_label_set_text(message, "Luistert naar knallen,\nniet naar gesprekken.\nGeen continue opname.");
lv_obj_set_width(message, 190);
lv_obj_set_style_text_align(message, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(message, LV_ALIGN_TOP_MID, 0, 101);
publicCount_ = lv_label_create(publicScreen_);
lv_label_set_text(publicCount_, "Vandaag: 0");
lv_obj_set_width(publicCount_, 190);
lv_obj_set_style_text_align(publicCount_, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(publicCount_, LV_ALIGN_BOTTOM_MID, 0, -27);
publicCapture_ = lv_label_create(publicScreen_);
lv_label_set_text(publicCapture_, "BOOT: details");
lv_obj_set_width(publicCapture_, 190);
lv_obj_set_style_text_align(publicCapture_, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(publicCapture_, LV_ALIGN_BOTTOM_MID, 0, -5);
// ---------------------------------------------------------------------------
// Screen 2: owner / technical status
// ---------------------------------------------------------------------------
detailScreen_ = lv_obj_create(nullptr);
stylePlainScreen(detailScreen_);
detailWifi_ = makeStatusChip(detailScreen_, 8, "--", true);
detailSd_ = makeStatusChip(detailScreen_, 110, "--", false);
lv_obj_t* statusTitle = lv_label_create(detailScreen_);
lv_label_set_text(statusTitle, "BoomCast status");
lv_obj_align(statusTitle, LV_ALIGN_TOP_LEFT, 10, 40);
detailToday_ = lv_label_create(detailScreen_);
lv_obj_set_pos(detailToday_, 10, 66);
detailQueue_ = lv_label_create(detailScreen_);
lv_obj_set_pos(detailQueue_, 10, 88);
detailSensitivity_ = lv_label_create(detailScreen_);
lv_obj_set_pos(detailSensitivity_, 10, 110);
detailMode_ = lv_label_create(detailScreen_);
lv_obj_set_pos(detailMode_, 10, 132);
detailCapture_ = lv_label_create(detailScreen_);
lv_obj_set_pos(detailCapture_, 10, 154);
detailIp_ = lv_label_create(detailScreen_);
lv_obj_set_width(detailIp_, 190);
lv_obj_set_style_text_align(detailIp_, LV_TEXT_ALIGN_LEFT, 0);
lv_obj_align(detailIp_, LV_ALIGN_BOTTOM_LEFT, 10, -8);
}
void BoomDisplay::requestFullRefresh() {
fullRefreshNext_ = true;
if (display_) lv_obj_invalidate(lv_scr_act());
}
void BoomDisplay::showPage(uint8_t page) {
page_ = page % 2;
if (!lock()) return;
lv_scr_load(page_ == 0 ? publicScreen_ : detailScreen_);
if (FULL_REFRESH_ON_PAGE_SWITCH) requestFullRefresh();
unlock();
}
void BoomDisplay::nextPage() {
showPage((page_ + 1) % 2);
}
void BoomDisplay::applySnapshot(const BoomStateSnapshot& s) {
const char* wifiText = s.wifiConnected ? "OK" : (s.apMode ? "AP" : "--");
const char* sdText = s.sdReady ? "OK" : "--";
lv_label_set_text(publicWifi_, wifiText);
lv_label_set_text(detailWifi_, wifiText);
lv_label_set_text(publicSd_, sdText);
lv_label_set_text(detailSd_, sdText);
if (SHOW_TODAY_COUNT_ON_PUBLIC_SCREEN) {
lv_label_set_text_fmt(publicCount_, "Vandaag: %d", s.todayEvents);
lv_obj_clear_flag(publicCount_, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag(publicCount_, LV_OBJ_FLAG_HIDDEN);
}
lv_label_set_text_fmt(detailToday_, "Vandaag gevangen: %d", s.todayEvents);
lv_label_set_text_fmt(detailQueue_, "Nog te synchroniseren: %d", s.queueCount);
lv_label_set_text_fmt(detailSensitivity_, "Gevoeligheid: %u%% E:%lu", s.sensitivity,
(unsigned long)(s.audioReadErrors + s.recordingErrors));
lv_label_set_text_fmt(detailMode_, "Netwerk: %s", s.apMode && !s.wifiConnected ? "setup-AP" : "WiFi");
const char* state = s.capturing ? "Opname" : s.syncing ? "Uploaden" :
s.lastSyncMessage.startsWith("Submitted") ? "Verzonden" :
s.lastSyncMessage.startsWith("Upload failed") ? "Fout / herproberen" :
!s.wifiConnected && s.queueCount ? "Offline / wachtrij" : "Luisteren";
lv_label_set_text(detailCapture_, state);
lv_label_set_text_fmt(detailIp_, "Beheer: http://%s", s.ip.length() ? s.ip.c_str() : "-");
lv_label_set_text(publicCapture_, s.capturing ? "Knal gezien..." : "BOOT: details");
}
void BoomDisplay::update() {
if (millis() - lastStatusUpdateMs_ < DISPLAY_STATUS_UPDATE_MS) return;
lastStatusUpdateMs_ = millis();
BoomStateSnapshot current = boomState.snapshot();
bool changed = !haveLast_ ||
current.wifiConnected != last_.wifiConnected ||
current.apMode != last_.apMode ||
current.sdReady != last_.sdReady ||
current.capturing != last_.capturing ||
current.queueCount != last_.queueCount ||
current.syncing != last_.syncing ||
current.lastSyncMessage != last_.lastSyncMessage ||
current.audioReadErrors != last_.audioReadErrors ||
current.recordingErrors != last_.recordingErrors ||
current.todayEvents != last_.todayEvents ||
current.sensitivity != last_.sensitivity ||
current.ip != last_.ip;
if (!changed) return;
if (lock()) {
applySnapshot(current);
// Clear old digits after a discard or explicit counter reset on ePaper.
if (haveLast_ && current.todayEvents < last_.todayEvents) requestFullRefresh();
unlock();
last_ = current;
haveLast_ = true;
}
}