148
148
149
149
class Seg14x4 (HT16K33 ):
150
150
"""Alpha-numeric, 14-segment display."""
151
+ def print (self , value ):
152
+ """Print the value to the display."""
153
+ if isinstance (value , (str )):
154
+ self ._text (value )
155
+ elif isinstance (value , (int , float )):
156
+ self ._number (value )
157
+ else :
158
+ raise ValueError ('Unsupported display value type: {}' .format (type (value )))
159
+
160
+ def __setitem__ (self , key , value ):
161
+ self ._put (value , key )
162
+
151
163
def scroll (self , count = 1 ):
152
164
"""Scroll the display by specified number of places."""
153
165
if count >= 0 :
@@ -157,7 +169,7 @@ def scroll(self, count=1):
157
169
for i in range (6 ):
158
170
self ._set_buffer (i + offset , self ._get_buffer (i + 2 * count ))
159
171
160
- def put (self , char , index = 0 ):
172
+ def _put (self , char , index = 0 ):
161
173
"""Put a character at the specified place."""
162
174
if not 0 <= index <= 3 :
163
175
return
@@ -170,44 +182,42 @@ def put(self, char, index=0):
170
182
self ._set_buffer (index * 2 , CHARS [1 + character ])
171
183
self ._set_buffer (index * 2 + 1 , CHARS [character ])
172
184
173
- def push (self , char ):
185
+ def _push (self , char ):
174
186
"""Scroll the display and add a character at the end."""
175
187
if char != '.' or self ._get_buffer (7 ) & 0b01000000 :
176
188
self .scroll ()
177
- self .put (' ' , 3 )
178
- self .put (char , 3 )
189
+ self ._put (' ' , 3 )
190
+ self ._put (char , 3 )
179
191
180
- def text (self , text ):
192
+ def _text (self , text ):
181
193
"""Display the specified text."""
182
194
for character in text :
183
- self .push (character )
195
+ self ._push (character )
184
196
185
- def number (self , number ):
197
+ def _number (self , number ):
186
198
"""Display the specified decimal number."""
187
- string = "{:f }" .format (number )
199
+ string = "{}" .format (number )
188
200
if len (string ) > 4 :
189
201
if string .find ('.' ) > 4 :
190
202
raise ValueError ("Overflow" )
191
203
self .fill (False )
192
204
places = 4
193
205
if '.' in string :
194
206
places += 1
195
- self .text (string [:places ])
196
-
197
- def hex (self , number ):
198
- """Display the specified hexadecimal number."""
199
- string = "{:x}" .format (number )
200
- if len (string ) > 4 :
201
- raise ValueError ("Overflow" )
202
- self .fill (False )
203
- self .text (string )
204
-
207
+ self ._text (string [:places ])
205
208
206
209
class Seg7x4 (Seg14x4 ):
207
210
"""Numeric 7-segment display. It has the same methods as the alphanumeric display, but only
208
211
supports displaying decimal and hex digits, period and a minus sign."""
209
212
POSITIONS = [0 , 2 , 6 , 8 ] # The positions of characters.
210
213
214
+ def print (self , value ):
215
+ """Print the value to the display."""
216
+ if isinstance (value , (int , float )):
217
+ self ._number (value )
218
+ else :
219
+ raise ValueError ('Unsupported display value type: {}' .format (type (value )))
220
+
211
221
def scroll (self , count = 1 ):
212
222
"""Scroll the display by specified number of places."""
213
223
if count >= 0 :
@@ -218,14 +228,14 @@ def scroll(self, count=1):
218
228
self ._set_buffer (self .POSITIONS [i + offset ],
219
229
self ._get_buffer (self .POSITIONS [i + count ]))
220
230
221
- def push (self , char ):
231
+ def _push (self , char ):
222
232
"""Scroll the display and add a character at the end."""
223
233
if char in ':;' :
224
- self .put (char )
234
+ self ._put (char )
225
235
else :
226
- super ().push (char )
236
+ super ()._push (char )
227
237
228
- def put (self , char , index = 0 ):
238
+ def _put (self , char , index = 0 ):
229
239
"""Put a character at the specified place."""
230
240
if not 0 <= index <= 3 :
231
241
return
0 commit comments