@@ -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 (0 > val ) or (65535 < val ):
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 (1 > num_drivers ):
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,8 @@ 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 (0 > channel ) or (_CHANNELS * self ._n <= channel ):
172
+ raise ValueError ("channel {0} not available with {1} board(s)." .format (channel , self ._n ))
169
173
# Invert channel position as the last channel needs to be written first.
170
174
# I.e. is in the first position of the shift registr.
171
175
channel = _CHANNELS * self ._n - 1 - channel
@@ -190,8 +194,11 @@ def _get_gs_value(self, channel):
190
194
raise RuntimeError ('Unsupported bit offset!' )
191
195
192
196
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
+
195
202
# Invert channel position as the last channel needs to be written first.
196
203
# I.e. is in the first position of the shift registr.
197
204
channel = _CHANNELS * self ._n - 1 - channel
@@ -247,8 +254,7 @@ def __getitem__(self, key):
247
254
"""
248
255
if key < 0 : # allow reverse adressing with negative index
249
256
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
252
258
253
259
def __setitem__ (self , key , val ):
254
260
"""Set the 12-bit PWM value (0-4095) for the specified channel (0-max).
@@ -259,6 +265,4 @@ def __setitem__(self, key, val):
259
265
"""
260
266
if key < 0 : # allow reverse adressing with negative index
261
267
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