Skip to content

Commit 13d4ca9

Browse files
authored
Merge pull request #759 from adafruit/fix-i2c-display-cr
Patch - \r handling for I2C Displays
2 parents 4494e3e + b9a33a7 commit 13d4ca9

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ extra_scripts = pre:rename_usb_config.py
374374
extends = common:esp32
375375
board = adafruit_qtpy_esp32s3_n4r2
376376
build_flags = -DARDUINO_ADAFRUIT_QTPY_ESP32S3_N4R2 -DBOARD_HAS_PSRAM
377+
board_build.partitions = tinyuf2-partitions-4MB-noota.csv
377378
extra_scripts = pre:rename_usb_config.py
378379

379380
[env:adafruit_qtpy_esp32s3_with_psram_debug]

src/components/i2c/drivers/WipperSnapper_I2C_Driver_Out_CharLcd.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,30 @@ class WipperSnapper_I2C_Driver_Out_CharLcd
117117
cur_col++) {
118118
char c = message[cur_idx];
119119
if (c == '\\' && cur_idx + 1 < message_length &&
120-
message[cur_idx + 1] == 'n') {
121-
cur_idx += 2; // Skip the '\n' character in the buffer
122-
break; // and move to the next row
120+
(message[cur_idx + 1] == 'n' || message[cur_idx + 1] == 'r')) {
121+
// Handle \r\n sequence as a single newline
122+
if (message[cur_idx + 1] == 'r' && cur_idx + 3 < message_length &&
123+
message[cur_idx + 2] == '\\' && message[cur_idx + 3] == 'n') {
124+
cur_idx += 4; // Skip \r\n and don't move the cursor two rows
125+
break; // Move to the next row
126+
} else {
127+
if (message[cur_idx + 1] == 'r') {
128+
_lcd->write('\\');
129+
_lcd->write('r');
130+
cur_idx += 2; // Skip the \r
131+
} else {
132+
cur_idx += 2; // Skip the \n
133+
break; // Move to the next row
134+
}
135+
}
136+
} else if ((c == 0x0A || c == 0x0D) && cur_idx + 1 < message_length) {
137+
if (c == 0x0A && cur_idx + 1 < message_length &&
138+
message[cur_idx + 1] == 0x0D) {
139+
cur_idx += 2; // Skip both LF and CR characters
140+
} else {
141+
cur_idx += 1; // Skip single newline character
142+
}
143+
break; // and move to the next row
123144
} else if (c == 194 && cur_idx + 1 < message_length &&
124145
message[cur_idx + 1] == 176) {
125146
cur_idx += 2; // Skip the degree symbol sequence in the buffer

src/components/i2c/drivers/WipperSnapper_I2C_Driver_Out_Ssd1306.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,21 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
121121
uint16_t c_idx = 0;
122122
size_t msg_size = strlen(message);
123123
for (size_t i = 0; i < msg_size && c_idx < msg_size; i++) {
124-
if (message[i] == '\\' && i + 1 < msg_size && message[i + 1] == 'n') {
125-
// detected a newline char sequence (\n)
126-
i++;
127-
// Skip to the next possible line
128-
y_idx += line_height;
129-
_display->setCursor(0, y_idx);
124+
if (message[i] == '\\' && i + 1 < msg_size &&
125+
(message[i + 1] == 'n' || message[i + 1] == 'r')) {
126+
// Handle \r\n sequence as a single newline
127+
if (message[i + 1] == 'r' && i + 3 < msg_size &&
128+
message[i + 2] == '\\' && message[i + 3] == 'n') {
129+
// Skip to the next line
130+
y_idx += line_height;
131+
_display->setCursor(0, y_idx);
132+
i += 3;
133+
} else if (message[i + 1] == 'n') {
134+
// Skip to the next line
135+
y_idx += line_height;
136+
_display->setCursor(0, y_idx);
137+
i++;
138+
}
130139
} else if (message[i] == 0xC2 && message[i + 1] == 0xB0) {
131140
_display->write(char(248));
132141
_display->display();

0 commit comments

Comments
 (0)