Skip to content

Commit 47afeb7

Browse files
committed
Works on both display sizes
1 parent a86ead8 commit 47afeb7

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,6 @@ bool WipperSnapper_Component_I2C::Handle_I2cDeviceOutputWrite(
11931193
driver_out->WriteMessageCharLCD(&msgDeviceWrite->output_msg.write_char_lcd);
11941194
} else if (msgDeviceWrite->which_output_msg ==
11951195
wippersnapper_i2c_v1_I2CDeviceOutputWrite_write_ssd1306_tag) {
1196-
WS_DEBUG_PRINTLN("Calling WriteMessageSSD1306..");
11971196
driver_out->WriteMessageSSD1306(
11981197
msgDeviceWrite->output_msg.write_ssd1306.message);
11991198
} else {

src/components/i2c/drivers/WipperSnapper_I2C_Driver_Out_Ssd1306.h

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
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

Comments
 (0)