20
20
#include < Adafruit_SSD1306.h>
21
21
#include < Arduino.h>
22
22
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
25
25
26
26
/* !
27
27
@brief Class that provides a driver interface for a SSD1306
@@ -63,17 +63,10 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
63
63
@returns True if initialized successfully, False otherwise.
64
64
*/
65
65
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.
72
67
_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 ;
77
70
// Clear the buffer
78
71
_display->clearDisplay ();
79
72
// Configure the text size and color
@@ -82,7 +75,7 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
82
75
// Reset the cursor position
83
76
_display->setCursor (0 , 0 );
84
77
_display->display ();
85
- return did_begin ;
78
+ return true ;
86
79
}
87
80
88
81
/* !
@@ -109,34 +102,31 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
109
102
void WriteMessageSSD1306 (const char *message) {
110
103
if (_display == nullptr )
111
104
return ;
105
+
106
+ // Start with a fresh display buffer
107
+ // and settings
108
+ int16_t y_idx = 0 ;
112
109
_display->clearDisplay ();
110
+ _display->setTextSize (_text_sz);
111
+ _display->setTextColor (SSD1306_WHITE);
112
+ _display->setCursor (0 , y_idx);
113
+ _display->display ();
113
114
114
115
// Calculate the line height based on the text size (NOTE: base height is
115
116
// 8px)
116
117
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 ;
121
118
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
130
125
y_idx += line_height;
131
126
_display->setCursor (0 , y_idx);
132
127
} 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]);
138
129
_display->display ();
139
- c_idx++;
140
130
}
141
131
}
142
132
}
0 commit comments