Skip to content

Commit 885cfe9

Browse files
authored
Merge pull request #294 from 446564/SH1106-display
Sh1106 display
2 parents d13ff7e + 25b534a commit 885cfe9

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

examples/companion_radio/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
#include <helpers/ui/ST7735Display.h>
6767
#elif ST7789
6868
#include <helpers/ui/ST7789Display.h>
69+
#elif SH1106
70+
#include <helpers/ui/SH1106Display.h>
6971
#elif defined(HAS_GxEPD)
7072
#include <helpers/ui/GxEPDDisplay.h>
7173
#else

src/helpers/ui/SH1106Display.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "SH1106Display.h"
2+
#include <Adafruit_GrayOLED.h>
3+
#include "Adafruit_SH110X.h"
4+
5+
bool SH1106Display::i2c_probe(TwoWire &wire, uint8_t addr)
6+
{
7+
wire.beginTransmission(addr);
8+
uint8_t error = wire.endTransmission();
9+
return (error == 0);
10+
}
11+
12+
bool SH1106Display::begin()
13+
{
14+
return display.begin(DISPLAY_ADDRESS, true) && i2c_probe(Wire, DISPLAY_ADDRESS);
15+
}
16+
17+
void SH1106Display::turnOn()
18+
{
19+
display.oled_command(SH110X_DISPLAYON);
20+
_isOn = true;
21+
}
22+
23+
void SH1106Display::turnOff()
24+
{
25+
display.oled_command(SH110X_DISPLAYOFF);
26+
_isOn = false;
27+
}
28+
29+
void SH1106Display::clear()
30+
{
31+
display.clearDisplay();
32+
display.display();
33+
}
34+
35+
void SH1106Display::startFrame(Color bkg)
36+
{
37+
display.clearDisplay(); // TODO: apply 'bkg'
38+
_color = SH110X_WHITE;
39+
display.setTextColor(_color);
40+
display.setTextSize(1);
41+
display.cp437(true); // Use full 256 char 'Code Page 437' font
42+
}
43+
44+
void SH1106Display::setTextSize(int sz)
45+
{
46+
display.setTextSize(sz);
47+
}
48+
49+
void SH1106Display::setColor(Color c)
50+
{
51+
_color = (c != 0) ? SH110X_WHITE : SH110X_BLACK;
52+
display.setTextColor(_color);
53+
}
54+
55+
void SH1106Display::setCursor(int x, int y)
56+
{
57+
display.setCursor(x, y);
58+
}
59+
60+
void SH1106Display::print(const char *str)
61+
{
62+
display.print(str);
63+
}
64+
65+
void SH1106Display::fillRect(int x, int y, int w, int h)
66+
{
67+
display.fillRect(x, y, w, h, _color);
68+
}
69+
70+
void SH1106Display::drawRect(int x, int y, int w, int h)
71+
{
72+
display.drawRect(x, y, w, h, _color);
73+
}
74+
75+
void SH1106Display::drawXbm(int x, int y, const uint8_t *bits, int w, int h)
76+
{
77+
display.drawBitmap(x, y, bits, w, h, SH110X_WHITE);
78+
}
79+
80+
uint16_t SH1106Display::getTextWidth(const char *str)
81+
{
82+
int16_t x1, y1;
83+
uint16_t w, h;
84+
display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
85+
return w;
86+
}
87+
88+
void SH1106Display::endFrame()
89+
{
90+
display.display();
91+
}

src/helpers/ui/SH1106Display.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include "DisplayDriver.h"
4+
#include <Wire.h>
5+
#include <Adafruit_GFX.h>
6+
#define SH110X_NO_SPLASH
7+
#include <Adafruit_SH110X.h>
8+
9+
#ifndef PIN_OLED_RESET
10+
#define PIN_OLED_RESET -1
11+
#endif
12+
13+
#ifndef DISPLAY_ADDRESS
14+
#define DISPLAY_ADDRESS 0x3C
15+
#endif
16+
17+
class SH1106Display : public DisplayDriver
18+
{
19+
Adafruit_SH1106G display;
20+
bool _isOn;
21+
uint8_t _color;
22+
23+
bool i2c_probe(TwoWire &wire, uint8_t addr);
24+
25+
public:
26+
SH1106Display() : DisplayDriver(128, 64), display(128, 64, &Wire, PIN_OLED_RESET) { _isOn = false; }
27+
bool begin();
28+
29+
bool isOn() override { return _isOn; }
30+
void turnOn() override;
31+
void turnOff() override;
32+
void clear() override;
33+
void startFrame(Color bkg = DARK) override;
34+
void setTextSize(int sz) override;
35+
void setColor(Color c) override;
36+
void setCursor(int x, int y) override;
37+
void print(const char *str) override;
38+
void fillRect(int x, int y, int w, int h) override;
39+
void drawRect(int x, int y, int w, int h) override;
40+
void drawXbm(int x, int y, const uint8_t *bits, int w, int h) override;
41+
uint16_t getTextWidth(const char *str) override;
42+
void endFrame() override;
43+
};

0 commit comments

Comments
 (0)