Skip to content

Commit 058e121

Browse files
authored
Merge pull request #43 from makermelissa/master
Implemented set_digit_raw for 7 and 14 segment displays
2 parents 9ec0036 + 89233db commit 058e121

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

adafruit_ht16k33/segments.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ def _number(self, number):
215215
self._text(string[:places])
216216
self._auto_write = auto_write
217217

218+
def set_digit_raw(self, index, bitmask):
219+
"""Set digit at position to raw bitmask value. Position should be a value
220+
of 0 to 3 with 0 being the left most character on the display.
221+
222+
bitmask should be 2 bytes such as: 0xFFFF
223+
If can be passed as an integer, list, or tuple
224+
"""
225+
if not 0 <= index <= 3:
226+
return
227+
228+
if isinstance(bitmask, (tuple, list)):
229+
bitmask = bitmask[0] << 8 | bitmask[1]
230+
231+
# Set the digit bitmask value at the appropriate position.
232+
self._set_buffer(index * 2, (bitmask >> 8) & 0xFF)
233+
self._set_buffer(index * 2 + 1, bitmask & 0xFF)
234+
235+
if self._auto_write:
236+
self.show()
237+
218238
class Seg7x4(Seg14x4):
219239
"""Numeric 7-segment display. It has the same methods as the alphanumeric display, but only
220240
supports displaying a limited set of characters."""
@@ -249,7 +269,7 @@ def _put(self, char, index=0):
249269
if char == '.':
250270
self._set_buffer(index, self._get_buffer(index) | 0b10000000)
251271
return
252-
elif char in 'abcdef':
272+
if char in 'abcdef':
253273
character = ord(char) - 97 + 10
254274
elif char == '-':
255275
character = 16
@@ -268,6 +288,19 @@ def _put(self, char, index=0):
268288
return
269289
self._set_buffer(index, NUMBERS[character])
270290

291+
def set_digit_raw(self, index, bitmask):
292+
"""Set digit at position to raw bitmask value. Position should be a value
293+
of 0 to 3 with 0 being the left most digit on the display.
294+
"""
295+
if not 0 <= index <= 3:
296+
return
297+
298+
# Set the digit bitmask value at the appropriate position.
299+
self._set_buffer(self.POSITIONS[index], bitmask & 0xFF)
300+
301+
if self._auto_write:
302+
self.show()
303+
271304
class BigSeg7x4(Seg7x4):
272305
"""Numeric 7-segment display. It has the same methods as the alphanumeric display, but only
273306
supports displaying a limited set of characters."""

examples/ht16k33_segments_simpletest.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# Import the HT16K33 LED segment module.
1313
from adafruit_ht16k33 import segments
1414

15-
1615
# Create the I2C interface.
1716
i2c = busio.I2C(board.SCL, board.SDA)
1817

@@ -40,3 +39,18 @@
4039
display[2] = 'A'
4140
# Set the forth character to 'B':
4241
display[3] = 'B'
42+
time.sleep(2)
43+
44+
# Or, can even set the segments to make up characters
45+
if isinstance(display, segments.Seg7x4):
46+
# 7-segment raw digits
47+
display.set_digit_raw(0, 0xFF)
48+
display.set_digit_raw(1, 0b11111111)
49+
display.set_digit_raw(2, 0x79)
50+
display.set_digit_raw(3, 0b01111001)
51+
else:
52+
# 14-segment raw digits
53+
display.set_digit_raw(0, 0x3F2D)
54+
display.set_digit_raw(1, 0b0011111100101101)
55+
display.set_digit_raw(2, (0b00111111, 0b00101101))
56+
display.set_digit_raw(3, [0b00111111, 0b00101101])

0 commit comments

Comments
 (0)