Skip to content

Replaced asserts with exceptions #15

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
Changes from 5 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
28 changes: 17 additions & 11 deletions adafruit_tlc5947.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TLC5947:

:param auto_write: This is a boolean that defaults to True and will automatically
write out all the channel values to the chip as soon as a
single one is updated. If you set to false to disable then
single one is updated. If you set to False to disable then
you MUST call write after every channel update or when you
deem necessary to update the chip state.

Expand All @@ -76,7 +76,7 @@ class TLC5947:
on the driver directly connected to the controller are 0 to
23, and for each driver add 24 to the port number printed.
The more drivers are chained, the more viable it is to set
auto_write=false, and call write explicitly after updating
auto_write=False, and call write explicitly after updating
all the channels.
"""

Expand Down Expand Up @@ -104,7 +104,8 @@ def duty_cycle(self):

@duty_cycle.setter
def duty_cycle(self, val):
assert 0 <= val <= 65535
if (val < 0) or (val > 65535):
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove ()s. The "pythonic" syntax is:

if val < 0 or val > 65535:

Same for all other conditionals.

raise ValueError("PWM intensity {0} outside supported range [0;65535]".format(val))
# Convert to 12-bit value (quantization error will occur!).
val = (val >> 4) & 0xFFF
self._tlc5947._set_gs_value(self._channel, val)
Expand All @@ -128,6 +129,8 @@ def frequency(self, val):


def __init__(self, spi, latch, *, auto_write=True, num_drivers=1):
if num_drivers < 1:
raise ValueError("need at least one driver; {0} is not supported.".format(num_drivers))
Copy link
Contributor

Choose a reason for hiding this comment

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

Capitalize first word in message.

Need at least...

Same for all messages.

self._spi = spi
self._latch = latch
self._latch.switch_to_output(value=False)
Expand Down Expand Up @@ -165,7 +168,9 @@ def write(self):
def _get_gs_value(self, channel):
# pylint: disable=no-else-return
# Disable should be removed when refactor can be tested
assert 0 <= channel < _CHANNELS * self._n
if (channel < 0) or (channel >= _CHANNELS * self._n):
raise ValueError(
"channel {0} not available with {1} board(s).".format(channel, self._n))
# Invert channel position as the last channel needs to be written first.
# I.e. is in the first position of the shift registr.
channel = _CHANNELS * self._n - 1 - channel
Expand All @@ -190,8 +195,12 @@ def _get_gs_value(self, channel):
raise RuntimeError('Unsupported bit offset!')

def _set_gs_value(self, channel, val):
assert 0 <= channel < _CHANNELS * self._n
assert 0 <= val <= 4095
if (channel < 0) or (channel >= _CHANNELS * self._n):
raise ValueError(
"channel {0} not available with {1} board(s).".format(channel, self._n))
if (val < 0) or (val > 4095):
raise ValueError("PWM intensity {0} outside supported range [0;4095]".format(val))

# Invert channel position as the last channel needs to be written first.
# I.e. is in the first position of the shift registr.
channel = _CHANNELS * self._n - 1 - channel
Expand Down Expand Up @@ -247,8 +256,7 @@ def __getitem__(self, key):
"""
if key < 0: # allow reverse adressing with negative index
key = key + _CHANNELS * self._n
assert 0 <= key < _CHANNELS * self._n
return self._get_gs_value(key)
return self._get_gs_value(key) # does parameter checking

def __setitem__(self, key, val):
"""Set the 12-bit PWM value (0-4095) for the specified channel (0-max).
Expand All @@ -259,6 +267,4 @@ def __setitem__(self, key, val):
"""
if key < 0: # allow reverse adressing with negative index
key = key + _CHANNELS * self._n
assert 0 <= key < _CHANNELS * self._n
assert 0 <= val <= 4095
self._set_gs_value(key, val)
self._set_gs_value(key, val) # does parameter checking