20170901 【资料2】API-BUTTON

树莓派Python课程 徐 自远 846℃

http://gpiozero.readthedocs.io/en/stable/api_input.html#button

11.1. Button

classgpiozero.Button(pin*pull_up=Truebounce_time=Nonehold_time=1hold_repeat=Falsepin_factory=None)[source]
Extends DigitalInputDevice and represents a simple push button or switch.

Connect one side of the button to a ground pin, and the other to any GPIO pin. Alternatively, connect one side of the button to the 3V3 pin, and the other to any GPIO pin, then set pull_upto False in the Button constructor.

The following example will print a line of text when the button is pushed:

Parameters:
  • pin (int) – The GPIO pin which the button is attached to. See Pin Numbering for valid pin numbers.
  • pull_up (bool) – If True (the default), the GPIO pin will be pulled high by default. In this case, connect the other side of the button to ground. If False, the GPIO pin will be pulled low by default. In this case, connect the other side of the button to 3V3.
  • bounce_time (float) – If None (the default), no software bounce compensation will be performed. Otherwise, this is the length of time (in seconds) that the component will ignore changes in state after an initial change.
  • hold_time (float) – The length of time (in seconds) to wait after the button is pushed, until executing the when_held handler. Defaults to 1.
  • hold_repeat (bool) – If True, the when_held handler will be repeatedly executed as long as the device remains active, every hold_time seconds. If False (the default) the when_held handler will be only be executed once per hold.
  • pin_factory (Factory) – See API – Pins for more information (this is an advanced feature which most users can ignore).
wait_for_press(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.
wait_for_release(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.
held_time
The length of time (in seconds) that the device has been held for. This is counted from the first execution of the when_held event rather than when the device activated, in contrast toactive_time. If the device is not currently held, this is None.
hold_repeat
If Truewhen_held will be executed repeatedly with hold_time seconds between each invocation.
hold_time
The length of time (in seconds) to wait after the device is activated, until executing the when_held handler. If hold_repeat is True, this is also the length of time between invocations of when_held.
is_held
When True, the device has been active for at least hold_time seconds.
is_pressed
Returns True if the device is currently active and False otherwise. This property is usually derived from value. Unlike value, this is always a boolean.
pin
The Pin that the device is connected to. This will be None if the device has been closed (see the close() method). When dealing with GPIO pins, query pin.number to discover the GPIO pin (in BCM numbering) that the device is connected to.
pull_up
If True, the device uses a pull-up resistor to set the GPIO pin “high” by default.
when_held
The function to run when the device has remained active for hold_time seconds.

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.

when_pressed
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.

when_released
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.

11.2. Line Sensor (TRCT5000)

classgpiozero.LineSensor(pin*queue_len=5sample_rate=100threshold=0.5partial=Falsepin_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:

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 theis_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 to True 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 be None if the device has been closed (see the close() method). When dealing with GPIO pins, query pin.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

喜欢 (0)

苏ICP备18041234号-1 bei_an 苏公网安备 32021402001397号