Skip to content

Commit 5c808b0

Browse files
authored
Merge pull request #12 from caternuson/iss3
Updates for segment displays
2 parents f2c2619 + 10e8b74 commit 5c808b0

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

adafruit_ht16k33/segments.py

+32-22
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@
148148

149149
class Seg14x4(HT16K33):
150150
"""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+
151163
def scroll(self, count=1):
152164
"""Scroll the display by specified number of places."""
153165
if count >= 0:
@@ -157,7 +169,7 @@ def scroll(self, count=1):
157169
for i in range(6):
158170
self._set_buffer(i + offset, self._get_buffer(i + 2 * count))
159171

160-
def put(self, char, index=0):
172+
def _put(self, char, index=0):
161173
"""Put a character at the specified place."""
162174
if not 0 <= index <= 3:
163175
return
@@ -170,44 +182,42 @@ def put(self, char, index=0):
170182
self._set_buffer(index * 2, CHARS[1 + character])
171183
self._set_buffer(index * 2 + 1, CHARS[character])
172184

173-
def push(self, char):
185+
def _push(self, char):
174186
"""Scroll the display and add a character at the end."""
175187
if char != '.' or self._get_buffer(7) & 0b01000000:
176188
self.scroll()
177-
self.put(' ', 3)
178-
self.put(char, 3)
189+
self._put(' ', 3)
190+
self._put(char, 3)
179191

180-
def text(self, text):
192+
def _text(self, text):
181193
"""Display the specified text."""
182194
for character in text:
183-
self.push(character)
195+
self._push(character)
184196

185-
def number(self, number):
197+
def _number(self, number):
186198
"""Display the specified decimal number."""
187-
string = "{:f}".format(number)
199+
string = "{}".format(number)
188200
if len(string) > 4:
189201
if string.find('.') > 4:
190202
raise ValueError("Overflow")
191203
self.fill(False)
192204
places = 4
193205
if '.' in string:
194206
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])
205208

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

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+
211221
def scroll(self, count=1):
212222
"""Scroll the display by specified number of places."""
213223
if count >= 0:
@@ -218,14 +228,14 @@ def scroll(self, count=1):
218228
self._set_buffer(self.POSITIONS[i + offset],
219229
self._get_buffer(self.POSITIONS[i + count]))
220230

221-
def push(self, char):
231+
def _push(self, char):
222232
"""Scroll the display and add a character at the end."""
223233
if char in ':;':
224-
self.put(char)
234+
self._put(char)
225235
else:
226-
super().push(char)
236+
super()._push(char)
227237

228-
def put(self, char, index=0):
238+
def _put(self, char, index=0):
229239
"""Put a character at the specified place."""
230240
if not 0 <= index <= 3:
231241
return

examples/segments.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
display.show()
2929

3030
# Set the first character to '1':
31-
display.put('1', 0)
31+
display[0] = '1'
3232
# Set the second character to '2':
33-
display.put('2', 1)
33+
display[1] = '2'
3434
# Set the third character to 'A':
35-
display.put('A', 2)
35+
display[2] = 'A'
3636
# Set the forth character to 'B':
37-
display.put('B', 3)
37+
display[3] = 'B'
3838
# Make sure to call show to see the changes above on the display!
3939
display.show()

0 commit comments

Comments
 (0)