Skip to content

Make BCDDigits chainable #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions adafruit_max7219/bcddigits.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
_DISPLAYTEST = const(15)


class BCDDigits(max7219.MAX7219):
class BCDDigits(max7219.ChainableMAX7219):
"""
Basic support for display on a 7-Segment BCD display controlled
by a Max7219 chip using SPI.

:param ~busio.SPI spi: an spi busio or spi bitbangio object
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
:param int nDigits: number of led 7-segment digits; default 1; max 8
:param int nDigits: number of led 7-segment digits; default 1; max 8 if one
MAX7219; max 16 if two MAX7219 are chained together
"""

def __init__(self, spi: busio.SPI, cs: digitalio.DigitalInOut, nDigits: int = 1):
Expand All @@ -46,10 +47,10 @@ def init_display(self) -> None:
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, (2**self._ndigits) - 1),
(_SHUTDOWN, 1),
):
self.write_cmd(cmd, data)
self.write_cmd(_DECODEMODE, (2**self._ndigits) - 1, data_overflow=True)
self.write_cmd(_SHUTDOWN, 1)

self.clear_all()
self.show()
Expand All @@ -61,6 +62,15 @@ def set_digit(self, dpos: int, value: int) -> None:
:param int dpos: the digit position; zero-based
:param int value: integer ranging from 0->15
"""

if not 0 <= dpos < self.chain_length * 8:
raise ValueError(
"Digit with index {0} cannot be set with only {1} MAX7219(s)".format(
dpos,
self.chain_length,
)
)

dpos = self._ndigits - dpos - 1
for i in range(4):
# print('digit {} pixel {} value {}'.format(dpos,i+4,v & 0x01))
Expand Down
13 changes: 10 additions & 3 deletions adafruit_max7219/max7219.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,25 @@ def __init__(
self._buffer = bytearray(self.chain_length * 8)
self.framebuf = framebuf.FrameBuffer1(self._buffer, self.chain_length * 8, 8)

def write_cmd(self, cmd: int, data: int) -> None:
# pylint: disable=arguments-differ
def write_cmd(self, cmd: int, data: int, *, data_overflow: bool = False) -> None:
"""
Writes a command to spi device.

:param int cmd: register address to write data to
:param int data: data to be written to commanded register
:param bool data_overflow: if data is larger than byte, write byte
by byte in big-endian order
"""
# print('cmd {} data {}'.format(cmd,data))
self._chip_select.value = False
with self._spi_device as my_spi_device:
for _ in range(self.chain_length):
my_spi_device.write(bytearray([cmd, data]))
for byte_num in range(1, self.chain_length + 1):
byte_data = data
if data_overflow:
byte_data = data >> 8 * (self.chain_length - byte_num)
byte_data &= 0xFF
my_spi_device.write(bytearray([cmd, byte_data]))

def show(self) -> None:
"""
Expand Down