2020#include < Adafruit_SSD1306.h>
2121#include < Arduino.h>
2222
23- #define DEFAULT_WIDTH 128
24- #define DEFAULT_HEIGHT 64
23+ #define DEFAULT_WIDTH 128 // /< Default width for a ssd1306 128x64 display
24+ #define DEFAULT_HEIGHT 64 // /< Default height for a ssd1306 128x64 display
2525
2626/* !
2727 @brief Class that provides a driver interface for a SSD1306
@@ -63,17 +63,10 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
6363 @returns True if initialized successfully, False otherwise.
6464 */
6565 bool begin () {
66- WS_DEBUG_PRINT (" New SSD1306 with height: " );
67- WS_DEBUG_PRINT (_height);
68- WS_DEBUG_PRINT (" width: " );
69- WS_DEBUG_PRINT (_width);
70- WS_DEBUG_PRINT (" text size: " );
71- WS_DEBUG_PRINT (_text_sz);
66+ // Attempt to create and allocate a SSD1306 obj.
7267 _display = new Adafruit_SSD1306 (_width, _height, _i2c);
73- // TODO: Use _sensorAddress instead of 0x3C hardcode!
74- bool did_begin = _display->begin (SSD1306_SWITCHCAPVCC, 0x3C );
75- if (!did_begin)
76- return false ;
68+ if (!_display->begin (SSD1306_SWITCHCAPVCC, _sensorAddress))
69+ return false ;
7770 // Clear the buffer
7871 _display->clearDisplay ();
7972 // Configure the text size and color
@@ -82,7 +75,7 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
8275 // Reset the cursor position
8376 _display->setCursor (0 , 0 );
8477 _display->display ();
85- return did_begin ;
78+ return true ;
8679 }
8780
8881 /* !
@@ -109,34 +102,31 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
109102 void WriteMessageSSD1306 (const char *message) {
110103 if (_display == nullptr )
111104 return ;
105+
106+ // Start with a fresh display buffer
107+ // and settings
108+ int16_t y_idx = 0 ;
112109 _display->clearDisplay ();
110+ _display->setTextSize (_text_sz);
111+ _display->setTextColor (SSD1306_WHITE);
112+ _display->setCursor (0 , y_idx);
113+ _display->display ();
113114
114115 // Calculate the line height based on the text size (NOTE: base height is
115116 // 8px)
116117 int16_t line_height = 8 * _text_sz;
117- WS_DEBUG_PRINT (" Line height: " );
118- WS_DEBUG_PRINTLN (line_height);
119-
120- int16_t y_idx = 0 ;
121118 uint16_t c_idx = 0 ;
122- _display->setCursor (0 , y_idx);
123- for (int i = 0 ; i < strlen (message); i++) {
124- char c = message[c_idx];
125- if (c == ' \\ ' && message[c_idx + 1 ] == ' n' ) {
126- WS_DEBUG_PRINTLN (" New line detected!" );
127- // Skip the '\n' character in the buffer
128- c_idx += 2 ;
129- // Move the cursor to the next line
119+ size_t msg_size = strlen (message);
120+ for (size_t i = 0 ; i < msg_size && c_idx < msg_size; i++) {
121+ if (message[i] == ' \\ ' && i + 1 < msg_size && message[i + 1 ] == ' n' ) {
122+ // detected a newline char sequence (\n)
123+ i++;
124+ // Skip to the next possible line
130125 y_idx += line_height;
131126 _display->setCursor (0 , y_idx);
132127 } else {
133- WS_DEBUG_PRINT (" Printing char: " );
134- WS_DEBUG_PRINT (message[c_idx]);
135- WS_DEBUG_PRINT (" at y: " );
136- WS_DEBUG_PRINTLN (y_idx);
137- _display->print (message[c_idx]);
128+ _display->print (message[i]);
138129 _display->display ();
139- c_idx++;
140130 }
141131 }
142132 }
0 commit comments