Adafruit Library
WipperSnapper_I2C_Driver_Out_Ssd1306.h
Go to the documentation of this file.
1 
16 #ifndef WIPPERSNAPPER_I2C_DRIVER_OUT_SSD1306_H
17 #define WIPPERSNAPPER_I2C_DRIVER_OUT_SSD1306_H
18 
20 #include <Adafruit_SSD1306.h>
21 #include <Arduino.h>
22 
23 #define WS_SSD1306_DEFAULT_WIDTH \
24  128
25 #define WS_SSD1306_DEFAULT_HEIGHT \
26  64
27 
28 
34 
35 public:
36  /*******************************************************************************/
44  /*******************************************************************************/
45  WipperSnapper_I2C_Driver_Out_Ssd1306(TwoWire *i2c, uint16_t sensorAddress)
46  : WipperSnapper_I2C_Driver_Out(i2c, sensorAddress) {
47  _i2c = i2c;
48  _sensorAddress = sensorAddress;
51  }
52 
57  if (_display != nullptr) {
58  _display->clearDisplay();
59  _display->display();
60  _display->ssd1306_command(SSD1306_DISPLAYOFF);
61  delete _display;
62  _display = nullptr;
63  }
64  }
65 
70  bool begin() {
71  // Attempt to create and allocate a SSD1306 obj.
72  _display = new Adafruit_SSD1306(_width, _height, _i2c);
73  if (!_display->begin(SSD1306_SWITCHCAPVCC, _sensorAddress))
74  return false;
75  // Configure the rotation, text size and color
76  _display->setRotation(_rotation);
77  _display->setTextSize(_text_sz);
78  _display->setTextColor(SSD1306_WHITE);
79  // Use full 256 char 'Code Page 437' font
80  _display->cp437(true);
81  // Clear the buffer
82  _display->clearDisplay();
83  _display->display();
84  return true;
85  }
86 
99  void ConfigureSSD1306(uint8_t width, uint8_t height, uint8_t text_size,
100  uint8_t rotation = 0) {
101  _width = width;
102  _height = height;
103  _text_sz = text_size;
104  _rotation = rotation;
105  }
106 
112  void WriteMessageSSD1306(const char *message) {
113  if (_display == nullptr)
114  return;
115 
116  // Start with a fresh display buffer
117  // and settings
118  int16_t y_idx = 0;
119  _display->clearDisplay();
120  _display->setTextSize(_text_sz);
121  _display->setTextColor(SSD1306_WHITE);
122  _display->setCursor(0, y_idx);
123  _display->display();
124 
125  // Calculate the line height based on the text size (NOTE: base height is
126  // 8px)
127  int16_t line_height = 8 * _text_sz;
128  uint16_t c_idx = 0;
129  size_t msg_size = strlen(message);
130  for (size_t i = 0; i < msg_size && c_idx < msg_size; i++) {
131  if (message[i] == '\\' && i + 1 < msg_size &&
132  (message[i + 1] == 'n' || message[i + 1] == 'r')) {
133  // Handle \r\n sequence as a single newline
134  if (message[i + 1] == 'r' && i + 3 < msg_size &&
135  message[i + 2] == '\\' && message[i + 3] == 'n') {
136  // Skip to the next line
137  y_idx += line_height;
138  _display->setCursor(0, y_idx);
139  i += 3;
140  } else if (message[i + 1] == 'n') {
141  // Skip to the next line
142  y_idx += line_height;
143  _display->setCursor(0, y_idx);
144  i++;
145  }
146  } else if (message[i] == 0xC2 && message[i + 1] == 0xB0) {
147  _display->write(char(248));
148  _display->display();
149  i++;
150  } else {
151  _display->print(message[i]);
152  _display->display();
153  }
154  }
155  }
156 
157 protected:
158  Adafruit_SSD1306 *_display =
159  nullptr;
160  uint8_t _width;
161  uint8_t _height;
162  uint8_t _rotation;
163  uint8_t _text_sz;
164 };
165 
166 #endif // WIPPERSNAPPER_I2C_DRIVER_OUT_SSD1306_H
Class that provides a driver interface for a SSD1306 OLED Display.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:32
~WipperSnapper_I2C_Driver_Out_Ssd1306()
Destructor for a SSD1306 OLED display.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:56
uint8_t _height
Height of the display in pixels.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:161
WipperSnapper_I2C_Driver_Out_Ssd1306(TwoWire *i2c, uint16_t sensorAddress)
Constructor for a SSD1306 OLED display.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:45
Derived class for I2C output component drivers.
Definition: WipperSnapper_I2C_Driver_Out.h:25
#define WS_SSD1306_DEFAULT_HEIGHT
Default height for a ssd1306 128x64 display.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:25
uint16_t _sensorAddress
The I2C driver&#39;s unique I2C address.
Definition: WipperSnapper_I2C_Driver.h:1322
#define WS_SSD1306_DEFAULT_WIDTH
Default width for a ssd1306 128x64 display.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:23
void ConfigureSSD1306(uint8_t width, uint8_t height, uint8_t text_size, uint8_t rotation=0)
Configures a SSD1306 OLED display. Must be called before driver begin()
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:99
uint8_t _text_sz
Text size of the display.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:163
uint8_t _width
Width of the display in pixels.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:160
Adafruit_SSD1306 * _display
Pointer to the Adafruit_SSD1306 object.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:158
uint8_t _rotation
Rotation of the display in degrees.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:162
void WriteMessageSSD1306(const char *message)
Writes a message to the SSD1306 display.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:112
TwoWire * _i2c
Pointer to the I2C driver&#39;s Wire object.
Definition: WipperSnapper_I2C_Driver.h:1321
bool begin()
Initializes the SSD1306 display and begins I2C.
Definition: WipperSnapper_I2C_Driver_Out_Ssd1306.h:70