Skip to content

Commit 18bc2e0

Browse files
committed
Fixed problem where newline happens after line is full
1 parent a3e6c77 commit 18bc2e0

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lcd/lcd_api.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __init__(self, num_lines, num_columns):
5656
self.num_columns = 40
5757
self.cursor_x = 0
5858
self.cursor_y = 0
59+
self.implied_newline = False
5960
self.backlight = True
6061
self.display_off()
6162
self.backlight_on()
@@ -135,15 +136,23 @@ def putchar(self, char):
135136
"""Writes the indicated character to the LCD at the current cursor
136137
position, and advances the cursor by one position.
137138
"""
138-
if char != '\n':
139+
if char == '\n':
140+
if self.implied_newline:
141+
# self.implied_newline means we advanced due to a wraparound,
142+
# so if we get a newline right after that we ignore it.
143+
pass
144+
else:
145+
self.cursor_x = self.num_columns
146+
else:
139147
self.hal_write_data(ord(char))
140148
self.cursor_x += 1
141-
if self.cursor_x >= self.num_columns or char == '\n':
149+
if self.cursor_x >= self.num_columns:
142150
self.cursor_x = 0
143151
self.cursor_y += 1
144-
if self.cursor_y >= self.num_lines:
145-
self.cursor_y = 0
146-
self.move_to(self.cursor_x, self.cursor_y)
152+
self.implied_newline = (char != '\n')
153+
if self.cursor_y >= self.num_lines:
154+
self.cursor_y = 0
155+
self.move_to(self.cursor_x, self.cursor_y)
147156

148157
def putstr(self, string):
149158
"""Write the indicated string to the LCD at the current cursor

0 commit comments

Comments
 (0)