Skip to content

Commit 9abe203

Browse files
authored
Merge pull request #12 from VladMihai28/issue11
Issue#11 Changing asserts to raise errors
2 parents a93aa75 + 9376732 commit 9abe203

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

adafruit_tcs34725.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
_ENABLE_PON = const(0x01)
7979
_GAINS = (1, 4, 16, 60)
8080
_CYCLES = (0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60)
81+
_INTEGRATION_TIME_THRESHOLD_LOW = 2.4
82+
_INTEGRATION_TIME_THRESHOLD_HIGH = 614.4
8183
# pylint: enable=bad-whitespace
8284

8385

@@ -172,7 +174,9 @@ def integration_time(self):
172174

173175
@integration_time.setter
174176
def integration_time(self, val):
175-
assert 2.4 <= val <= 614.4
177+
if not _INTEGRATION_TIME_THRESHOLD_LOW <= val <= _INTEGRATION_TIME_THRESHOLD_HIGH:
178+
raise ValueError("Integration Time must be between '{0}' and '{1}'".format(
179+
_INTEGRATION_TIME_THRESHOLD_LOW, _INTEGRATION_TIME_THRESHOLD_HIGH))
176180
cycles = int(val / 2.4)
177181
self._integration_time = cycles * 2.4 # pylint: disable=attribute-defined-outside-init
178182
self._write_u8(_REGISTER_ATIME, 256-cycles)
@@ -186,7 +190,8 @@ def gain(self):
186190

187191
@gain.setter
188192
def gain(self, val):
189-
assert val in _GAINS
193+
if val not in _GAINS:
194+
raise ValueError("Gain should be one of the following values: {0}".format(_GAINS))
190195
self._write_u8(_REGISTER_CONTROL, _GAINS.index(val))
191196

192197
@property
@@ -198,7 +203,8 @@ def interrupt(self):
198203

199204
@interrupt.setter
200205
def interrupt(self, val):
201-
assert not val
206+
if val:
207+
raise ValueError("Interrupt should be set to False in order to clear the interrupt")
202208
with self._device:
203209
self._device.write(b'\xe6')
204210

@@ -262,7 +268,8 @@ def cycles(self, val):
262268
if val == -1:
263269
self._write_u8(_REGISTER_ENABLE, enable & ~(_ENABLE_AIEN))
264270
else:
265-
assert val in _CYCLES
271+
if val not in _CYCLES:
272+
raise ValueError("Only the following cycles are permitted: {0}".format(_CYCLES))
266273
self._write_u8(_REGISTER_ENABLE, enable | _ENABLE_AIEN)
267274
self._write_u8(_REGISTER_APERS, _CYCLES.index(val))
268275

0 commit comments

Comments
 (0)