47
47
#
48
48
# Invent an alien animal or plant, name it, and vividly describe it in 1
49
49
# sentence
50
+ #
51
+ # Write 1 setence starting "you can" about an unconventional but useful superpower
50
52
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
52
54
""" ).strip ()
53
55
please_wait = os .getenv ("PLEASE_WAIT" , """
54
56
Finding superpower
57
59
openai_api_key = os .getenv ("OPENAI_API_KEY" )
58
60
59
61
nice_font = load_font ("helvR08.pcf" )
60
- line_spacing = 0.68
62
+ line_spacing = 9 # in pixels
61
63
62
64
# i2c display setup
63
65
displayio .release_displays ()
69
71
70
72
WIDTH = 128
71
73
HEIGHT = 64
72
- offset_y = 5
73
74
74
75
display = adafruit_displayio_ssd1306 .SSD1306 (
75
76
display_bus , width = WIDTH , height = HEIGHT
76
77
)
77
78
if openai_api_key is None :
78
79
input ("Place your\n OPENAI_API_KEY\n in settings.toml" )
79
80
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 )
93
81
94
- class WrappedTextDisplay :
82
+ class WrappedTextDisplay ( displayio . Group ) :
95
83
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 = ["" ]
98
90
self .text = ""
99
91
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
+
100
103
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 )
102
110
self .scroll_to_end ()
103
111
104
112
def set_text (self , text ):
105
113
print ("\033 [H\033 [2J" , end = text )
106
114
self .text = text
107
115
self .lines = wrap_text_to_pixels (text , display .width , nice_font )
108
- self .line_offset = 0
116
+ self .offset = 0
109
117
110
118
def show (self , text ):
111
119
self .set_text (text )
@@ -116,25 +124,34 @@ def add_show(self, new_text):
116
124
self .refresh ()
117
125
118
126
def scroll_to_end (self ):
119
- self .line_offset = self .max_offset ()
127
+ self .offset = self .max_offset ()
120
128
121
129
def scroll_next_line (self ):
122
130
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 )
126
132
127
133
def max_offset (self ):
128
- return max (0 , len (self .lines ) - max_lines )
134
+ return max (0 , len (self .lines ) - self . max_lines )
129
135
130
136
def on_last_line (self ):
131
- return self .line_offset == self .max_offset ()
137
+ return self .offset == self .max_offset ()
132
138
133
139
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
136
152
display .refresh ()
137
- wrapped_text = WrappedTextDisplay ()
153
+
154
+ display .root_group = wrapped_text = WrappedTextDisplay ()
138
155
139
156
def wait_button_scroll_text ():
140
157
led .switch_to_output (True )
@@ -189,8 +206,6 @@ def iter_lines(resp):
189
206
else :
190
207
wrapped_text .show ("" )
191
208
for line in iter_lines (response ):
192
- # print(line)
193
- # continue
194
209
if line .startswith ("data: [DONE]" ):
195
210
break
196
211
if line .startswith ("data:" ):
0 commit comments