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