Skip to content

Issue#11 Changing asserts to raise errors #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions adafruit_tcs34725.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
_ENABLE_PON = const(0x01)
_GAINS = (1, 4, 16, 60)
_CYCLES = (0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60)
_INTEGRATION_TIME_THRESHOLD_LOW = 2.4
_INTEGRATION_TIME_THRESHOLD_HIGH = 614.4
# pylint: enable=bad-whitespace


Expand Down Expand Up @@ -172,7 +174,9 @@ def integration_time(self):

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

@gain.setter
def gain(self, val):
assert val in _GAINS
if val not in _GAINS:
raise ValueError("Gain should be one of the following values: {0}".format(_GAINS))
self._write_u8(_REGISTER_CONTROL, _GAINS.index(val))

@property
Expand All @@ -198,7 +203,8 @@ def interrupt(self):

@interrupt.setter
def interrupt(self, val):
assert not val
if val:
raise ValueError("Interrupt should be set to False in order to clear the interrupt")
with self._device:
self._device.write(b'\xe6')

Expand Down Expand Up @@ -262,7 +268,8 @@ def cycles(self, val):
if val == -1:
self._write_u8(_REGISTER_ENABLE, enable & ~(_ENABLE_AIEN))
else:
assert val in _CYCLES
if val not in _CYCLES:
raise ValueError("Only the following cycles are permitted: {0}".format(_CYCLES))
self._write_u8(_REGISTER_ENABLE, enable | _ENABLE_AIEN)
self._write_u8(_REGISTER_APERS, _CYCLES.index(val))

Expand Down