From 768a27a71bb90d3f8175c4f1c52a91ec8bce39e4 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Sat, 20 Feb 2016 17:32:11 +0000 Subject: [PATCH] MockPin fixups * alter the PinSetInput exception message to match other `Pin` implementations * constrain the state of MockPin to a `bool`, and the state of MockPWMPin to a `float` * allow MockPin to have a `None` frequency set, matching the docs http://gpiozero.readthedocs.org/en/latest/api_pins.html#gpiozero.pins.Pin.frequency --- gpiozero/pins/mock.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/gpiozero/pins/mock.py b/gpiozero/pins/mock.py index aa6afa5..ee5a6ad 100644 --- a/gpiozero/pins/mock.py +++ b/gpiozero/pins/mock.py @@ -64,9 +64,10 @@ class MockPin(Pin): def _set_state(self, value): if self._function == 'input': - raise PinSetInput() + raise PinSetInput('cannot set state of pin %r' % self) assert self._function == 'output' assert 0 <= value <= 1 + value = bool(value) if self._state != value: t = time() self._state = value @@ -77,7 +78,8 @@ class MockPin(Pin): return None def _set_frequency(self, value): - raise PinPWMUnsupported() + if value is not None: + raise PinPWMUnsupported() def _get_pull(self): return self._pull @@ -159,6 +161,18 @@ class MockPWMPin(MockPin): super(MockPWMPin, self).__init__(number) self._frequency = None + def _set_state(self, value): + if self._function == 'input': + raise PinSetInput('cannot set state of pin %r' % self) + assert self._function == 'output' + assert 0 <= value <= 1 + value = float(value) + if self._state != value: + t = time() + self._state = value + self.states.append(PinState(t - self._last_change, value)) + self._last_change = t + def _get_frequency(self): return self._frequency