Skip to content

Commit 43f6c80

Browse files
authored
Merge pull request #15 from ArthurDent62/ArthurDent62-exceptionNotAssert
Replaced asserts with exceptions. Thanks @ArthurDent62
2 parents 7799703 + 1e6dfbe commit 43f6c80

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

adafruit_tlc5947.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class TLC5947:
6565
6666
:param auto_write: This is a boolean that defaults to True and will automatically
6767
write out all the channel values to the chip as soon as a
68-
single one is updated. If you set to false to disable then
68+
single one is updated. If you set to False to disable then
6969
you MUST call write after every channel update or when you
7070
deem necessary to update the chip state.
7171
@@ -76,7 +76,7 @@ class TLC5947:
7676
on the driver directly connected to the controller are 0 to
7777
23, and for each driver add 24 to the port number printed.
7878
The more drivers are chained, the more viable it is to set
79-
auto_write=false, and call write explicitly after updating
79+
auto_write=False, and call write explicitly after updating
8080
all the channels.
8181
"""
8282

@@ -104,7 +104,8 @@ def duty_cycle(self):
104104

105105
@duty_cycle.setter
106106
def duty_cycle(self, val):
107-
assert 0 <= val <= 65535
107+
if val < 0 or val > 65535:
108+
raise ValueError("PWM intensity {0} outside supported range [0;65535]".format(val))
108109
# Convert to 12-bit value (quantization error will occur!).
109110
val = (val >> 4) & 0xFFF
110111
self._tlc5947._set_gs_value(self._channel, val)
@@ -128,6 +129,8 @@ def frequency(self, val):
128129

129130

130131
def __init__(self, spi, latch, *, auto_write=True, num_drivers=1):
132+
if num_drivers < 1:
133+
raise ValueError("Need at least one driver; {0} is not supported.".format(num_drivers))
131134
self._spi = spi
132135
self._latch = latch
133136
self._latch.switch_to_output(value=False)
@@ -165,7 +168,9 @@ def write(self):
165168
def _get_gs_value(self, channel):
166169
# pylint: disable=no-else-return
167170
# Disable should be removed when refactor can be tested
168-
assert 0 <= channel < _CHANNELS * self._n
171+
if channel < 0 or channel >= _CHANNELS * self._n:
172+
raise ValueError(
173+
"Channel {0} not available with {1} board(s).".format(channel, self._n))
169174
# Invert channel position as the last channel needs to be written first.
170175
# I.e. is in the first position of the shift registr.
171176
channel = _CHANNELS * self._n - 1 - channel
@@ -190,8 +195,12 @@ def _get_gs_value(self, channel):
190195
raise RuntimeError('Unsupported bit offset!')
191196

192197
def _set_gs_value(self, channel, val):
193-
assert 0 <= channel < _CHANNELS * self._n
194-
assert 0 <= val <= 4095
198+
if channel < 0 or channel >= _CHANNELS * self._n:
199+
raise ValueError(
200+
"Channel {0} not available with {1} board(s).".format(channel, self._n))
201+
if val < 0 or val > 4095:
202+
raise ValueError("PWM intensity {0} outside supported range [0;4095]".format(val))
203+
195204
# Invert channel position as the last channel needs to be written first.
196205
# I.e. is in the first position of the shift registr.
197206
channel = _CHANNELS * self._n - 1 - channel
@@ -247,8 +256,7 @@ def __getitem__(self, key):
247256
"""
248257
if key < 0: # allow reverse adressing with negative index
249258
key = key + _CHANNELS * self._n
250-
assert 0 <= key < _CHANNELS * self._n
251-
return self._get_gs_value(key)
259+
return self._get_gs_value(key) # does parameter checking
252260

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

0 commit comments

Comments
 (0)