1
+ /* !
2
+ * @file drvOutSsd1306.h
3
+ *
4
+ * Device driver for OLED displays with a SSD1306 driver
5
+ *
6
+ * Adafruit invests time and resources providing this open source code,
7
+ * please support Adafruit and open-source hardware by purchasing
8
+ * products from Adafruit!
9
+ *
10
+ * Copyright (c) Brent Rubell for Adafruit Industries 2025
11
+ *
12
+ * MIT license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+
16
+ #ifndef DRV_OUT_SSD1306_H
17
+ #define DRV_OUT_SSD1306_H
18
+
19
+ #include " drvOutputBase.h"
20
+ #include < Adafruit_SSD1306.h>
21
+ #include < Arduino.h>
22
+
23
+ /* !
24
+ @brief Class that provides a driver interface for a SSD1306 OLED display.
25
+ This class is a wrapper around the Adafruit_SSD1306 library.
26
+ */
27
+ class drvOutSsd1306 : public drvOutputBase {
28
+ public:
29
+ /* !
30
+ @brief Constructor for a lcd character display.
31
+ @param i2c
32
+ The I2C interface.
33
+ @param sensorAddress
34
+ 7-bit device address.
35
+ @param mux_channel
36
+ The I2C multiplexer channel.
37
+ @param driver_name
38
+ The name of the driver.
39
+ */
40
+ drvOutSsd1306 (TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
41
+ const char *driver_name)
42
+ : drvOutputBase(i2c, sensorAddress, mux_channel, driver_name) {
43
+ // Initialization handled by drvOutPutBase constructor
44
+ }
45
+
46
+ /* !
47
+ @brief Destructor for a quad alphanumeric display.
48
+ */
49
+ ~drvOutSsd1306 () {
50
+ if (_display != nullptr ) {
51
+ _display->ssd1306_command (SSD1306_DISPLAYOFF);
52
+ delete _display;
53
+ _display = nullptr ;
54
+ }
55
+ }
56
+
57
+ /* !
58
+ @brief Initializes the SSD1306 display and begins I2C.
59
+ @returns True if initialized successfully, False otherwise.
60
+ */
61
+ bool begin () {
62
+ // Attempt to create and allocate a SSD1306 obj.
63
+ _display = new Adafruit_SSD1306 (_width, _height, _i2c);
64
+ if (!_display->begin (SSD1306_SWITCHCAPVCC, _address))
65
+ return false ;
66
+ // Clear the buffer
67
+ _display->clearDisplay ();
68
+ // Configure the text size and color
69
+ _display->setTextSize (_text_sz);
70
+ _display->setTextColor (SSD1306_WHITE);
71
+ // Reset the cursor position
72
+ _display->setCursor (0 , 0 );
73
+ _display->display ();
74
+ return true ;
75
+ }
76
+
77
+ /* !
78
+ @brief Configures a SSD1306 OLED display. Must be called before driver
79
+ begin()
80
+ @param width
81
+ The width of the display in pixels.
82
+ @param height
83
+ The height of the display in pixels.
84
+ @param text_size
85
+ The magnification factor for the text size.
86
+ */
87
+ void ConfigureSSD1306 (uint8_t width, uint8_t height, uint8_t text_size) {
88
+ _width = width;
89
+ _height = height;
90
+ _text_sz = text_size;
91
+ }
92
+
93
+ /* !
94
+ @brief Writes a message to the SSD1306 display.
95
+ @param message
96
+ The message to be displayed.
97
+ */
98
+ void WriteMessageSSD1306 (const char *message) {
99
+ if (_display == nullptr )
100
+ return ;
101
+
102
+ // Start with a fresh display buffer
103
+ // and settings
104
+ int16_t y_idx = 0 ;
105
+ _display->clearDisplay ();
106
+ _display->setTextSize (_text_sz);
107
+ _display->setTextColor (SSD1306_WHITE);
108
+ _display->setCursor (0 , y_idx);
109
+ _display->display ();
110
+
111
+ // Calculate the line height based on the text size (NOTE: base height is
112
+ // 8px)
113
+ int16_t line_height = 8 * _text_sz;
114
+ uint16_t c_idx = 0 ;
115
+ size_t msg_size = strlen (message);
116
+ for (size_t i = 0 ; i < msg_size && c_idx < msg_size; i++) {
117
+ if (message[i] == ' \\ ' && i + 1 < msg_size && message[i + 1 ] == ' n' ) {
118
+ // detected a newline char sequence (\n)
119
+ i++;
120
+ // Skip to the next possible line
121
+ y_idx += line_height;
122
+ _display->setCursor (0 , y_idx);
123
+ } else {
124
+ _display->print (message[i]);
125
+ _display->display ();
126
+ }
127
+ }
128
+ }
129
+
130
+ protected:
131
+ Adafruit_SSD1306 *_display =
132
+ nullptr ; // /< Pointer to the Adafruit_SSD1306 object
133
+ uint8_t _width; // /< Width of the display in pixels
134
+ uint8_t _height; // /< Height of the display in pixels
135
+ uint8_t _text_sz; // /< Text size of the display
136
+ };
137
+
138
+ #endif // DRV_OUT_SSD1306_H
0 commit comments