mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-07-26 14:58:12 +00:00
feat: runtime font switcher — Default vs Lemon in Settings > Display
- Add Lemon font (LemonFont.h, LemonIcons.h) to the standard build alongside the Adafruit built-in font; ~101 KB added to flash - SH1106Display: add _use_lemon flag + setLemonFont(bool) to switch at runtime; print() and getTextWidth() use Lemon path when active; translateUTF8ToBlocks() passes UTF-8 through unchanged (Lemon supports full Unicode U+0020-U+04FF) vs transliterating for the default font - DisplayDriver: add getCharWidth()/getLineHeight() virtuals (defaults 6/8); SH1106Display returns 5/9 for Lemon, 6/8 for default; add setLemonFont() no-op virtual; fix orphaned UTF-8 leading byte in drawTextEllipsized - FullscreenMsgView: replace fixed-char-count word-wrap with pixel-accurate wrap using display.getTextWidth(); use getLineHeight() for spacing so Lemon (9 px) and default (8 px) both render correctly - NodePrefs: add use_lemon_font field (0=default, 1=Lemon) - SettingsScreen: add "Font" item in Display section (Default / Lemon), calls UITask::applyFont() on change - UITask: add applyFont() that calls display->setLemonFont(use_lemon_font); called at startup (begin()) and when the setting changes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -78,4 +78,5 @@ struct NodePrefs { // persisted to file
|
||||
struct DmMelodyEntry { uint8_t prefix[4]; uint8_t slot; }; // slot: 0=global,1=melody1,2=melody2
|
||||
static const int DM_MELODY_TABLE_MAX = 16;
|
||||
DmMelodyEntry dm_melody[DM_MELODY_TABLE_MAX];
|
||||
uint8_t use_lemon_font; // 0=default Adafruit font, 1=Lemon font (Unicode, pixel-accurate wrap)
|
||||
};
|
||||
@@ -3,10 +3,8 @@
|
||||
#include <helpers/ui/DisplayDriver.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
static const int FS_CHARS = 20;
|
||||
static const int FS_LINE_H = 8;
|
||||
static const int FS_START_Y = 12;
|
||||
static const int FS_VISIBLE = (64 - FS_START_Y) / FS_LINE_H;
|
||||
static const int FS_CHARS_MAX = 80; // max bytes per wrapped line
|
||||
static const int FS_START_Y = 12;
|
||||
|
||||
struct FullscreenMsgView {
|
||||
int scroll;
|
||||
@@ -18,23 +16,49 @@ struct FullscreenMsgView {
|
||||
|
||||
void begin() { scroll = 0; active = true; }
|
||||
|
||||
static int wrapLines(const char* text, char out[][FS_CHARS + 1], int max_lines) {
|
||||
// Pixel-accurate word-wrap: breaks at last space that fits within max_px,
|
||||
// hard-breaks long words. Works for both fixed- and variable-width fonts.
|
||||
static int wrapLines(DisplayDriver& display, const char* text, int max_px,
|
||||
char out[][FS_CHARS_MAX], int max_lines) {
|
||||
int count = 0;
|
||||
const char* p = text;
|
||||
while (*p && count < max_lines) {
|
||||
int len = strlen(p);
|
||||
if (len <= FS_CHARS) {
|
||||
strncpy(out[count++], p, FS_CHARS);
|
||||
out[count - 1][len] = '\0';
|
||||
break;
|
||||
const char* seg = text;
|
||||
while (*seg && count < max_lines) {
|
||||
const char* p = seg;
|
||||
const char* last_sp = nullptr;
|
||||
const char* last_fit = seg;
|
||||
|
||||
while (*p && (p - seg) < FS_CHARS_MAX - 1) {
|
||||
uint8_t b0 = (uint8_t)*p;
|
||||
int cb = (b0 < 0x80) ? 1 : (b0 < 0xE0) ? 2 : (b0 < 0xF0) ? 3 : 4;
|
||||
if ((p - seg) + cb >= FS_CHARS_MAX) break;
|
||||
|
||||
char buf[FS_CHARS_MAX];
|
||||
int n = (int)(p - seg) + cb;
|
||||
memcpy(buf, seg, n); buf[n] = '\0';
|
||||
if (display.getTextWidth(buf) > max_px && p > seg) break;
|
||||
|
||||
if (*p == ' ') last_sp = p;
|
||||
p += cb;
|
||||
last_fit = p;
|
||||
}
|
||||
int brk = FS_CHARS;
|
||||
for (int i = FS_CHARS - 1; i > 0; i--) {
|
||||
if (p[i] == ' ') { brk = i; break; }
|
||||
|
||||
const char* end;
|
||||
const char* next_seg;
|
||||
if (!*p) {
|
||||
end = p; next_seg = p;
|
||||
} else if (last_sp && last_sp > seg) {
|
||||
end = last_sp; next_seg = last_sp + 1;
|
||||
} else {
|
||||
end = last_fit; next_seg = last_fit;
|
||||
}
|
||||
strncpy(out[count], p, brk);
|
||||
out[count++][brk] = '\0';
|
||||
p += brk + (p[brk] == ' ' ? 1 : 0);
|
||||
|
||||
int len = (int)(end - seg);
|
||||
if (len <= 0) { seg = *seg ? seg + 1 : seg; continue; }
|
||||
if (len > FS_CHARS_MAX - 1) len = FS_CHARS_MAX - 1;
|
||||
memcpy(out[count], seg, len);
|
||||
out[count][len] = '\0';
|
||||
count++;
|
||||
seg = next_seg;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -42,6 +66,8 @@ struct FullscreenMsgView {
|
||||
int render(DisplayDriver& display, const char* sender, const char* text,
|
||||
bool has_prev, bool has_next) {
|
||||
display.setTextSize(1);
|
||||
const int lineH = display.getLineHeight();
|
||||
const int max_px = display.width() - 6;
|
||||
|
||||
// detect @recipient at start of message
|
||||
char to_nick[32] = "";
|
||||
@@ -59,7 +85,7 @@ struct FullscreenMsgView {
|
||||
|
||||
const int header_h = to_nick[0] ? 20 : 10;
|
||||
const int startY = header_h + 2;
|
||||
const int visible = (display.height() - startY - FS_LINE_H) / FS_LINE_H;
|
||||
const int visible = (display.height() - startY - lineH) / lineH;
|
||||
|
||||
display.setColor(DisplayDriver::LIGHT);
|
||||
display.fillRect(0, 0, display.width(), header_h);
|
||||
@@ -75,35 +101,36 @@ struct FullscreenMsgView {
|
||||
|
||||
char trans_text[512];
|
||||
display.translateUTF8ToBlocks(trans_text, body, sizeof(trans_text));
|
||||
char lines[12][FS_CHARS + 1];
|
||||
int lcount = wrapLines(trans_text, lines, 12);
|
||||
char lines[12][FS_CHARS_MAX];
|
||||
int lcount = wrapLines(display, trans_text, max_px, lines, 12);
|
||||
int max_scroll = lcount > visible ? lcount - visible : 0;
|
||||
if (scroll > max_scroll) scroll = max_scroll;
|
||||
|
||||
for (int i = 0; i < visible && (scroll + i) < lcount; i++) {
|
||||
display.setCursor(0, startY + i * FS_LINE_H);
|
||||
display.setCursor(0, startY + i * lineH);
|
||||
display.print(lines[scroll + i]);
|
||||
}
|
||||
if (scroll > 0) {
|
||||
display.setColor(DisplayDriver::DARK);
|
||||
display.fillRect(display.width() - 6, startY, 6, FS_LINE_H);
|
||||
display.fillRect(display.width() - 6, startY, 6, lineH);
|
||||
display.setColor(DisplayDriver::LIGHT);
|
||||
display.setCursor(display.width() - 6, startY);
|
||||
display.print("^");
|
||||
}
|
||||
if (scroll < max_scroll) {
|
||||
display.setColor(DisplayDriver::DARK);
|
||||
display.fillRect(display.width() - 6, startY + (visible - 1) * FS_LINE_H, 6, FS_LINE_H);
|
||||
display.fillRect(display.width() - 6, startY + (visible - 1) * lineH, 6, lineH);
|
||||
display.setColor(DisplayDriver::LIGHT);
|
||||
display.setCursor(display.width() - 6, startY + (visible - 1) * FS_LINE_H);
|
||||
display.setCursor(display.width() - 6, startY + (visible - 1) * lineH);
|
||||
display.print("v");
|
||||
}
|
||||
const int nav_y = display.height() - lineH;
|
||||
if (has_next) {
|
||||
display.setCursor(0, 56);
|
||||
display.setCursor(0, nav_y);
|
||||
display.print("<");
|
||||
}
|
||||
if (has_prev) {
|
||||
display.setCursor(display.width() - 6, 56);
|
||||
display.setCursor(display.width() - 6, nav_y);
|
||||
display.print(">");
|
||||
}
|
||||
return 2000;
|
||||
|
||||
@@ -14,6 +14,7 @@ class SettingsScreen : public UIScreen {
|
||||
BATT_DISPLAY,
|
||||
CLOCK_SECONDS,
|
||||
CLOCK_FORMAT,
|
||||
FONT,
|
||||
// Sound section
|
||||
SECTION_SOUND,
|
||||
BUZZER,
|
||||
@@ -316,6 +317,10 @@ class SettingsScreen : public UIScreen {
|
||||
display.print("Format");
|
||||
display.setCursor(VAL_X, y);
|
||||
display.print((p && p->clock_12h) ? "12h" : "24h");
|
||||
} else if (item == FONT) {
|
||||
display.print("Font");
|
||||
display.setCursor(VAL_X, y);
|
||||
display.print((p && p->use_lemon_font) ? "Lemon" : "Default");
|
||||
} else if (item == DM_FILTER) {
|
||||
display.print("DM");
|
||||
display.setCursor(VAL_X, y);
|
||||
@@ -506,6 +511,12 @@ public:
|
||||
_dirty = true;
|
||||
return true;
|
||||
}
|
||||
if (_selected == FONT && p && (left || right || enter)) {
|
||||
p->use_lemon_font ^= 1;
|
||||
_task->applyFont();
|
||||
_dirty = true;
|
||||
return true;
|
||||
}
|
||||
if (_selected == DM_FILTER && p && (left || right || enter)) {
|
||||
p->dm_show_all = p->dm_show_all ? 0 : 1;
|
||||
_dirty = true;
|
||||
|
||||
@@ -816,6 +816,7 @@ void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* no
|
||||
setCurrScreen(splash);
|
||||
|
||||
applyBrightness();
|
||||
applyFont();
|
||||
}
|
||||
|
||||
void UITask::gotoSettingsScreen() {
|
||||
@@ -1557,6 +1558,12 @@ void UITask::applyBrightness() {
|
||||
}
|
||||
}
|
||||
|
||||
void UITask::applyFont() {
|
||||
if (_display != NULL && _node_prefs != NULL) {
|
||||
_display->setLemonFont(_node_prefs->use_lemon_font != 0);
|
||||
}
|
||||
}
|
||||
|
||||
void UITask::setBrightnessLevel(uint8_t level) {
|
||||
if (_node_prefs == NULL) return;
|
||||
if (level > 4) level = 4;
|
||||
|
||||
@@ -158,6 +158,7 @@ public:
|
||||
uint8_t getBuzzerVolume() const { return _node_prefs ? _node_prefs->buzzer_volume : 4; }
|
||||
void applyTxPower();
|
||||
void applyGPSInterval();
|
||||
void applyFont();
|
||||
uint32_t autoOffMillis() const {
|
||||
if (!_node_prefs || _node_prefs->auto_off_secs == 0) return 0;
|
||||
return (uint32_t)_node_prefs->auto_off_secs * 1000UL;
|
||||
|
||||
@@ -27,6 +27,9 @@ public:
|
||||
virtual void drawRect(int x, int y, int w, int h) = 0;
|
||||
virtual void drawXbm(int x, int y, const uint8_t* bits, int w, int h) = 0;
|
||||
virtual uint16_t getTextWidth(const char* str) = 0;
|
||||
virtual int getCharWidth() const { return 6; } // typical character advance width (px)
|
||||
virtual int getLineHeight() const { return 8; } // pixel rows per text line
|
||||
virtual void setLemonFont(bool) { } // no-op; overridden by displays that support Lemon
|
||||
virtual void drawTextCentered(int mid_x, int y, const char* str) { // helper method (override to optimise)
|
||||
int w = getTextWidth(str);
|
||||
setCursor(mid_x - w/2, y);
|
||||
@@ -182,6 +185,10 @@ public:
|
||||
while (str_len > 0 && getTextWidth(temp_str) > max_width - ellipsis_width) {
|
||||
temp_str[--str_len] = 0;
|
||||
}
|
||||
// Strip orphaned UTF-8 leading byte left by byte-at-a-time trimming above.
|
||||
while (str_len > 0 && ((uint8_t)temp_str[str_len - 1] & 0xC0) == 0xC0) {
|
||||
temp_str[--str_len] = 0;
|
||||
}
|
||||
strcat(temp_str, ellipsis);
|
||||
|
||||
setCursor(x, y);
|
||||
|
||||
1523
src/helpers/ui/LemonFont.h
Normal file
1523
src/helpers/ui/LemonFont.h
Normal file
File diff suppressed because it is too large
Load Diff
19
src/helpers/ui/LemonIcons.h
Normal file
19
src/helpers/ui/LemonIcons.h
Normal file
@@ -0,0 +1,19 @@
|
||||
// Lemon font icon glyphs (Private Use Area), converted manually from lemon.bdf
|
||||
// Add new icons by appending to the bitmap array, lemonIconCPs, and lemonIconGlyphs.
|
||||
#pragma once
|
||||
#include <Adafruit_GFX.h>
|
||||
|
||||
static const uint8_t lemonIconBitmaps[] PROGMEM = {
|
||||
0xEE, 0xBA, 0x2B, 0xA3, 0xB0, // U+E03B mute: 6×6, yoff=0
|
||||
};
|
||||
|
||||
static const uint32_t lemonIconCPs[] PROGMEM = {
|
||||
0xE03B, // mute
|
||||
};
|
||||
|
||||
static const GFXglyph lemonIconGlyphs[] PROGMEM = {
|
||||
// off w h adv xo yo
|
||||
{ 0, 6, 6, 5, 0, -6 }, // U+E03B mute
|
||||
};
|
||||
|
||||
static const uint8_t lemonIconCount = sizeof(lemonIconCPs) / sizeof(lemonIconCPs[0]);
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "SH1106Display.h"
|
||||
#include <Adafruit_GrayOLED.h>
|
||||
#include "Adafruit_SH110X.h"
|
||||
#include "LemonFont.h"
|
||||
#include "LemonIcons.h"
|
||||
|
||||
bool SH1106Display::i2c_probe(TwoWire &wire, uint8_t addr)
|
||||
{
|
||||
@@ -60,9 +62,98 @@ void SH1106Display::setCursor(int x, int y)
|
||||
display.setCursor(x, y);
|
||||
}
|
||||
|
||||
uint32_t SH1106Display::decodeUtf8(const uint8_t*& p) {
|
||||
uint8_t c = *p++;
|
||||
if (c < 0x80) return c;
|
||||
if ((c & 0xE0) == 0xC0) {
|
||||
uint32_t cp = c & 0x1F;
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
return cp;
|
||||
}
|
||||
if ((c & 0xF0) == 0xE0) {
|
||||
uint32_t cp = c & 0x0F;
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
return cp;
|
||||
}
|
||||
if ((c & 0xF8) == 0xF0) {
|
||||
uint32_t cp = c & 0x07;
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
return cp;
|
||||
}
|
||||
while (*p && (*p & 0xC0) == 0x80) p++;
|
||||
return 0xFFFD;
|
||||
}
|
||||
|
||||
int16_t SH1106Display::drawLemonChar(int16_t x, int16_t y, uint32_t cp) {
|
||||
for (uint8_t i = 0; i < lemonIconCount; i++) {
|
||||
if (pgm_read_dword(&lemonIconCPs[i]) == cp) {
|
||||
const GFXglyph* g = &lemonIconGlyphs[i];
|
||||
uint8_t w = pgm_read_byte(&g->width), h = pgm_read_byte(&g->height);
|
||||
int8_t xo = (int8_t)pgm_read_byte(&g->xOffset), yo = (int8_t)pgm_read_byte(&g->yOffset);
|
||||
uint8_t xa = pgm_read_byte(&g->xAdvance);
|
||||
uint16_t bo = pgm_read_word(&g->bitmapOffset);
|
||||
uint8_t bits = 0, bit = 0;
|
||||
for (uint8_t row = 0; row < h; row++)
|
||||
for (uint8_t col = 0; col < w; col++) {
|
||||
if (!bit) { bits = pgm_read_byte(&lemonIconBitmaps[bo++]); bit = 0x80; }
|
||||
if (bits & bit) display.drawPixel(x + xo + col, y + 6 + yo + row, _color);
|
||||
bit >>= 1;
|
||||
}
|
||||
return x + xa;
|
||||
}
|
||||
}
|
||||
|
||||
if (cp < Lemon.first || cp > Lemon.last) {
|
||||
if (cp >= 0x20) display.fillRect(x + 1, y - 1, 4, 6, _color);
|
||||
return x + 6;
|
||||
}
|
||||
const GFXglyph* g = &lemonGlyphs[cp - Lemon.first];
|
||||
uint8_t w = pgm_read_byte(&g->width), h = pgm_read_byte(&g->height);
|
||||
int8_t xo = (int8_t)pgm_read_byte(&g->xOffset), yo = (int8_t)pgm_read_byte(&g->yOffset);
|
||||
uint8_t xa = pgm_read_byte(&g->xAdvance);
|
||||
uint16_t bo = pgm_read_word(&g->bitmapOffset);
|
||||
uint8_t bits = 0, bit = 0;
|
||||
for (uint8_t row = 0; row < h; row++)
|
||||
for (uint8_t col = 0; col < w; col++) {
|
||||
if (!bit) { bits = pgm_read_byte(&lemonBitmaps[bo++]); bit = 0x80; }
|
||||
if (bits & bit) display.drawPixel(x + xo + col, y + 6 + yo + row, _color);
|
||||
bit >>= 1;
|
||||
}
|
||||
return x + xa;
|
||||
}
|
||||
|
||||
uint8_t SH1106Display::lemonXAdvance(uint32_t cp) {
|
||||
if (cp < Lemon.first || cp > Lemon.last) return 6;
|
||||
return pgm_read_byte(&lemonGlyphs[cp - Lemon.first].xAdvance);
|
||||
}
|
||||
|
||||
void SH1106Display::translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) {
|
||||
if (_use_lemon) {
|
||||
size_t n = strlen(src);
|
||||
if (n >= dest_size) n = dest_size - 1;
|
||||
memcpy(dest, src, n);
|
||||
dest[n] = '\0';
|
||||
} else {
|
||||
DisplayDriver::translateUTF8ToBlocks(dest, src, dest_size);
|
||||
}
|
||||
}
|
||||
|
||||
void SH1106Display::print(const char *str)
|
||||
{
|
||||
display.print(str);
|
||||
if (!_use_lemon) { display.print(str); return; }
|
||||
|
||||
int16_t cx = display.getCursorX();
|
||||
int16_t cy = display.getCursorY();
|
||||
const uint8_t* p = (const uint8_t*)str;
|
||||
while (*p) {
|
||||
uint32_t cp = decodeUtf8(p);
|
||||
if (cp == '\n') { cy += Lemon.yAdvance; cx = 0; }
|
||||
else { cx = drawLemonChar(cx, cy, cp); }
|
||||
}
|
||||
display.setCursor(cx, cy);
|
||||
}
|
||||
|
||||
void SH1106Display::fillRect(int x, int y, int w, int h)
|
||||
@@ -82,6 +173,12 @@ void SH1106Display::drawXbm(int x, int y, const uint8_t *bits, int w, int h)
|
||||
|
||||
uint16_t SH1106Display::getTextWidth(const char *str)
|
||||
{
|
||||
if (_use_lemon) {
|
||||
uint16_t width = 0;
|
||||
const uint8_t* p = (const uint8_t*)str;
|
||||
while (*p) width += lemonXAdvance(decodeUtf8(p));
|
||||
return width;
|
||||
}
|
||||
int16_t x1, y1;
|
||||
uint16_t w, h;
|
||||
display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
|
||||
|
||||
@@ -21,11 +21,17 @@ class SH1106Display : public DisplayDriver
|
||||
uint8_t _color;
|
||||
uint8_t _contrast;
|
||||
uint8_t _precharge;
|
||||
bool _use_lemon;
|
||||
|
||||
bool i2c_probe(TwoWire &wire, uint8_t addr);
|
||||
static uint32_t decodeUtf8(const uint8_t*& p);
|
||||
int16_t drawLemonChar(int16_t x, int16_t y, uint32_t cp);
|
||||
uint8_t lemonXAdvance(uint32_t cp);
|
||||
|
||||
public:
|
||||
SH1106Display() : DisplayDriver(128, 64), display(128, 64, &Wire, PIN_OLED_RESET) { _isOn = false; _contrast = 255; _precharge = 0x1F; }
|
||||
SH1106Display() : DisplayDriver(128, 64), display(128, 64, &Wire, PIN_OLED_RESET) {
|
||||
_isOn = false; _contrast = 255; _precharge = 0x1F; _use_lemon = false;
|
||||
}
|
||||
bool begin();
|
||||
|
||||
bool isOn() override { return _isOn; }
|
||||
@@ -41,6 +47,10 @@ public:
|
||||
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;
|
||||
uint16_t getTextWidth(const char *str) override;
|
||||
int getCharWidth() const override { return _use_lemon ? 5 : 6; }
|
||||
int getLineHeight() const override { return _use_lemon ? 9 : 8; }
|
||||
void setLemonFont(bool enabled) override { _use_lemon = enabled; }
|
||||
void translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) override;
|
||||
void setBrightness(uint8_t level) override;
|
||||
void endFrame() override;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user