Detect SSD1306 and SH1106 screens on RAK boards

Use repeated status reads at screen initialization and preserve SSD1306 for ambiguous responses. Share the existing framebuffer, drawing, rotation and power handling; use SH1106 page transfers when detected.

Enable this for RAK4631 and RAK3401, including WisMesh Pocket v2/v3. Add controller overrides, protocol tests and CI coverage. Addresses meshcore-dev/MeshCore#3296.
This commit is contained in:
mikecarper committed 2026-10-20 09:43:25 -07:00
1 parent f57dffa2be
commit d5a6df6283
8 files changed
+729

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
#pragma once
#include <Adafruit_SSD1306.h>
#include <stdlib.h>
#include "OLEDDisplayProbe.h"
// Keep Adafruit's drawing, rotation and single 1 KiB framebuffer. SH1106 uses
// page writes into 132-column RAM, with the visible 128 columns starting at 2.
class AutoOLEDBackend : public Adafruit_SSD1306 {
mesh::ui::OLEDController _controller = mesh::ui::OLEDController::Absent;
bool _ready = false;
bool writeCommands(const uint8_t* commands, uint8_t count) {
wire->beginTransmission(i2caddr);
const bool control = wire->write(uint8_t(0x00)) == 1;
const bool payload = wire->write(commands, count) == count;
return wire->endTransmission() == 0 && control && payload;
}
public:
using Adafruit_SSD1306::Adafruit_SSD1306;
bool begin(uint8_t vcc, uint8_t address, bool reset, bool periphBegin) {
_ready = false;
i2caddr = address;
if (periphBegin) wire->begin();
if (reset && rstPin >= 0) {
pinMode(rstPin, OUTPUT);
digitalWrite(rstPin, HIGH);
delay(1);
digitalWrite(rstPin, LOW);
delay(10);
digitalWrite(rstPin, HIGH);
}
#if defined(OLED_CONTROLLER_OVERRIDE) && OLED_CONTROLLER_OVERRIDE != 0
static_assert(OLED_CONTROLLER_OVERRIDE == 1 || OLED_CONTROLLER_OVERRIDE == 2,
"OLED_CONTROLLER_OVERRIDE must be 0 (auto), 1 (SSD1306) or 2 (SH1106)");
wire->beginTransmission(address);
_controller = wire->endTransmission() != 0 ? mesh::ui::OLEDController::Absent
: OLED_CONTROLLER_OVERRIDE == 2 ? mesh::ui::OLEDController::SH1106
: mesh::ui::OLEDController::SSD1306;
#else
_controller = mesh::ui::detectOLEDController(*wire, address);
#endif
if (_controller == mesh::ui::OLEDController::Absent) return false;
if (_controller == mesh::ui::OLEDController::SSD1306) {
_ready = Adafruit_SSD1306::begin(vcc, address, false, false);
return _ready;
}
if (!buffer && !(buffer = static_cast<uint8_t*>(malloc(WIDTH * ((HEIGHT + 7) / 8)))))
return false;
vccstate = vcc;
clearDisplay();
// SH1106G initialization, matching Adafruit_SH1106G::begin().
static const uint8_t init[] = {
0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40,
0xAD, 0x8B, 0xA1, 0xC8, 0xDA, 0x12, 0x81, 0xFF,
0xD9, 0x1F, 0xDB, 0x40, 0x33, 0xA6, 0x20, 0x10, 0xA4
};
wire->setClock(wireClk);
const bool initialized = writeCommands(init, sizeof(init));
wire->setClock(restoreClk);
if (!initialized) return false;
delay(100);
const uint8_t on = 0xAF;
wire->setClock(wireClk);
_ready = writeCommands(&on, 1);
wire->setClock(restoreClk);
return _ready;
}
mesh::ui::OLEDController controller() const { return _controller; }
void ssd1306_command(uint8_t command) {
if (_ready) Adafruit_SSD1306::ssd1306_command(command);
}
void display() {
if (!_ready) return;
if (_controller == mesh::ui::OLEDController::SSD1306) {
Adafruit_SSD1306::display();
return;
}
wire->setClock(wireClk);
bool ok = true;
for (uint8_t page = 0; ok && page < (HEIGHT + 7) / 8; ++page) {
const uint8_t commands[] = {uint8_t(0xB0 | page), 0x02, 0x10};
ok = writeCommands(commands, sizeof(commands));
for (uint16_t column = 0; ok && column < WIDTH;) {
const uint8_t count = WIDTH - column > 31 ? 31 : WIDTH - column;
wire->beginTransmission(i2caddr);
const bool control = wire->write(uint8_t(0x40)) == 1;
const bool payload = wire->write(buffer + page * WIDTH + column, count) == count;
ok = wire->endTransmission() == 0 && control && payload;
column += count;
}
}
wire->setClock(restoreClk);
}
};
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <stdint.h>
namespace mesh { namespace ui {
enum class OLEDController : uint8_t { Absent, SSD1306, SH1106 };
// Status-byte detection is used by ss_oled and Meshtastic. Only 0x08 is
// evidence of SH1106: some RAK1921 SSD1306 panels return the ambiguous 0x00.
// See https://github.com/meshtastic/firmware/pull/11265.
template <typename WireBus>
OLEDController detectOLEDController(WireBus& wire, uint8_t address) {
wire.beginTransmission(address);
if (wire.endTransmission() != 0) return OLEDController::Absent;
int previous = -1;
for (unsigned attempt = 0; attempt < 4; ++attempt) {
wire.beginTransmission(address);
const bool written = wire.write(uint8_t(0x00)) == 1;
if (wire.endTransmission() != 0 || !written) break;
if (wire.requestFrom(address, uint8_t(1)) != 1 || wire.available() != 1) break;
const int status = wire.read();
if (status < 0) break;
const int nibble = status & 0x0f;
if (nibble == previous)
return nibble == 0x08 ? OLEDController::SH1106 : OLEDController::SSD1306;
previous = nibble;
}
return OLEDController::SSD1306;
}
}} // namespace mesh::ui
+7
View File
@@ -6,6 +6,9 @@
#define SSD1306_NO_SPLASH
#include <Adafruit_SSD1306.h>
#include <helpers/RefCountedDigitalPin.h>
#if defined(OLED_AUTO_DETECT) && OLED_AUTO_DETECT
#include "AutoOLEDBackend.h"
#endif
#ifndef PIN_OLED_RESET
#define PIN_OLED_RESET 21 // Reset pin # (or -1 if sharing Arduino reset pin)
@@ -21,7 +24,11 @@ public:
static constexpr int16_t PANEL_HEIGHT = 64;
static constexpr size_t FRAMEBUFFER_BYTES = PANEL_WIDTH * ((PANEL_HEIGHT + 7) / 8);
private:
#if defined(OLED_AUTO_DETECT) && OLED_AUTO_DETECT
AutoOLEDBackend display;
#else
Adafruit_SSD1306 display;
#endif
bool _isOn;
bool _panel_ready = false;
bool _flipped = false;