Skip to content

Commit 0f55bfd

Browse files
author
caternuson
committed
add print, privatize old funcs
1 parent f2c2619 commit 0f55bfd

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

adafruit_ht16k33/segments.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@
148148

149149
class Seg14x4(HT16K33):
150150
"""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+
151162
def scroll(self, count=1):
152163
"""Scroll the display by specified number of places."""
153164
if count >= 0:
@@ -157,7 +168,7 @@ def scroll(self, count=1):
157168
for i in range(6):
158169
self._set_buffer(i + offset, self._get_buffer(i + 2 * count))
159170

160-
def put(self, char, index=0):
171+
def _put(self, char, index=0):
161172
"""Put a character at the specified place."""
162173
if not 0 <= index <= 3:
163174
return
@@ -170,44 +181,41 @@ def put(self, char, index=0):
170181
self._set_buffer(index * 2, CHARS[1 + character])
171182
self._set_buffer(index * 2 + 1, CHARS[character])
172183

173-
def push(self, char):
184+
def _push(self, char):
174185
"""Scroll the display and add a character at the end."""
175186
if char != '.' or self._get_buffer(7) & 0b01000000:
176187
self.scroll()
177-
self.put(' ', 3)
178-
self.put(char, 3)
188+
self._put(' ', 3)
189+
self._put(char, 3)
179190

180-
def text(self, text):
191+
def _text(self, text):
181192
"""Display the specified text."""
182193
for character in text:
183-
self.push(character)
194+
self._push(character)
184195

185-
def number(self, number):
196+
def _number(self, number):
186197
"""Display the specified decimal number."""
187-
string = "{:f}".format(number)
198+
string = "{}".format(number)
188199
if len(string) > 4:
189200
if string.find('.') > 4:
190201
raise ValueError("Overflow")
191202
self.fill(False)
192203
places = 4
193204
if '.' in string:
194205
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])
205207

206208
class Seg7x4(Seg14x4):
207209
"""Numeric 7-segment display. It has the same methods as the alphanumeric display, but only
208210
supports displaying decimal and hex digits, period and a minus sign."""
209211
POSITIONS = [0, 2, 6, 8] # The positions of characters.
210212

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+
211219
def scroll(self, count=1):
212220
"""Scroll the display by specified number of places."""
213221
if count >= 0:
@@ -218,14 +226,14 @@ def scroll(self, count=1):
218226
self._set_buffer(self.POSITIONS[i + offset],
219227
self._get_buffer(self.POSITIONS[i + count]))
220228

221-
def push(self, char):
229+
def _push(self, char):
222230
"""Scroll the display and add a character at the end."""
223231
if char in ':;':
224-
self.put(char)
232+
self._put(char)
225233
else:
226-
super().push(char)
234+
super()._push(char)
227235

228-
def put(self, char, index=0):
236+
def _put(self, char, index=0):
229237
"""Put a character at the specified place."""
230238
if not 0 <= index <= 3:
231239
return

0 commit comments

Comments
 (0)