feat: add MeshPocket V2 support

This commit is contained in:
Quency-D committed 2026-11-10 18:34:03 +08:00
1 parent 9f10793f7a
commit ae8cc34e10
25 files changed
+1210 -37

No files matched your search

+6
View File
@@ -42,6 +42,8 @@ namespace mesh {
#define BD_STARTUP_NORMAL 0 // getStartupReason() codes
#define BD_STARTUP_RX_PACKET 1
class BoardUiExtension;
class MainBoard {
public:
virtual uint16_t getBattMilliVolts() = 0;
@@ -70,6 +72,7 @@ public:
virtual bool isExternalPowered() { return false; }
// True while a board can identify an active battery-charging source.
virtual bool isChargerActive() { return false; }
virtual bool hasChargerStatus() const { return false; }
// Optional, source-specific power detection. Boards that can distinguish a USB
// supply from a solar charger override these; defaults keep every other board
@@ -85,6 +88,9 @@ public:
virtual uint8_t getShutdownReason() const { return 0; }
virtual const char* getShutdownReasonString(uint8_t reason) { return "Not available"; }
// Optional full-screen board UI. The returned object remains owned by the board.
virtual BoardUiExtension* getUiExtension() { return nullptr; }
virtual bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply) { return false; }
// Called from the example main loops. Lets a board feed its watchdog and
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include <stdint.h>
class DisplayDriver;
namespace mesh {
class BoardUiExtension {
public:
enum Event : uint8_t {
EVENT_NONE = 0,
EVENT_SHOW_PAGE = 1 << 0,
EVENT_PAGE_ACTION = 1 << 1,
EVENT_REQUEST_FOCUS = 1 << 2,
};
// Consume and return all board-owned input and focus events for this loop.
virtual uint8_t pollEvents() = 0;
virtual void handlePageAction() {}
// The extension owns startFrame() and any coordinate-mode changes. The UI
// host calls endFrame() after render() returns.
virtual uint32_t render(DisplayDriver& display, bool fullRefresh) = 0;
};
}
+74
View File
@@ -0,0 +1,74 @@
#pragma once
#include <MeshCore.h>
#include "BoardUiExtension.h"
#include "DisplayDriver.h"
class BoardUiScreen {
mesh::BoardUiExtension* extension = nullptr;
DisplayDriver* display = nullptr;
bool selected = false;
bool fullRefresh = true;
void selectPage() {
if (!selected) {
selected = true;
fullRefresh = true;
}
}
public:
void begin(mesh::MainBoard& board, DisplayDriver& screen) {
extension = board.getUiExtension();
display = &screen;
}
bool available() const { return extension != nullptr; }
uint8_t pollEvents() {
if (!extension) return mesh::BoardUiExtension::EVENT_NONE;
if (!display->isOn()) fullRefresh = true;
return extension->pollEvents();
}
bool handleInput(uint8_t events, bool canOperate) {
if (!extension || !canOperate) return false;
const bool awake = display->isOn();
if (events & mesh::BoardUiExtension::EVENT_SHOW_PAGE) {
if (!awake) display->turnOn();
selectPage();
return true;
}
if ((events & mesh::BoardUiExtension::EVENT_PAGE_ACTION) && awake && selected) {
extension->handlePageAction();
return true;
}
return false;
}
bool handleFocusRequest(uint8_t events, bool canTakeFocus, bool handlingInput) {
if (!extension || !(events & mesh::BoardUiExtension::EVENT_REQUEST_FOCUS) ||
!canTakeFocus || handlingInput) return false;
const bool awake = display->isOn();
if (!awake) display->turnOn();
selectPage();
if (!awake) fullRefresh = true;
return true;
}
bool togglePage() {
if (!extension) return false;
selected = !selected;
fullRefresh = true;
return true;
}
bool renderIfSelected(uint32_t& interval) {
if (!extension || !selected) return false;
interval = extension->render(*display, fullRefresh);
fullRefresh = false;
return true;
}
};
+4
View File
@@ -15,12 +15,16 @@ class DisplayDriver {
int _w, _h;
protected:
DisplayDriver(int w, int h) { _w = w; _h = h; }
void setLogicalSize(int w, int h) { _w = w; _h = h; }
public:
//enum Color { DARK=0, LIGHT, RED, GREEN, BLUE, YELLOW, ORANGE }; // on b/w screen, colors will be !=0 synonym of light
int width() const { return _w; }
int height() const { return _h; }
// Optional physical-pixel coordinate mode. Unsupported displays keep their
// existing logical coordinate system.
virtual void setNativePixels(bool) {}
virtual bool isOn() = 0;
virtual bool isEink() { return false; } // default to non-eink, override in eink drivers
virtual void forceFullRefresh() {} // next refresh will be full for eink
+39 -8
View File
@@ -107,21 +107,27 @@ ColorVal UIColor::popup_bkg = 0x07FF; // CYAN
ColorVal UIColor::popup_txt = 0x0000;
ColorVal UIColor::corp_blue = 0x001A;
static int scaleX(int x) {
return (int)(x * DISPLAY_SCALE_X);
void NV3001BDisplay::setNativePixels(bool enabled) {
nativePixels = enabled;
setLogicalSize(enabled ? NV3001B_SCREEN_WIDTH : NV3001B_LOGICAL_WIDTH,
enabled ? NV3001B_SCREEN_HEIGHT : NV3001B_LOGICAL_HEIGHT);
}
static int scaleY(int y) {
return (int)(y * DISPLAY_SCALE_Y);
int NV3001BDisplay::scaleX(int x) const {
return nativePixels ? x : (int)(x * DISPLAY_SCALE_X);
}
static int scaleWidth(int x, int w) {
int NV3001BDisplay::scaleY(int y) const {
return nativePixels ? y : (int)(y * DISPLAY_SCALE_Y);
}
int NV3001BDisplay::scaleWidth(int x, int w) const {
if (w <= 0) return 0;
int scaled = scaleX(x + w) - scaleX(x);
return scaled > 0 ? scaled : 1;
}
static int scaleHeight(int y, int h) {
int NV3001BDisplay::scaleHeight(int y, int h) const {
if (h <= 0) return 0;
int scaled = scaleY(y + h) - scaleY(y);
return scaled > 0 ? scaled : 1;
@@ -183,7 +189,8 @@ static int textPixelScaleX(uint8_t size) {
return size <= 1 ? NV3001B_TEXT_SIZE1_SCALE_X : NV3001B_TEXT_SIZE2_SCALE_X;
}
static int textPixelScaleY(uint8_t size) {
int NV3001BDisplay::textPixelScaleY(uint8_t size) const {
if (nativePixels) return textPixelScaleX(size); // Square glyph pixels on the native canvas.
return size <= 1 ? NV3001B_TEXT_SIZE1_SCALE_Y : NV3001B_TEXT_SIZE2_SCALE_Y;
}
@@ -254,10 +261,26 @@ void NV3001BDisplay::writeColor(uint16_t rgb, uint32_t count) {
spi.beginTransaction(SPISettings(SPI_FREQUENCY, MSBFIRST, SPI_MODE0));
digitalWrite(PIN_TFT_DC, HIGH);
digitalWrite(PIN_TFT_CS, LOW);
#ifdef NRF52_PLATFORM
// EasyDMA needs a RAM buffer. Send repeated pixels in bursts at 4 MHz.
uint8_t pixels[64];
size_t bytes = sizeof(pixels);
if (count < bytes / 2) bytes = count * 2;
for (size_t i = 0; i < bytes; i += 2) {
pixels[i] = hi;
pixels[i + 1] = lo;
}
while (count) {
if (count < bytes / 2) bytes = count * 2;
spi.transfer(pixels, nullptr, bytes);
count -= bytes / 2;
}
#else
while (count--) {
spi.transfer(hi);
spi.transfer(lo);
}
#endif
digitalWrite(PIN_TFT_CS, HIGH);
spi.endTransaction();
}
@@ -425,7 +448,11 @@ bool NV3001BDisplay::begin() {
digitalWrite(PIN_TFT_DC, HIGH);
delay(20);
#ifdef NRF52_PLATFORM
spi.begin(); // Pins are set by the nRF52 variant.
#else
spi.begin(PIN_TFT_SCL, PIN_TFT_MISO, PIN_TFT_SDA, PIN_TFT_CS);
#endif
if (PIN_TFT_RST >= 0) {
pinMode(PIN_TFT_RST, OUTPUT);
digitalWrite(PIN_TFT_RST, HIGH);
@@ -456,6 +483,9 @@ void NV3001BDisplay::turnOff() {
if (!is_on) return;
writeOptionalPin(PIN_TFT_BL, !PIN_TFT_BL_ACTIVE);
#ifdef NRF52_PLATFORM
spi.end();
#endif
writeOptionalPin(PIN_TFT_EN, !PIN_TFT_EN_ACTIVE);
is_on = false;
if (periph_power) periph_power->release();
@@ -544,7 +574,8 @@ uint16_t NV3001BDisplay::getTextWidth(const char* str) {
uint16_t len = 0;
while (str[len] && str[len] != '\n' && str[len] != '\r') len++;
return (uint16_t)((len * 6 * textPixelScaleX(text_size)) / DISPLAY_SCALE_X);
const int pixels = len * 6 * textPixelScaleX(text_size);
return nativePixels ? pixels : (uint16_t)(pixels / DISPLAY_SCALE_X);
}
void NV3001BDisplay::endFrame() {
+17
View File
@@ -25,13 +25,24 @@
#endif
class NV3001BDisplay : public DisplayDriver {
#ifdef NRF52_PLATFORM
SPIClass& spi;
#else
SPIClass spi;
#endif
RefCountedDigitalPin* periph_power;
bool is_on = false;
uint16_t color = 0xffff;
uint8_t text_size = 1;
int cursor_x = 0;
int cursor_y = 0;
bool nativePixels = false;
int scaleX(int x) const;
int scaleY(int y) const;
int scaleWidth(int x, int w) const;
int scaleHeight(int y, int h) const;
int textPixelScaleY(uint8_t size) const;
void writeCommand(uint8_t cmd);
void writeBytes(const uint8_t* data, size_t len);
@@ -43,10 +54,16 @@ class NV3001BDisplay : public DisplayDriver {
void drawChar(int x, int y, char ch);
public:
#ifdef NRF52_PLATFORM
explicit NV3001BDisplay(SPIClass& bus) :
DisplayDriver(NV3001B_LOGICAL_WIDTH, NV3001B_LOGICAL_HEIGHT), spi(bus), periph_power(nullptr) { }
#else
NV3001BDisplay(RefCountedDigitalPin* power = nullptr) :
DisplayDriver(NV3001B_LOGICAL_WIDTH, NV3001B_LOGICAL_HEIGHT), spi(NV3001B_SPI_HOST), periph_power(power) { }
#endif
bool begin();
void setNativePixels(bool enabled) override;
static const char* driverName() { return "NV3001B"; }
static uint16_t physicalWidth() { return NV3001B_PANEL_WIDTH; }
static uint16_t physicalHeight() { return NV3001B_PANEL_HEIGHT; }