Skip to content

Commit 4df009e

Browse files
committed
Fix charLCD formatting, decimal on quadalphanum
1 parent f8aa819 commit 4df009e

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/components/i2c/drivers/drvOutCharLcd.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ class drvOutCharLcd : public drvOutputBase {
111111
for (int cur_col = 0; cur_col < _cols && cur_idx < message_length;
112112
cur_col++) {
113113
char c = message[cur_idx];
114-
if (c == '\\' && message[cur_idx + 1] == 'n') {
114+
if (c == '\\' && cur_idx + 1 < message_length &&
115+
message[cur_idx + 1] == 'n') {
115116
cur_idx += 2; // Skip the '\n' character in the buffer
116117
break; // and move to the next row
117-
} else if (c == 194 && message[cur_idx + 1] == 176) {
118+
} else if (c == 194 && cur_idx + 1 < message_length &&
119+
message[cur_idx + 1] == 176) {
118120
cur_idx += 2; // Skip the degree symbol sequence in the buffer
119121
_lcd->write(0xDF); // and write the degree symbol
120122
} else {

src/components/i2c/drivers/drvOutQuadAlphaNum.h

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ class drvOutQuadAlphaNum : public drvOutputBase {
103103
}
104104

105105
/*!
106-
@brief Writes the first four characters of a message to the quad
107-
alphanumeric display.
106+
@brief Writes a message to the quad alphanumeric display.
108107
@param message
109108
The message to be displayed.
110109
*/
@@ -115,45 +114,52 @@ class drvOutQuadAlphaNum : public drvOutputBase {
115114
// Clear before writing
116115
_alpha4->clear();
117116

118-
// Calculate the number of characters to display
119-
size_t len_display = min(strlen(message), (size_t)LED_MAX_CHARS);
117+
// Calculate the total message length
118+
size_t msg_len = strlen(message);
119+
120+
// Count all characters but excluding decimal points as they are part of the
121+
// "segment"
122+
int char_count = 0;
123+
for (size_t i = 0; i < msg_len && char_count < LED_MAX_CHARS; i++) {
124+
if (message[i] != '.') {
125+
char_count++;
126+
}
127+
}
120128

121129
// Set the starting position based on alignment
122130
int pos_start;
123131
if (_alignment == LED_BACKPACK_ALIGNMENT_LEFT) {
124-
pos_start = 0; // start at the leftmost position of the display
132+
pos_start = 0; // start at the leftmost position
125133
} else {
126-
// Exclude decimal points from the character count because those get
127-
// displayed on a "special" segment of the LED display
128-
int seg_chars = 0;
129-
for (size_t i = 0; i < len_display; i++) {
130-
if (message[i] != '.') {
131-
seg_chars++;
132-
}
133-
}
134-
// start at the rightmost position of the display
135-
pos_start = LED_MAX_CHARS - seg_chars;
134+
pos_start = LED_MAX_CHARS - char_count; // start at the rightmost position
136135
}
137136

138137
// Write to the display's buffer
139138
int cur_idx = pos_start;
140-
for (size_t i = 0; i < len_display; i++) {
141-
// Save the character because if there's a decimal, we need to skip it in
142-
// the buffer
139+
for (size_t i = 0; i < msg_len && cur_idx < LED_MAX_CHARS; i++) {
143140
char ch = message[i];
144141

145-
// Look-ahead for a decimal point to attach to the current character
146-
bool display_dot = false;
147-
if (i + 1 < len_display && message[i + 1] == '.') {
148-
display_dot = true;
149-
i++;
150-
len_display++;
142+
if (ch == '.') {
143+
// If the previous character has already been processed (or we're at the
144+
// start of the message), we need to show the decimal as a character
145+
if (i == 0 || message[i - 1] == '.') {
146+
_alpha4->writeDigitAscii(cur_idx, ch, false);
147+
cur_idx++;
148+
} else {
149+
// Set decimal on an already written character
150+
_alpha4->writeDigitAscii(cur_idx - 1, message[i - 1], true);
151+
}
152+
continue;
151153
}
152154

155+
// Perform a look-ahead for the decimal
156+
bool display_dot = (i + 1 < msg_len && message[i + 1] == '.');
157+
153158
// Write the character to the display buffer
154159
_alpha4->writeDigitAscii(cur_idx, ch, display_dot);
155160
cur_idx++;
156161
}
162+
157163
// Issue the buffered data in RAM to the display
158164
_alpha4->writeDisplay();
159165
}
@@ -166,7 +172,7 @@ class drvOutQuadAlphaNum : public drvOutputBase {
166172
*/
167173
void WriteValue(float value) {
168174
char message[8 + 1];
169-
snprintf(message, sizeof(message), "%.5f", value);
175+
snprintf(message, sizeof(message), "%.3f", value);
170176
WriteMessage(message);
171177
}
172178

0 commit comments

Comments
 (0)