Use readable 6px message font and keep 5px on tiny panels

This commit is contained in:
mikecarper committed 2026-09-08 14:23:39 -07:00
1 parent 5fe6f299f3
commit 272dc9aa4b
14 files changed
+1550 -93

No files matched your search

File diff suppressed because it is too large. Load diff
+23 -4
View File
@@ -1,5 +1,5 @@
#include <helpers/ui/SSD1306Display.h>
#include <helpers/ui/Pixel5Text.h>
#include <helpers/ui/SmallMessageText.h>
#include <Fonts/Picopixel.h>
#include <cassert>
#include <fstream>
@@ -12,8 +12,13 @@ void save(const char* file) {
f << "P1\n128 64\n";
for (int y=0;y<64;y++) { for (int x=0;x<128;x++) f << c.getPixel(x,y) << ' '; f << '\n'; }
}
class TestSSD1306Display : public SSD1306Display {
public:
void setCanvasSize(int w, int h) { setDimensions(w, h); }
};
int main(int argc, char** argv) {
SSD1306Display d;
TestSSD1306Display d;
assert(d.begin());
auto& c=*Adafruit_SSD1306::last;
const char* message="GIANT KILLER: Out on the Mercerwood mesh today, just the V4 and a small battery Checking the smaller font so the rest of this message is visible. 0123456789 END";
@@ -32,7 +37,21 @@ int main(int argc, char** argv) {
#if UI_SMALL_MESSAGE_FONT == 1
assert(c.outside==0);
assert(d.getTextWidth("ABC")==18); // regular driver font untouched
mesh::ui::Pixel5Text text(d);
mesh::ui::SmallMessageText six(d);
assert(six.capitalHeight()==6);
for (int ch=32;ch<=126;ch++) {
d.startFrame(); six.setCursor(0,0);
const std::string glyph(1,char(ch));
six.print(glyph.c_str());
// Compared with the independent upstream BDF in the Python harness.
std::cout << "six " << ch << ' ' << six.getTextWidth(glyph.c_str()) << ' ';
for (int y=0;y<8;y++) for (int x=0;x<8;x++)
std::cout << int(c.getPixel(x,y));
std::cout << '\n';
}
d.setCanvasSize(72,40);
mesh::ui::SmallMessageText text(d);
assert(text.capitalHeight()==5);
GFXcanvas1 reference(128,64);
reference.setFont(&Picopixel);
reference.setTextColor(1);
@@ -44,6 +63,6 @@ int main(int argc, char** argv) {
assert(c.getPixel(x,y)==reference.getPixel(x,y));
assert(text.getTextWidth(std::string(1,char(ch)).c_str())==PicopixelGlyphs[ch-32].xAdvance);
}
std::cout << "95 glyphs match Adafruit GFX Picopixel pixel for pixel; normal font preserved\n";
std::cout << "95 tiny-screen glyphs match Adafruit GFX Picopixel; normal font preserved\n";
#endif
}
+105 -17
View File
@@ -3,7 +3,7 @@
#include <helpers/ui/DisplayDriver.h>
#include <helpers/ui/CompanionHomeLayout.h>
#include <helpers/ui/DisplayTextLayout.h>
#include <helpers/ui/Pixel5Text.h>
#include <helpers/ui/SmallMessageText.h>
#include <helpers/ui/WiFiSetupQrDisplay.h>
#include <helpers/ui/WiFiSetupQrPayload.h>
@@ -134,9 +134,9 @@ TEST(DisplayDriver, EllipsizesOnlyAtUTF8CodepointBoundaries) {
EXPECT_TRUE(isValidUTF8(display.printed.c_str()));
}
TEST(Pixel5Text, CapitalsAreFivePixelsAndNormalFontIsUntouched) {
TestDisplay display(128, 64, 6);
mesh::ui::Pixel5Text text(display);
TEST(SmallMessageText, TinyPanelsKeepFivePixelCapitalsAndNormalFont) {
TestDisplay display(72, 40, 6);
mesh::ui::SmallMessageText text(display);
text.setCursor(0, 0);
text.print("A");
const char* rows[] = {"010", "101", "111", "101", "101"};
@@ -152,7 +152,41 @@ TEST(Pixel5Text, CapitalsAreFivePixelsAndNormalFontIsUntouched) {
EXPECT_EQ(18, display.getTextWidth("ABC"));
}
TEST(Pixel5Text, CompactOriginAllowsSixCompleteMessageRows) {
TEST(SmallMessageText, LargerPanelsUseSixPixelCapitalsAndNormalFont) {
TestDisplay display(128, 64, 6);
mesh::ui::SmallMessageText text(display);
text.setCursor(0, 0);
text.print("A");
const char* rows[] = {"010", "101", "111", "101", "101", "101"};
bool pixels[6][3] = {};
for (const auto& pixel : display.fills) {
ASSERT_GE(pixel.x, 0); ASSERT_LT(pixel.x, 3);
ASSERT_GE(pixel.y, 0); ASSERT_LT(pixel.y, 6);
pixels[pixel.y][pixel.x] = true;
}
for (int y = 0; y < 6; ++y) for (int x = 0; x < 3; ++x)
EXPECT_EQ(rows[y][x] == '1', pixels[y][x]);
EXPECT_EQ(11, text.getTextWidth("ABC"));
EXPECT_EQ(18, display.getTextWidth("ABC"));
}
TEST(SmallMessageText, FontChoiceAndLineSpacingFollowPanelGeometry) {
const int cases[][3] = {
{128, 64, 6}, {64, 128, 6}, {160, 80, 6},
{72, 40, 5}, {40, 72, 5}, {128, 32, 5}, {32, 128, 5},
{64, 48, 5}, {48, 64, 5}};
for (const auto& dimensions : cases) {
TestDisplay display(dimensions[0], dimensions[1]);
mesh::ui::SmallMessageText text(display);
EXPECT_EQ(dimensions[2], text.capitalHeight());
EXPECT_EQ(dimensions[2] + 1, text.glyphHeight());
EXPECT_EQ(dimensions[2] + 2, text.lineHeight());
EXPECT_EQ(0, text.lineCount(display.height() - text.glyphHeight() + 1));
EXPECT_EQ(1, text.lineCount(display.height() - text.glyphHeight()));
}
}
TEST(SmallMessageText, CompactOriginAllowsFiveCompleteSixPixelMessageRows) {
TestDisplay display(128, 64);
mesh::ui::drawSmallMessageBody(display, std::string(61, 'W').c_str(),
std::string(160, 'W').c_str());
@@ -162,33 +196,87 @@ TEST(Pixel5Text, CompactOriginAllowsSixCompleteMessageRows) {
EXPECT_GE(pixel.x, 0); EXPECT_LT(pixel.x, 128);
EXPECT_GE(pixel.y, 14); EXPECT_LT(pixel.y, 64);
// The origin stays in its own line; its long name cannot cover the body.
EXPECT_TRUE(pixel.y < 20 || pixel.y >= 21);
EXPECT_TRUE(pixel.y < 21 || pixel.y >= 22);
for (int i = 0; i < 3; ++i)
if (pixel.y == 60 && pixel.x == 120 + 2 * i) final_dots[i] = true;
if (pixel.y == 59 && pixel.x == 120 + 2 * i) final_dots[i] = true;
}
for (bool dot : final_dots) EXPECT_TRUE(dot);
EXPECT_EQ(5, mesh::ui::smallMessageLineCount(64, 25));
EXPECT_EQ(6, mesh::ui::smallMessageLineCount(64, 21));
EXPECT_EQ(3, mesh::ui::smallMessageLineCount(40, 17));
EXPECT_EQ(0, mesh::ui::smallMessageLineCount(64, 59));
mesh::ui::SmallMessageText text(display);
EXPECT_EQ(5, text.lineCount(22));
EXPECT_EQ(4, text.lineCount(22, 56));
EXPECT_EQ(5, text.lineCount(22, 80)); // never extend past the panel
EXPECT_EQ(0, text.lineCount(22, 20));
}
TEST(Pixel5Text, GlyphsStayInsideTinyAndRotatedScreens) {
for (auto dimensions : {std::pair<int, int>{72, 40}, {64, 128}, {128, 64}}) {
TEST(SmallMessageText, TinyInterfaceKeepsThreeCompleteFivePixelRows) {
TestDisplay display(72, 40);
mesh::ui::SmallMessageText text(display);
EXPECT_EQ(3, text.lineCount(17));
mesh::ui::drawSmallMessageBody(display, std::string(61, 'W').c_str(),
std::string(160, 'W').c_str(), 10);
bool final_dots[3] = {};
for (const auto& pixel : display.fills) {
EXPECT_GE(pixel.x, 0); EXPECT_LT(pixel.x, 72);
EXPECT_GE(pixel.y, 10); EXPECT_LT(pixel.y, 40);
EXPECT_TRUE(pixel.y < 16 || pixel.y >= 17);
for (int i = 0; i < 3; ++i)
if (pixel.y == 35 && pixel.x == 60 + 2 * i) final_dots[i] = true;
}
for (bool dot : final_dots) EXPECT_TRUE(dot);
}
TEST(SmallMessageText, MessageBodyRespectsReservedFooter) {
TestDisplay display(128, 64);
mesh::ui::drawSmallMessageBody(display, "Ch 0 Public [4h]:",
std::string(160, 'W').c_str(), 14, 56);
ASSERT_FALSE(display.fills.empty());
for (const auto& pixel : display.fills) EXPECT_LT(pixel.y, 56);
}
TEST(SmallMessageText, Full160CharacterSamplesFitInFiveRows) {
class RecordingText : public mesh::ui::SmallMessageText {
public:
std::string drawn;
explicit RecordingText(DisplayDriver& display) : SmallMessageText(display) {}
void print(const char* str) override {
drawn += str;
SmallMessageText::print(str);
}
};
const char* samples[] = {
"GIANT KILLER: Out on the Mercerwood mesh today, just the V4 and a small "
"battery Checking the smaller font so the rest of this message is visible. "
"0123456789 END",
"NimBLE-XIAO-Trial: Picopixel: 5 pixel letters and a compact channel line. "
"More of this message now fits on the small screen. "
"0123456789 0123456789 012345678 END"};
for (const char* sample : samples) {
ASSERT_EQ(160U, strlen(sample));
TestDisplay display(128, 64);
RecordingText text(display);
EXPECT_EQ(5, mesh::ui::drawTextWrapped(text, 0, 22, 128,
text.lineHeight(), text.lineCount(22), sample));
EXPECT_EQ(sample, text.drawn);
}
}
TEST(SmallMessageText, GlyphsStayInsideTinyAndRotatedScreens) {
for (auto dimensions : {std::pair<int, int>{72, 40}, {40, 72},
{128, 32}, {32, 128}, {64, 128}, {128, 64}}) {
TestDisplay display(dimensions.first, dimensions.second);
mesh::ui::Pixel5Text text(display);
mesh::ui::SmallMessageText text(display);
for (int c = 32; c <= 126; ++c) {
display.fills.clear();
text.setCursor(0, display.height() - 6);
text.setCursor(0, display.height() - text.glyphHeight());
text.printWordWrap(std::string(160, char(c)).c_str(), display.width());
for (const auto& pixel : display.fills) {
EXPECT_GE(pixel.x, 0); EXPECT_LT(pixel.x, display.width());
EXPECT_GE(pixel.y, display.height() - 6);
EXPECT_GE(pixel.y, display.height() - text.glyphHeight());
EXPECT_LT(pixel.y, display.height());
}
}
display.fills.clear();
text.setCursor(0, display.height() - 5);
text.setCursor(0, display.height() - text.glyphHeight() + 1);
text.printWordWrap("must not start a clipped final line", display.width());
EXPECT_TRUE(display.fills.empty());
}
+28 -3
View File
@@ -3,11 +3,13 @@
Build a V4 environment first to install Adafruit GFX, or point
MESHCORE_GFX_LIBRARY to that library's directory. Only the hardware panel,
Arduino strings and Print glue are replaced; the shared pixel renderer is compared with the original Adafruit font.
Arduino strings and Print glue are replaced. Picopixel is compared with the
original Adafruit font; Squeezed Regular 6 with its upstream BDF glyphs.
"""
import os
from pathlib import Path
import re
import subprocess
import tempfile
import unittest
@@ -16,7 +18,28 @@ ROOT = Path(__file__).resolve().parents[1]
FIXTURE = ROOT / "test/fixtures/ssd1306_picopixel"
class SSD1306PicopixelTest(unittest.TestCase):
class SSD1306SmallMessageFontTest(unittest.TestCase):
def assert_squeezed_glyphs_match_upstream(self, output):
rendered = {}
for line in output.splitlines():
if line.startswith("six "):
_, code, advance, pixels = line.split()
rendered[int(code)] = (int(advance), pixels)
self.assertEqual(set(rendered), set(range(32, 127)))
source = (ROOT / "test/fixtures/small_message_font/squeezed6.bdf").read_text()
for entry in source.split("STARTCHAR ")[1:]:
code = int(re.search(r"^ENCODING (\d+)", entry, re.M)[1])
advance = int(re.search(r"^DWIDTH (\d+)", entry, re.M)[1])
w, h, x, y = map(int, re.search(r"^BBX (.+)$", entry, re.M)[1].split())
rows = entry.split("BITMAP\n", 1)[1].split("ENDCHAR", 1)[0].splitlines()
expected = ["0"] * 64
for row, bits in enumerate(rows):
for col in range(w):
if int(bits, 16) & (1 << (len(bits) * 4 - 1 - col)):
expected[(6 - h - y + row) * 8 + x + col] = "1"
with self.subTest(glyph=chr(code)):
self.assertEqual(rendered[code], (advance, "".join(expected)))
def test_actual_font_bounds_wrapping_and_default_font_restoration(self):
configured = os.environ.get("MESHCORE_GFX_LIBRARY")
candidates = [Path(configured)] if configured else sorted(
@@ -29,7 +52,7 @@ class SSD1306PicopixelTest(unittest.TestCase):
with tempfile.TemporaryDirectory(prefix="meshcore-picopixel-") as temp:
for enabled in (0, 1):
with self.subTest(picopixel=enabled):
with self.subTest(small_message_font=enabled):
binary = Path(temp) / f"render-{enabled}"
result = subprocess.run([
"c++", "-std=c++17", "-g",
@@ -45,6 +68,8 @@ class SSD1306PicopixelTest(unittest.TestCase):
result = subprocess.run(
[str(binary)], text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
if enabled:
self.assert_squeezed_glyphs_match_upstream(result.stdout)
if __name__ == "__main__":