From 54bcd3ea9e631062cb06ae6d4a130f19a7232048 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 30 Mar 2022 10:58:38 -0400 Subject: [PATCH 1/5] Max BCDDigits chainable --- adafruit_max7219/bcddigits.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/adafruit_max7219/bcddigits.py b/adafruit_max7219/bcddigits.py index 81ec5ff..80617f1 100644 --- a/adafruit_max7219/bcddigits.py +++ b/adafruit_max7219/bcddigits.py @@ -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): @@ -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)) From 6262da8af1db1767923a37ade5171a57e07576b3 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 6 Apr 2022 11:39:57 -0400 Subject: [PATCH 2/5] Correct how large numbers can be handled --- adafruit_max7219/max7219.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/adafruit_max7219/max7219.py b/adafruit_max7219/max7219.py index c1960c2..e42b548 100644 --- a/adafruit_max7219/max7219.py +++ b/adafruit_max7219/max7219.py @@ -193,18 +193,24 @@ 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) + my_spi_device.write(bytearray([cmd, byte_data])) def show(self) -> None: """ From ed0043ffb1ee96adb91ea932cf80de26ebcc53e1 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 6 Apr 2022 12:03:13 -0400 Subject: [PATCH 3/5] Set _DECODEMODE with data_overflow = True --- adafruit_max7219/bcddigits.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_max7219/bcddigits.py b/adafruit_max7219/bcddigits.py index 80617f1..1688bb1 100644 --- a/adafruit_max7219/bcddigits.py +++ b/adafruit_max7219/bcddigits.py @@ -47,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() From 3813480764cd3756f138961b94e20ded07388018 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 6 Apr 2022 12:13:08 -0400 Subject: [PATCH 4/5] Make data_overflow arg keyword only --- adafruit_max7219/max7219.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_max7219/max7219.py b/adafruit_max7219/max7219.py index e42b548..de4df19 100644 --- a/adafruit_max7219/max7219.py +++ b/adafruit_max7219/max7219.py @@ -194,7 +194,7 @@ def __init__( self.framebuf = framebuf.FrameBuffer1(self._buffer, self.chain_length * 8, 8) # pylint: disable=arguments-differ - def write_cmd(self, cmd: int, data: int, data_overflow: bool = False) -> None: + def write_cmd(self, cmd: int, data: int, *, data_overflow: bool = False) -> None: """ Writes a command to spi device. From 13128e9f20569bb6c6d3131aa43f5817f8adef5e Mon Sep 17 00:00:00 2001 From: Alec Delaney <89490472+tekktrik@users.noreply.github.com> Date: Wed, 6 Apr 2022 12:48:39 -0400 Subject: [PATCH 5/5] Use bit mask when using data_overflow --- adafruit_max7219/max7219.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_max7219/max7219.py b/adafruit_max7219/max7219.py index de4df19..a71331e 100644 --- a/adafruit_max7219/max7219.py +++ b/adafruit_max7219/max7219.py @@ -210,6 +210,7 @@ def write_cmd(self, cmd: int, data: int, *, data_overflow: bool = False) -> None 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: