11.2. Line Sensor (TRCT5000)
- class
gpiozero.
LineSensor
(pin, *, queue_len=5, sample_rate=100, threshold=0.5, partial=False, pin_factory=None)[source] - Extends
SmoothedInputDevice
and represents a single pin line sensor like the TCRT5000 infra-red proximity sensor found in the CamJam #3 EduKit.A typical line sensor has a small circuit board with three pins: VCC, GND, and OUT. VCC should be connected to a 3V3 pin, GND to one of the ground pins, and finally OUT to the GPIO specified as the value of the pin parameter in the constructor.
The following code will print a line of text indicating when the sensor detects a line, or stops detecting a line:
1234567from gpiozero import LineSensorfrom signal import pausesensor = LineSensor(4)sensor.when_line = lambda: print('Line detected')sensor.when_no_line = lambda: print('No line detected')pause()Parameters: - pin (int) – The GPIO pin which the sensor is attached to. See Pin Numbering for valid pin numbers.
- queue_len (int) – The length of the queue used to store values read from the sensor. This defaults to 5.
- sample_rate (float) – The number of values to read from the device (and append to the internal queue) per second. Defaults to 100.
- threshold (float) – Defaults to 0.5. When the mean of all values in the internal queue rises above this value, the sensor will be considered “active” by the
is_active
property, and all appropriate events will be fired. - partial (bool) – When
False
(the default), the object will not return a value foris_active
until the internal queue has filled with values. Only set this toTrue
if you require values immediately after object construction. - pin_factory (Factory) – See API – Pins for more information (this is an advanced feature which most users can ignore).
wait_for_line
(timeout=None)- Pause the script until the device is deactivated, or the timeout is reached.
Parameters: timeout (float) – Number of seconds to wait before proceeding. If this is None
(the default), then wait indefinitely until the device is inactive.
wait_for_no_line
(timeout=None)- Pause the script until the device is activated, or the timeout is reached.
Parameters: timeout (float) – Number of seconds to wait before proceeding. If this is None
(the default), then wait indefinitely until the device is active.
pin
- The
Pin
that the device is connected to. This will beNone
if the device has been closed (see theclose()
method). When dealing with GPIO pins, querypin.number
to discover the GPIO pin (in BCM numbering) that the device is connected to.
when_line
- The function to run when the device changes state from active to inactive.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that deactivated will be passed as that parameter.
Set this property to
None
(the default) to disable the event.
when_no_line
- The function to run when the device changes state from inactive to active.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.
Set this property to
None
(the default) to disable the event.
转载请注明:徐自远的乱七八糟小站 » 20170901 【资料2】API-BUTTON