Skip to content

Commit 952c334

Browse files
committed
improve the text wrapping speed by avoiding re-doing work
1 parent e1bcf07 commit 952c334

File tree

1 file changed

+47
-32
lines changed
  • CircuitPython_GetSuperpower_PicoW_OpenAI

1 file changed

+47
-32
lines changed

CircuitPython_GetSuperpower_PicoW_OpenAI/code.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
#
4848
# Invent an alien animal or plant, name it, and vividly describe it in 1
4949
# sentence
50+
#
51+
# Write 1 setence starting "you can" about an unconventional but useful superpower
5052
prompt=os.getenv("MY_PROMPT", """
51-
Write 1 setence starting "you can" about an unconventional but useful superpower
53+
Invent and vividly describe an alien species. write one paragraph
5254
""").strip()
5355
please_wait=os.getenv("PLEASE_WAIT", """
5456
Finding superpower
@@ -57,7 +59,7 @@
5759
openai_api_key = os.getenv("OPENAI_API_KEY")
5860

5961
nice_font = load_font("helvR08.pcf")
60-
line_spacing = 0.68
62+
line_spacing = 9 # in pixels
6163

6264
# i2c display setup
6365
displayio.release_displays()
@@ -69,43 +71,49 @@
6971

7072
WIDTH = 128
7173
HEIGHT = 64
72-
offset_y = 5
7374

7475
display = adafruit_displayio_ssd1306.SSD1306(
7576
display_bus, width=WIDTH, height=HEIGHT
7677
)
7778
if openai_api_key is None:
7879
input("Place your\nOPENAI_API_KEY\nin settings.toml")
7980
display.auto_refresh = False
80-
main_group = displayio.Group()
81-
display.root_group = main_group
82-
83-
terminal = Label(
84-
font=nice_font,
85-
color=0xFFFFFF,
86-
background_color=0,
87-
line_spacing=line_spacing,
88-
anchor_point=(0, 0),
89-
anchored_position=(0, 0),
90-
)
91-
max_lines = display.height // int(nice_font.get_bounding_box()[1] * terminal.line_spacing)
92-
main_group.append(terminal)
9381

94-
class WrappedTextDisplay:
82+
class WrappedTextDisplay(displayio.Group):
9583
def __init__(self):
96-
self.line_offset = 0
97-
self.lines = []
84+
super().__init__()
85+
self.offset = 0
86+
self.max_lines = display.height // line_spacing
87+
for i in range(self.max_lines):
88+
self.make_label("", i * line_spacing)
89+
self.lines = [""]
9890
self.text = ""
9991

92+
def make_label(self, text, y):
93+
result = Label(
94+
font=nice_font,
95+
color=0xFFFFFF,
96+
background_color=None,
97+
line_spacing=line_spacing,
98+
anchor_point=(0, 0),
99+
anchored_position=(0, y),
100+
text=text)
101+
self.append(result)
102+
100103
def add_text(self, new_text):
101-
self.set_text(self.text + new_text)
104+
print(end=new_text)
105+
if self.lines:
106+
text = self.lines[-1] + new_text
107+
else:
108+
text = new_text
109+
self.lines[-1:] = wrap_text_to_pixels(text, display.width, nice_font)
102110
self.scroll_to_end()
103111

104112
def set_text(self, text):
105113
print("\033[H\033[2J", end=text)
106114
self.text = text
107115
self.lines = wrap_text_to_pixels(text, display.width, nice_font)
108-
self.line_offset = 0
116+
self.offset = 0
109117

110118
def show(self, text):
111119
self.set_text(text)
@@ -116,25 +124,34 @@ def add_show(self, new_text):
116124
self.refresh()
117125

118126
def scroll_to_end(self):
119-
self.line_offset = self.max_offset()
127+
self.offset = self.max_offset()
120128

121129
def scroll_next_line(self):
122130
max_offset = self.max_offset()
123-
if max_offset > 0:
124-
line_offset = self.line_offset + 1
125-
self.line_offset = line_offset % (max_offset + 1)
131+
self.offset = (self.offset + 1) % (max_offset + 1)
126132

127133
def max_offset(self):
128-
return max(0, len(self.lines) - max_lines)
134+
return max(0, len(self.lines) - self.max_lines)
129135

130136
def on_last_line(self):
131-
return self.line_offset == self.max_offset()
137+
return self.offset == self.max_offset()
132138

133139
def refresh(self):
134-
text = '\n'.join(self.lines[self.line_offset : self.line_offset + max_lines])
135-
terminal.text = text
140+
lines = self.lines
141+
# update labels from wrapped text, accounting for scroll offset
142+
for i in range(len(self)):
143+
line = i + self.offset
144+
if line >= len(lines):
145+
content = ""
146+
else:
147+
content = lines[line]
148+
if content != self[i].text:
149+
self[i].text = content
150+
151+
# Actually update the display all at once
136152
display.refresh()
137-
wrapped_text = WrappedTextDisplay()
153+
154+
display.root_group = wrapped_text = WrappedTextDisplay()
138155

139156
def wait_button_scroll_text():
140157
led.switch_to_output(True)
@@ -189,8 +206,6 @@ def iter_lines(resp):
189206
else:
190207
wrapped_text.show("")
191208
for line in iter_lines(response):
192-
# print(line)
193-
# continue
194209
if line.startswith("data: [DONE]"):
195210
break
196211
if line.startswith("data:"):

0 commit comments

Comments
 (0)