Skip to content

Fix bytearray TypeError Issue For Non-Express Boards #5

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

Merged
merged 5 commits into from
Apr 6, 2018
Merged
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
21 changes: 10 additions & 11 deletions adafruit_trellis.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ def __getitem__(self, x):
raise ValueError(('LED number must be between 0 -', self._parent._num_leds - 1))
led = ledLUT[x % 16] >> 4
mask = 1 << (ledLUT[x % 16] & 0x0f)
return bool(((self._parent._led_buffer[x // 16][led * 2] | \
self._parent._led_buffer[x // 16][(led * 2) + 1] << 8) & mask) > 0)
return bool(((self._parent._led_buffer[x // 16][(led * 2) + 1] | \
self._parent._led_buffer[x // 16][(led * 2) + 2] << 8) & mask) > 0)

def __setitem__(self, x, value):
if 0 < x >= self._parent._num_leds:
raise ValueError(('LED number must be between 0 -', self._parent._num_leds - 1))
led = ledLUT[x % 16] >> 4
mask = 1 << (ledLUT[x % 16] & 0x0f)
if value:
self._parent._led_buffer[x // 16][led * 2] |= mask
self._parent._led_buffer[x // 16][(led * 2) + 1] |= mask >> 8
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] &= ~mask
self._parent._led_buffer[x // 16][(led * 2) + 1] &= ~mask >> 8
self._parent._led_buffer[x // 16][(led * 2) + 1] &= ~mask
self._parent._led_buffer[x // 16][(led * 2) + 2] &= ~mask >> 8
else:
raise ValueError("LED value must be True or False")

Expand All @@ -105,7 +105,7 @@ def __setitem__(self, x, value):
def fill(self, on):
fill = 0xff if on else 0x00
for buff in range(len(self._parent._i2c_devices)):
for i in range(16):
for i in range(1, 17):
self._parent._led_buffer[buff][i] = fill
if self._parent._auto_show:
self._parent.show()
Expand Down Expand Up @@ -136,7 +136,7 @@ def __init__(self, i2c, addresses=None):
self._buttons = []
for i2c_address in addresses:
self._i2c_devices.append(i2c_device.I2CDevice(i2c, i2c_address))
self._led_buffer.append(bytearray(16))
self._led_buffer.append(bytearray(17))
self._buttons.append([bytearray(6), bytearray(6)])
self._num_leds = len(self._i2c_devices) * 16
self._temp = bytearray(1)
Expand Down Expand Up @@ -196,10 +196,9 @@ def brightness(self, brightness):

def show(self):
"""Refresh the LED buffer and show the changes."""
temp_led_buffer = bytearray(self._num_leds + 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this have an extra byte? I see you shifted the indices above but I don't remember why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because the first byte when sending register info to the HT16K33 is the register location; in this case it should be 0x00 (the +1 in the buffer construct leaves the first byte untouched). Thanks for asking this question though, because it highlighted the fact that I forgot to change the array size of ._led_buffer to 17... Which was coincidentally causing the simpletest off-one-at-a-time problem. I really need to do better with curbing familiarity influencing methodical reviewing. 🤔

pos = 0
for device in self._i2c_devices:
temp_led_buffer[1:] = self._led_buffer[pos]
temp_led_buffer = bytearray(self._led_buffer[pos])
with device:
device.write(temp_led_buffer)
pos += 1
Expand All @@ -224,7 +223,7 @@ def read_buttons(self):
lists: 1 for new button presses, 1 for button relases.
"""
for i in range(len(self._buttons)):
self._buttons[i][0][:] = self._buttons[i][1][:]
self._buttons[i][0] = bytearray(self._buttons[i][1])
self._write_cmd(_HT16K33_KEY_READ_CMD)
pos = 0
for device in self._i2c_devices:
Expand Down