@@ -65,7 +65,7 @@ class TLC5947:
65
65
66
66
:param auto_write: This is a boolean that defaults to True and will automatically
67
67
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
69
69
you MUST call write after every channel update or when you
70
70
deem necessary to update the chip state.
71
71
@@ -76,7 +76,7 @@ class TLC5947:
76
76
on the driver directly connected to the controller are 0 to
77
77
23, and for each driver add 24 to the port number printed.
78
78
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
80
80
all the channels.
81
81
"""
82
82
@@ -104,7 +104,8 @@ def duty_cycle(self):
104
104
105
105
@duty_cycle .setter
106
106
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 ))
108
109
# Convert to 12-bit value (quantization error will occur!).
109
110
val = (val >> 4 ) & 0xFFF
110
111
self ._tlc5947 ._set_gs_value (self ._channel , val )
@@ -128,6 +129,8 @@ def frequency(self, val):
128
129
129
130
130
131
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 ))
131
134
self ._spi = spi
132
135
self ._latch = latch
133
136
self ._latch .switch_to_output (value = False )
@@ -165,7 +168,9 @@ def write(self):
165
168
def _get_gs_value (self , channel ):
166
169
# pylint: disable=no-else-return
167
170
# 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 ))
169
174
# Invert channel position as the last channel needs to be written first.
170
175
# I.e. is in the first position of the shift registr.
171
176
channel = _CHANNELS * self ._n - 1 - channel
@@ -190,8 +195,12 @@ def _get_gs_value(self, channel):
190
195
raise RuntimeError ('Unsupported bit offset!' )
191
196
192
197
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
+
195
204
# Invert channel position as the last channel needs to be written first.
196
205
# I.e. is in the first position of the shift registr.
197
206
channel = _CHANNELS * self ._n - 1 - channel
@@ -247,8 +256,7 @@ def __getitem__(self, key):
247
256
"""
248
257
if key < 0 : # allow reverse adressing with negative index
249
258
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
252
260
253
261
def __setitem__ (self , key , val ):
254
262
"""Set the 12-bit PWM value (0-4095) for the specified channel (0-max).
@@ -259,6 +267,4 @@ def __setitem__(self, key, val):
259
267
"""
260
268
if key < 0 : # allow reverse adressing with negative index
261
269
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