Skip to content

Commit 162df6c

Browse files
authored
Changed assert to exceptions
According to Issue #10 of the main repo, exceptions shall be used instead of asserts. I'm game.
1 parent 096b603 commit 162df6c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

adafruit_tlc5947.py

Lines changed: 15 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 (0 > val) or (65535 < val):
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 (1 > num_drivers):
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,8 @@ 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 (0 > channel) or (_CHANNELS * self._n <= channel):
172+
raise ValueError("channel {0} not available with {1} board(s).".format(channel, self._n))
169173
# Invert channel position as the last channel needs to be written first.
170174
# I.e. is in the first position of the shift registr.
171175
channel = _CHANNELS * self._n - 1 - channel
@@ -190,8 +194,11 @@ def _get_gs_value(self, channel):
190194
raise RuntimeError('Unsupported bit offset!')
191195

192196
def _set_gs_value(self, channel, val):
193-
assert 0 <= channel < _CHANNELS * self._n
194-
assert 0 <= val <= 4095
197+
if (0 > channel) or (_CHANNELS * self._n <= channel):
198+
raise ValueError("channel {0} not available with {1} board(s).".format(channel, self._n))
199+
if (0 > val) or (4095 < val):
200+
raise ValueError("PWM intensity {0} outside supported range [0;4095]".format(val))
201+
195202
# Invert channel position as the last channel needs to be written first.
196203
# I.e. is in the first position of the shift registr.
197204
channel = _CHANNELS * self._n - 1 - channel
@@ -247,8 +254,7 @@ def __getitem__(self, key):
247254
"""
248255
if key < 0: # allow reverse adressing with negative index
249256
key = key + _CHANNELS * self._n
250-
assert 0 <= key < _CHANNELS * self._n
251-
return self._get_gs_value(key)
257+
return self._get_gs_value(key) # does parameter checking
252258

253259
def __setitem__(self, key, val):
254260
"""Set the 12-bit PWM value (0-4095) for the specified channel (0-max).
@@ -259,6 +265,4 @@ def __setitem__(self, key, val):
259265
"""
260266
if key < 0: # allow reverse adressing with negative index
261267
key = key + _CHANNELS * self._n
262-
assert 0 <= key < _CHANNELS * self._n
263-
assert 0 <= val <= 4095
264-
self._set_gs_value(key, val)
268+
self._set_gs_value(key, val) # does parameter checking

0 commit comments

Comments
 (0)