Python gpiozero 模块,Button() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用gpiozero.Button()

项目:playstreamation    作者:alewir    | 项目源码 | 文件源码
def main():
    button_enter = Button(PIN_ENTER)
    button_enter.when_pressed = btn_enter

    button_esc = Button(PIN_ESC)
    button_esc.when_pressed = btn_esc

    button_up = Button(PIN_UP)
    button_up.when_pressed = btn_up

    button_down = Button(PIN_DOWN)
    button_down.when_pressed = btn_down

    button_left = Button(PIN_LEFT)
    button_left.when_pressed = btn_left

    button_right = Button(PIN_RIGHT)
    button_right.when_pressed = btn_right

    pause()
项目:Sample-Code    作者:meigrafd    | 项目源码 | 文件源码
def main():
    try:
        with Button(21, hold_time=0) as button:
            button.when_held = lambda: count_time(button)
            signal.pause()
    except KeyboardInterrupt:
        pass
项目:EduKit1    作者:CamJam-EduKit    | 项目源码 | 文件源码
def startgreen():
    # Remember all code in the function is indented

# Turn the green off and the amber on for 3 seconds
# ('Pedestrian' red LED stays lit)
def steadyamber():
    # Remember all code in the function is indented

# Turn the amber off, and then the red on for 1 second
def steadyred():
    # Remember all code in the function is indented

# Sound the buzzer for 4 seconds
# (If you have the 'pedestrian' LEDs, turn the red off and green on)
def startwalking():
    # Make the buzzer buzz on and off, half a second of
    # sound followed by half a second of silence

# Turn the buzzer off and wait for 2 seconds
# (If you have a second green 'pedestrian' LED, make it flash on and
# off for the two seconds)
def dontwalk():
    # Remember all code in the function is indented

# Flash the amber on and off for 6 seconds
# (And the green 'pedestrian' LED too)
def flashingambergreen():
    # Remember all code in the function is indented

# Flash the amber for one more second
# (Turn the green 'pedestrian' LED off and the red on)
def flashingamber():
    # Remember all code in the function is indented

# Go through the traffic light sequence by calling each function
# one after the other.
def trafficlightqequence():
    # Remember all code in the function is indented

os.system('clear')  # Clears the terminal
print("Traffic Lights")
# Initialise the traffic lights
startgreen()

# Here is the loop that waits at lease 20 seconds before
# stopping the cars if the button has been pressed
while True:  # Loop around forever
    buttonnotpressed = True  # Button has not been pressed
    start = time.time()  # Records the current time
    while buttonnotpressed:  # While the button has not been pressed
        time.sleep(0.1)  # Wait for 0.1s
        if button.ispressed:  # If the button is pressed
            now = time.time()
            buttonnotpressed = False  # Button has been pressed
            if (now - start) <= 20:  # If under 20 seconds
                time.sleep(20 - (now - start))  # Wait until 20s is up
                trafficlightqequence()  # Run the traffic light sequence