2025-05-16 08:45:55 -07:00
|
|
|
#include "SH1106Display.h"
|
|
|
|
|
#include <Adafruit_GrayOLED.h>
|
|
|
|
|
#include "Adafruit_SH110X.h"
|
2026-05-20 09:29:38 +02:00
|
|
|
#include "LemonFont.h"
|
|
|
|
|
#include "LemonIcons.h"
|
2025-05-16 08:45:55 -07:00
|
|
|
|
|
|
|
|
bool SH1106Display::i2c_probe(TwoWire &wire, uint8_t addr)
|
|
|
|
|
{
|
|
|
|
|
wire.beginTransmission(addr);
|
|
|
|
|
uint8_t error = wire.endTransmission();
|
|
|
|
|
return (error == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SH1106Display::begin()
|
|
|
|
|
{
|
|
|
|
|
return display.begin(DISPLAY_ADDRESS, true) && i2c_probe(Wire, DISPLAY_ADDRESS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::turnOn()
|
|
|
|
|
{
|
|
|
|
|
display.oled_command(SH110X_DISPLAYON);
|
2026-05-12 08:19:27 +02:00
|
|
|
uint8_t pre[] = { 0xD9, _precharge };
|
|
|
|
|
display.oled_commandList(pre, 2);
|
|
|
|
|
display.setContrast(_contrast);
|
2025-05-16 08:45:55 -07:00
|
|
|
_isOn = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::turnOff()
|
|
|
|
|
{
|
|
|
|
|
display.oled_command(SH110X_DISPLAYOFF);
|
|
|
|
|
_isOn = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::clear()
|
|
|
|
|
{
|
|
|
|
|
display.clearDisplay();
|
|
|
|
|
display.display();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::startFrame(Color bkg)
|
|
|
|
|
{
|
|
|
|
|
display.clearDisplay(); // TODO: apply 'bkg'
|
|
|
|
|
_color = SH110X_WHITE;
|
|
|
|
|
display.setTextColor(_color);
|
|
|
|
|
display.setTextSize(1);
|
2026-05-20 10:32:26 +02:00
|
|
|
_text_sz = 1;
|
2025-05-16 08:45:55 -07:00
|
|
|
display.cp437(true); // Use full 256 char 'Code Page 437' font
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::setTextSize(int sz)
|
|
|
|
|
{
|
2026-05-20 10:32:26 +02:00
|
|
|
_text_sz = sz;
|
refactor(ui): apply code review fixes — bugs, risk, optimisations, cleanup
Fixes 7 bugs (timing UB on unsigned long, abs() no-op, XBM CRC PROGMEM
scan, CayenneLPP heap thrash ×2, UTF-8 reply prefix, lock-seq reset on
wake), 3 risk items (notif melody stop guard, bot trigger pre-lowercase,
lock-seq cleared on display wake), plus optimisations (fmtMsgAge/
getTextWidth caching, RTTTL builder consolidated to NodePrefs, HP_*
constants and homePageLabel table moved to NodePrefs, DataStore flat
lambda schema, translateUTF8Static factored out) and minor cleanup.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-23 20:03:45 +02:00
|
|
|
_vw_dirty = true;
|
2025-05-16 08:45:55 -07:00
|
|
|
display.setTextSize(sz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::setColor(Color c)
|
|
|
|
|
{
|
|
|
|
|
_color = (c != 0) ? SH110X_WHITE : SH110X_BLACK;
|
|
|
|
|
display.setTextColor(_color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::setCursor(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
display.setCursor(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 09:29:38 +02:00
|
|
|
int16_t SH1106Display::drawLemonChar(int16_t x, int16_t y, uint32_t cp) {
|
2026-05-20 10:32:26 +02:00
|
|
|
int sz = _text_sz;
|
2026-05-20 09:29:38 +02:00
|
|
|
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; }
|
2026-05-20 10:32:26 +02:00
|
|
|
if (bits & bit) {
|
|
|
|
|
if (sz == 1) display.drawPixel(x + xo + col, y + 6 + yo + row, _color);
|
|
|
|
|
else display.fillRect(x + xo*sz + col*sz, y + 6*sz + yo*sz + row*sz, sz, sz, _color);
|
|
|
|
|
}
|
2026-05-20 09:29:38 +02:00
|
|
|
bit >>= 1;
|
|
|
|
|
}
|
2026-05-20 10:32:26 +02:00
|
|
|
return x + xa * sz;
|
2026-05-20 09:29:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cp < Lemon.first || cp > Lemon.last) {
|
2026-05-20 10:32:26 +02:00
|
|
|
if (cp >= 0x20) display.fillRect(x + sz, y - sz, 4*sz, 6*sz, _color);
|
|
|
|
|
return x + 6 * sz;
|
2026-05-20 09:29:38 +02:00
|
|
|
}
|
|
|
|
|
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; }
|
2026-05-20 10:32:26 +02:00
|
|
|
if (bits & bit) {
|
|
|
|
|
if (sz == 1) display.drawPixel(x + xo + col, y + 6 + yo + row, _color);
|
|
|
|
|
else display.fillRect(x + xo*sz + col*sz, y + 6*sz + yo*sz + row*sz, sz, sz, _color);
|
|
|
|
|
}
|
2026-05-20 09:29:38 +02:00
|
|
|
bit >>= 1;
|
|
|
|
|
}
|
2026-05-20 10:32:26 +02:00
|
|
|
return x + xa * sz;
|
2026-05-20 09:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t SH1106Display::lemonXAdvance(uint32_t cp) {
|
2026-05-20 10:32:26 +02:00
|
|
|
uint8_t xa;
|
|
|
|
|
if (cp < Lemon.first || cp > Lemon.last) xa = 6;
|
|
|
|
|
else xa = pgm_read_byte(&lemonGlyphs[cp - Lemon.first].xAdvance);
|
|
|
|
|
return xa * _text_sz;
|
2026-05-20 09:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 08:45:55 -07:00
|
|
|
void SH1106Display::print(const char *str)
|
|
|
|
|
{
|
2026-05-20 09:29:38 +02:00
|
|
|
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) {
|
2026-05-24 20:25:35 +02:00
|
|
|
uint32_t cp = decodeCodepoint(p);
|
2026-05-20 09:29:38 +02:00
|
|
|
if (cp == '\n') { cy += Lemon.yAdvance; cx = 0; }
|
|
|
|
|
else { cx = drawLemonChar(cx, cy, cp); }
|
|
|
|
|
}
|
|
|
|
|
display.setCursor(cx, cy);
|
2025-05-16 08:45:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::fillRect(int x, int y, int w, int h)
|
|
|
|
|
{
|
|
|
|
|
display.fillRect(x, y, w, h, _color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::drawRect(int x, int y, int w, int h)
|
|
|
|
|
{
|
|
|
|
|
display.drawRect(x, y, w, h, _color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SH1106Display::drawXbm(int x, int y, const uint8_t *bits, int w, int h)
|
|
|
|
|
{
|
|
|
|
|
display.drawBitmap(x, y, bits, w, h, SH110X_WHITE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t SH1106Display::getTextWidth(const char *str)
|
|
|
|
|
{
|
2026-05-20 09:29:38 +02:00
|
|
|
if (_use_lemon) {
|
|
|
|
|
uint16_t width = 0;
|
|
|
|
|
const uint8_t* p = (const uint8_t*)str;
|
2026-05-24 20:25:35 +02:00
|
|
|
while (*p) width += lemonXAdvance(decodeCodepoint(p));
|
2026-05-20 09:29:38 +02:00
|
|
|
return width;
|
|
|
|
|
}
|
2025-05-16 08:45:55 -07:00
|
|
|
int16_t x1, y1;
|
|
|
|
|
uint16_t w, h;
|
|
|
|
|
display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
|
|
|
|
|
return w;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 15:18:35 +02:00
|
|
|
void SH1106Display::setBrightness(uint8_t level)
|
|
|
|
|
{
|
2026-05-12 08:19:27 +02:00
|
|
|
// Contrast alone has limited effect on some OLED panels; combining with
|
|
|
|
|
// pre-charge period (0xD9) gives a wider perceptible dimming range.
|
|
|
|
|
// Pre-charge 0x11 = phase1=1,phase2=1 (minimum drive); 0x1F = default.
|
|
|
|
|
static const uint8_t contrast_values[] = { 0, 25, 60, 150, 255 };
|
|
|
|
|
static const uint8_t precharge_values[] = { 0x11, 0x15, 0x1F, 0x1F, 0x1F };
|
|
|
|
|
uint8_t idx = level < 5 ? level : 4;
|
|
|
|
|
_contrast = contrast_values[idx];
|
|
|
|
|
_precharge = precharge_values[idx];
|
|
|
|
|
uint8_t pre[] = { 0xD9, _precharge };
|
|
|
|
|
display.oled_commandList(pre, 2);
|
|
|
|
|
display.setContrast(_contrast);
|
2026-05-10 15:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-16 08:45:55 -07:00
|
|
|
void SH1106Display::endFrame()
|
|
|
|
|
{
|
|
|
|
|
display.display();
|
|
|
|
|
}
|