Adafruit Library
WipperSnapper_I2C_Driver_Out_Sh1107.h
Go to the documentation of this file.
1 
16 #ifndef WIPPERSNAPPER_I2C_DRIVER_OUT_SH1107_H
17 #define WIPPERSNAPPER_I2C_DRIVER_OUT_SH1107_H
18 
20 #include <Adafruit_GFX.h>
21 #include <Adafruit_SH110X.h>
22 #include <Arduino.h>
23 
24 #define WS_SH1107_DEFAULT_WIDTH \
25  128
26 #define WS_SH1107_DEFAULT_HEIGHT \
27  64
28 
29 #define OLED_128X64_WING_WIDTH 128
30 #define OLED_128X64_WING_HEIGHT 64
31 #define OLED_128X64_WING_ROTATION_90 1
32 
33 
39 
40 public:
41  /*******************************************************************************/
49  /*******************************************************************************/
50  WipperSnapper_I2C_Driver_Out_SH1107(TwoWire *i2c, uint16_t sensorAddress)
51  : WipperSnapper_I2C_Driver_Out(i2c, sensorAddress) {
52  _i2c = i2c;
53  _sensorAddress = sensorAddress;
56  }
57 
62  if (_display != nullptr) {
63  _display->clearDisplay();
64  _display->display();
65  _display->oled_command(SH110X_DISPLAYOFF);
66  delete _display;
67  _display = nullptr;
68  }
69  }
70 
75  bool begin() {
79  // featherwing needs to be rotated 90 degrees and swap w/h ctor args
80  _display = new Adafruit_SH1107(_height, _width, _i2c);
81  } else {
82  _display = new Adafruit_SH1107(_width, _height, _i2c);
83  }
84  if (!_display->begin(_sensorAddress, true))
85  return false;
86 
87  // Clear the buffer.
88  _display->clearDisplay();
89  _display->display();
90  _display->setRotation(_rotation); // 0-3, not degrees for SH1107
91 
92  // Configure the text size and color
93  _display->setTextSize(_text_sz);
94  _display->setTextColor(SH110X_WHITE);
95  _display->setCursor(0, 0);
96  // Clear the buffer
97  _display->clearDisplay();
98  _display->display();
99  return true;
100  }
101 
114  void ConfigureSH1107(uint8_t width, uint8_t height, uint8_t text_size,
115  uint8_t rotation) {
116  _width = width;
117  _height = height;
118  _text_sz = text_size;
119  _rotation =
120  rotation % 90; // SH1107 requires rotation to be 0-3, not degrees
121  }
134  void ConfigureSSD1306(uint8_t width, uint8_t height, uint8_t text_size,
135  uint8_t rotation) {
136  // This is a SH1107, not a SSD1306, so we don't need to do anything here.
137  ConfigureSH1107(width, height, text_size, rotation);
138  }
139 
145  void WriteMessageSH1107(const char *message) {
146  if (_display == nullptr)
147  return;
148  // Start with a fresh display buffer
149  // and settings
150  int16_t y_idx = 0;
151  _display->clearDisplay();
152  _display->setTextSize(_text_sz);
153  _display->setTextColor(SH110X_WHITE);
154  _display->setCursor(0, y_idx);
155  _display->display();
156 
157  // Calculate the line height based on the text size (NOTE: base height is
158  // 8px)
159  int16_t line_height = 8 * _text_sz;
160  uint16_t c_idx = 0;
161  size_t msg_size = strlen(message);
162  for (size_t i = 0; i < msg_size && c_idx < msg_size; i++) {
163  if (message[i] == '\\' && i + 1 < msg_size &&
164  (message[i + 1] == 'n' || message[i + 1] == 'r')) {
165  // Handle \r\n sequence as a single newline
166  if (message[i + 1] == 'r' && i + 3 < msg_size &&
167  message[i + 2] == '\\' && message[i + 3] == 'n') {
168  // Skip to the next line
169  y_idx += line_height;
170  _display->setCursor(0, y_idx);
171  i += 3;
172  } else if (message[i + 1] == 'n') {
173  // Skip to the next line
174  y_idx += line_height;
175  _display->setCursor(0, y_idx);
176  i++;
177  }
178  } else if (message[i] == 0xC2 && message[i + 1] == 0xB0) {
179  _display->write(char(248));
180  _display->display();
181  i++;
182  } else {
183  _display->print(message[i]);
184  _display->display();
185  }
186  }
187  }
188 
194  void WriteMessageSSD1306(const char *message) {
195  // This is a SH1107, not a SSD1306, so we just call the SH1107 write
196  WriteMessageSH1107(message);
197  }
198 
199 protected:
200  Adafruit_SH1107 *_display =
201  nullptr;
202  uint8_t _width;
203  uint8_t _height;
204  uint8_t _rotation;
205  uint8_t _text_sz;
206 };
207 
208 #endif // WIPPERSNAPPER_I2C_DRIVER_OUT_SH1107_H
void ConfigureSSD1306(uint8_t width, uint8_t height, uint8_t text_size, uint8_t rotation)
Configures a SSD1306 OLED display. Must be called before driver begin() - This is a fake function to ...
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:134
WipperSnapper_I2C_Driver_Out_SH1107(TwoWire *i2c, uint16_t sensorAddress)
Constructor for an SH1107 OLED display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:50
uint8_t _text_sz
Text size of the display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:205
uint8_t _width
Width of the display in pixels.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:202
uint8_t _rotation
Rotation of the display (0-3)
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:204
uint8_t _height
Height of the display in pixels.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:203
Derived class for I2C output component drivers.
Definition: WipperSnapper_I2C_Driver_Out.h:25
Adafruit_SH1107 * _display
Pointer to the Adafruit_SH1107 object.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:200
~WipperSnapper_I2C_Driver_Out_SH1107()
Destructor for a SH1107 OLED display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:61
uint16_t _sensorAddress
The I2C driver&#39;s unique I2C address.
Definition: WipperSnapper_I2C_Driver.h:1322
#define OLED_128X64_WING_HEIGHT
Height of the 128x64 OLED FeatherWing.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:30
bool begin()
Initializes the SH1107 display and begins I2C.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:75
#define OLED_128X64_WING_ROTATION_90
Rotation of OLED FeatherWing 0-3.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:31
#define WS_SH1107_DEFAULT_WIDTH
Default width for a sh1107 128x64 display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:24
Class that provides a driver interface for a SH1107 OLED Display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:37
#define WS_SH1107_DEFAULT_HEIGHT
Default height for a sh1107 128x64 display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:26
#define OLED_128X64_WING_WIDTH
Width of the 128x64 OLED FeatherWing.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:29
void WriteMessageSSD1306(const char *message)
Writes a message to the fake "SSD1306" SH1107 display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:194
void WriteMessageSH1107(const char *message)
Writes a message to the SH1107 display.
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:145
TwoWire * _i2c
Pointer to the I2C driver&#39;s Wire object.
Definition: WipperSnapper_I2C_Driver.h:1321
void ConfigureSH1107(uint8_t width, uint8_t height, uint8_t text_size, uint8_t rotation)
Configures a SH1107 OLED display. Must be called before driver begin()
Definition: WipperSnapper_I2C_Driver_Out_Sh1107.h:114