Add a V4 Picopixel message font trial with longer previews

This commit is contained in:
mikecarper committed 2026-09-08 13:22:53 -07:00
1 parent d93bbe8d69
commit 9f3b48725d
15 files changed
+334

No files matched your search

@@ -0,0 +1 @@
#pragma once
@@ -0,0 +1 @@
#pragma once
@@ -0,0 +1,32 @@
#pragma once
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <vector>
#define SSD1306_BLACK 0
#define SSD1306_WHITE 1
#define SSD1306_SWITCHCAPVCC 2
#define SSD1306_DISPLAYON 0xAF
#define SSD1306_DISPLAYOFF 0xAE
class Adafruit_SSD1306 : public GFXcanvas1 {
public:
struct Glyph { char c; int x, y, w, h; };
std::vector<Glyph> glyphs;
inline static Adafruit_SSD1306* last;
int outside = 0;
Adafruit_SSD1306(int w, int h, TwoWire*, int) : GFXcanvas1(w, h) { last=this; }
bool begin(int, int, bool, bool) { return true; }
void clearDisplay() { fillScreen(0); glyphs.clear(); outside=0; }
void display() {}
void ssd1306_command(int) {}
void drawPixel(int16_t x, int16_t y, uint16_t c) override {
if (x<0 || y<0 || x>=width() || y>=height()) ++outside;
GFXcanvas1::drawPixel(x,y,c);
}
size_t write(uint8_t c) override {
if (gfxFont && c>=gfxFont->first && c<=gfxFont->last) {
const auto& g=gfxFont->glyph[c-gfxFont->first];
glyphs.push_back({char(c), cursor_x+g.xOffset, cursor_y+g.yOffset, g.width, g.height});
}
return Adafruit_GFX::write(c);
}
};
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#define PROGMEM
#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
#define HIGH 1
#define LOW 0
#define OUTPUT 1
using String = std::string;
class __FlashStringHelper;
inline void pinMode(int, int) {}
inline void digitalWrite(int, int) {}
using std::min;
using std::max;
inline float radians(float deg) { return deg * 0.017453292519943295f; }
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include <cstddef>
#include <cstdint>
class Print {
public:
virtual ~Print() = default;
virtual size_t write(uint8_t) = 0;
size_t print(const char* s) { size_t n=0; while (*s) n += write(*s++); return n; }
};
+4
View File
@@ -0,0 +1,4 @@
#pragma once
#include <Arduino.h>
class TwoWire { public: void beginTransmission(int) {} int endTransmission() { return 0; } };
inline TwoWire Wire;
+70
View File
@@ -0,0 +1,70 @@
#include <helpers/ui/SSD1306Display.h>
#include <cassert>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
void save(const char* file) {
auto& c=*Adafruit_SSD1306::last;
std::ofstream f(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'; }
}
int main(int argc, char** argv) {
SSD1306Display 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";
d.startFrame();
d.setCursor(0,0); d.print("Message 3/29");
d.setCursor(86,0); d.print("1m ago");
d.drawRect(0,11,128,1);
d.setCursor(0,14); d.print("Ch 0 Public [4h]:");
d.setCursor(0,25);
#if UI_SSD1306_PICOPIXEL_MESSAGES == 1
d.printWordWrap(message,128);
#else
d.print(std::string(message,77).c_str());
#endif
if (argc > 1) save(argv[1]);
#if UI_SSD1306_PICOPIXEL_MESSAGES == 1
std::string shown;
for (const auto& g:c.glyphs) {
shown += g.c;
if (g.w && g.h) assert(g.x>=0 && g.x+g.w<=128 && g.y>=25 && g.y+g.h<=64);
}
assert(shown==message);
assert(c.outside==0);
assert(d.getTextWidth("ABC")==18); // regular font restored
d.setCursor(0,0); d.print("Regular font");
assert(d.getTextWidth("ABC")==18);
int checks=1;
for (int rotation:{0,90,180,270}) for (int top:{0,25,55,58,59,63,64}) {
d.setRotationDegrees(rotation);
d.startFrame(); d.setCursor(0,top);
d.printWordWrap(std::string(160,'W').c_str(),d.width());
for (const auto& g:c.glyphs) {
if (g.w && g.h) assert(g.x>=0 && g.x+g.w<=d.width() && g.y>=top && g.y+g.h<=d.height());
}
if (!c.glyphs.empty()) {
std::string row;
for (const auto& g:c.glyphs) row += g.c;
while (!row.empty() && row.back()==' ') row.pop_back();
assert(row==std::string(160, 'W') || (row.size()>=3 && row.substr(row.size()-3)=="..."));
}
assert(c.outside==0);
++checks;
}
d.setRotationDegrees(0);
for (int ch=32;ch<=126;ch++) {
d.startFrame(); d.setCursor(0,25);
d.printWordWrap(std::string(160,char(ch)).c_str(),128);
assert(c.outside==0);
++checks;
}
d.startFrame(); d.setCursor(0,25);
d.printWordWrap("A\xDB" "B",128);
assert(c.glyphs.size()==3 && c.glyphs[1].c=='?');
std::cout << "Checks: " << checks+1 << "; sample characters: " << shown.size() << "; sample: " << shown << '\n';
#endif
}
+15
View File
@@ -66,6 +66,21 @@ def esp_fixture(path, modern=False, fragmented=False):
class FirmwareRamTest(unittest.TestCase):
def test_longer_display_previews_reserve_heap_and_contiguous_history(self):
defines = {"DISPLAY_CLASS": "SSD1306Display"}
base = ram.requirements("ESP32_PLATFORM", defines, "v4_companion")
expanded = ram.requirements("ESP32_PLATFORM", {
**defines, "UI_MSG_PREVIEW_SIZE": 161,
}, "v4_companion")
self.assertEqual(expanded["required_heap_bytes"],
base["required_heap_bytes"] + 2816)
self.assertGreaterEqual(expanded["required_contiguous_bytes"], 11008)
# Shrinking the preview cannot reduce the existing safety budget.
smaller = ram.requirements("ESP32_PLATFORM", {
**defines, "UI_MSG_PREVIEW_SIZE": 32,
}, "v4_companion")
self.assertEqual(smaller, base)
def test_published_image_tables_match_elf_and_use_its_own_reservations(self):
with tempfile.TemporaryDirectory() as temp:
path = Path(temp) / "reference.elf"
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""Render the real SSD1306 driver with Adafruit GFX and a host panel double.
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; font metrics and drawing come
from the library used by the firmware.
"""
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = ROOT / "test/fixtures/ssd1306_picopixel"
class SSD1306PicopixelTest(unittest.TestCase):
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(
(ROOT / ".pio/libdeps").glob("*/Adafruit GFX Library")
)
library = next((path for path in candidates
if (path / "Adafruit_GFX.cpp").is_file()), None)
if library is None:
self.skipTest("Build a V4 environment or set MESHCORE_GFX_LIBRARY")
with tempfile.TemporaryDirectory(prefix="meshcore-picopixel-") as temp:
for enabled in (0, 1):
with self.subTest(picopixel=enabled):
binary = Path(temp) / f"render-{enabled}"
result = subprocess.run([
"c++", "-std=c++17", "-g",
"-fsanitize=address,undefined", "-DARDUINO=10819",
f"-DUI_SSD1306_PICOPIXEL_MESSAGES={enabled}",
"-I", str(FIXTURE / "mocks"),
"-I", str(ROOT / "src"), "-I", str(library),
str(ROOT / "src/helpers/ui/SSD1306Display.cpp"),
str(library / "Adafruit_GFX.cpp"),
str(FIXTURE / "render.cpp"), "-o", str(binary),
], text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
result = subprocess.run(
[str(binary)], text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
if __name__ == "__main__":
unittest.main()