mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Some more tests...
This commit is contained in:
@@ -9,6 +9,7 @@ from __future__ import (
|
||||
|
||||
import warnings
|
||||
from time import sleep, time
|
||||
from threading import Event
|
||||
|
||||
from .exc import InputDeviceError, GPIODeviceError, GPIODeviceClosed
|
||||
from .devices import GPIODevice, CompositeDevice
|
||||
|
||||
@@ -9,13 +9,14 @@ str = type('')
|
||||
|
||||
from collections import namedtuple
|
||||
from time import time
|
||||
from threading import Thread, Event
|
||||
try:
|
||||
from math import isclose
|
||||
except ImportError:
|
||||
from ..compat import isclose
|
||||
|
||||
from . import Pin, PINS_CLEANUP
|
||||
from ..exc import PinSetInput, PinPWMUnsupported
|
||||
from ..exc import PinSetInput, PinPWMUnsupported, PinFixedPull
|
||||
|
||||
|
||||
PinState = namedtuple('PinState', ('timestamp', 'state'))
|
||||
@@ -162,6 +163,48 @@ class MockPin(Pin):
|
||||
assert isclose(actual.state, expected[1])
|
||||
|
||||
|
||||
class MockPulledUpPin(MockPin):
|
||||
"""
|
||||
This derivative of :class:`MockPin` emulates a pin with a physical pull-up
|
||||
resistor.
|
||||
"""
|
||||
def _set_pull(self, value):
|
||||
if value != 'up':
|
||||
raise PinFixedPull('pin has a physical pull-up resistor')
|
||||
|
||||
|
||||
class MockChargingPin(MockPin):
|
||||
"""
|
||||
This derivative of :class:`MockPin` emulates a pin which, when set to
|
||||
input, waits a predetermined length of time and then drives itself high
|
||||
(as if attached to, e.g. a typical circuit using an LDR and a capacitor
|
||||
to time the charging rate).
|
||||
"""
|
||||
def __init__(self, number):
|
||||
super(MockChargingPin, self).__init__()
|
||||
self.charge_time = 0.01
|
||||
self._charge_stop = Event()
|
||||
self._charge_thread = None
|
||||
|
||||
def _set_function(self, value):
|
||||
super(MockChargingPin, self)._set_function(value)
|
||||
if value == 'input':
|
||||
if self._charge_thread:
|
||||
self._charge_stop.set()
|
||||
self._charge_thread.join()
|
||||
self._charge_stop.clear()
|
||||
self._charge_thread = Thread(target=lambda: self._charged)
|
||||
self._charge_thread.start()
|
||||
elif value == 'output':
|
||||
if self.charge_thread:
|
||||
self._charge_stop.set()
|
||||
self._charge_thread.join()
|
||||
|
||||
def _charged(self):
|
||||
if not self._charge_stop.wait(self.charge_time):
|
||||
self.drive_high()
|
||||
|
||||
|
||||
class MockPWMPin(MockPin):
|
||||
"""
|
||||
This derivative of :class:`MockPin` adds PWM support.
|
||||
|
||||
Reference in New Issue
Block a user