From eb1f0a8abd3cbaf2fbf8d5f05c9f4cd8504fe193 Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Thu, 11 Oct 2018 17:16:32 -0600 Subject: [PATCH 1/2] only apply mask to _led_buffer if < 256 This works on CPython. I _think_ it will work on CircuitPython, since the old behavior is effectively a no-op there. On CPython it was throwing a `ValueError`. I want to be clear that I don't understand this code, so I'm definitely not confident that this change is the Correct Thing. --- adafruit_trellis.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_trellis.py b/adafruit_trellis.py index f2a2180..af09152 100644 --- a/adafruit_trellis.py +++ b/adafruit_trellis.py @@ -91,7 +91,8 @@ def __setitem__(self, x, value): led = ledLUT[x % 16] >> 4 mask = 1 << (ledLUT[x % 16] & 0x0f) if value: - self._parent._led_buffer[x // 16][(led * 2) + 1] |= mask + if mask < 256: + self._parent._led_buffer[x // 16][(led * 2) + 1] |= mask self._parent._led_buffer[x // 16][(led * 2) + 2] |= mask >> 8 elif not value: self._parent._led_buffer[x // 16][(led * 2) + 1] &= ~mask From 5151423270be13858ac2260df54be5214c7ad5a5 Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Wed, 17 Oct 2018 12:45:23 -0600 Subject: [PATCH 2/2] replace conditional with bitwise and, per @tannewt Confirmed working on Pi. --- adafruit_trellis.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adafruit_trellis.py b/adafruit_trellis.py index af09152..185f849 100644 --- a/adafruit_trellis.py +++ b/adafruit_trellis.py @@ -91,8 +91,7 @@ def __setitem__(self, x, value): led = ledLUT[x % 16] >> 4 mask = 1 << (ledLUT[x % 16] & 0x0f) if value: - if mask < 256: - self._parent._led_buffer[x // 16][(led * 2) + 1] |= mask + self._parent._led_buffer[x // 16][(led * 2) + 1] |= (mask & 0xff) self._parent._led_buffer[x // 16][(led * 2) + 2] |= mask >> 8 elif not value: self._parent._led_buffer[x // 16][(led * 2) + 1] &= ~mask