How to use the gpiozero.Button function in gpiozero

To help you get started, we’ve selected a few gpiozero examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gpiozero / gpiozero / tests / test_tools.py View on Github external
def test_zip_values(mock_factory):
    with Button(2) as btn1, Button(3) as btn2:
        zv = zip_values(btn1, btn2)
        assert next(zv) == (False, False)
        btn1.pin.drive_low()
        assert next(zv) == (True, False)
        btn2.pin.drive_low()
        assert next(zv) == (True, True)
        btn1.pin.drive_high()
        assert next(zv) == (False, True)
        btn2.pin.drive_high()
        assert next(zv) == (False, False)
    with Button(2) as btn1, Button(3) as btn2, Button(4) as btn3, Button(5) as btn4:
        zv = zip_values(btn1, btn2, btn3, btn4)
        assert next(zv) == (False, False, False, False)
        btn1.pin.drive_low()
        btn3.pin.drive_low()
        assert next(zv) == (True, False, True, False)
        btn2.pin.drive_low()
        btn4.pin.drive_low()
        assert next(zv) == (True, True, True, True)
        btn1.pin.drive_high()
        btn2.pin.drive_high()
        btn3.pin.drive_high()
        btn4.pin.drive_high()
        sleep(epsilon)
        assert next(zv) == (False, False, False, False)
github vwillcox / speedtest-cron / buttons.py View on Github external
import subprocess
from PIL import Image
from PIL import ImageOps
import ImageDraw
import ImageFont
import ipgetter
import socket
import subprocess
import shlex
from EPD import EPD

led = RGBLED(red=6, green=12, blue=5)
button1 = Button(16)
button2 = Button(19)
button3 = Button(20)
button4 = Button(26)


if os.name != "nt":
    import fcntl
    import struct

    def get_interface_ip(ifname):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s',
                                ifname[:15]))[20:24])

def get_lan_ip():
    ip = socket.gethostbyname(socket.gethostname())
    if ip.startswith("127.") and os.name != "nt":
        interfaces = [
            "eth0",
github gpiozero / gpiozero / tests / test_tools.py View on Github external
def test_zip_values(mock_factory):
    with Button(2) as btn1, Button(3) as btn2:
        zv = zip_values(btn1, btn2)
        assert next(zv) == (False, False)
        btn1.pin.drive_low()
        assert next(zv) == (True, False)
        btn2.pin.drive_low()
        assert next(zv) == (True, True)
        btn1.pin.drive_high()
        assert next(zv) == (False, True)
        btn2.pin.drive_high()
        assert next(zv) == (False, False)
    with Button(2) as btn1, Button(3) as btn2, Button(4) as btn3, Button(5) as btn4:
        zv = zip_values(btn1, btn2, btn3, btn4)
        assert next(zv) == (False, False, False, False)
        btn1.pin.drive_low()
        btn3.pin.drive_low()
        assert next(zv) == (True, False, True, False)
github gpiozero / gpiozero / docs / examples / music_box.py View on Github external
from gpiozero import Button
import pygame.mixer
from pygame.mixer import Sound
from signal import pause

pygame.mixer.init()

button_sounds = {
    Button(2): Sound("samples/drum_tom_mid_hard.wav"),
    Button(3): Sound("samples/drum_cymbal_open.wav"),
}

for button, sound in button_sounds.items():
    button.when_pressed = sound.play

pause()
github gpiozero / gpiozero / docs / examples / button_camera_2.py View on Github external
from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause

left_button = Button(2)
right_button = Button(3)
camera = PiCamera()

def capture():
    timestamp = datetime.now().isoformat()
    camera.capture('/home/pi/%s.jpg' % timestamp)

left_button.when_pressed = camera.start_preview
left_button.when_released = camera.stop_preview
right_button.when_pressed = capture

pause()
github JoelBender / bacpypes / samples / RaspberryPi / binaryio.py View on Github external
def __init__(self, button_id, **kwargs):
        if _debug:
            RPiBinaryInput._debug("__init__ %r %r", button_id, kwargs)
        BinaryInputObject.__init__(self, **kwargs)

        # create a button object
        self._button = Button(button_id)
github recantha / musicbox / musicbox.py View on Github external
import fluidsynth

# Start up the Synth and load the sound font
fs = fluidsynth.Synth()
fs.start(driver='alsa')

# Set-up buttons for reset and shutdown
button_reset = Button(5)
button_shutdown = Button(23)

# Set-up buttons for keyboard input
thumb_bottom = Button(13)
thumb_top = Button(6)
thumb_right = Button(12)
index_finger = Button(16)
middle_finger = Button(19)
ring_finger = Button(20)
pinky_finger = Button(21)

# Test procedure for the keyboard buttons
test_buttons = False
if test_buttons:
	while True:
		print("Tbot:{}/Ttop:{}/Trih:{}/Idx:{}/Mid:{}/Rng:{}/Pnk:{}".format(thumb_bottom.value, thumb_top.value, thumb_right.value, index_finger.value, middle_finger.value, ring_finger.value, pinky_finger.value))
		time.sleep(0.2)


# Define potentiometers
pot0 = MCP3008(channel=2)
pot1 = MCP3008(channel=1)
pot2 = MCP3008(channel=0)
github ukBaz / python-bluezero / examples / level10 / light_switch.py View on Github external
An honourable mention to the Authors of Make:Bluetooth for the idea.
In particular Alasdair Allan that did a great demo at OSCON 2015.
"""
import logging
from gpiozero import LED
from gpiozero import Button

from bluezero import peripheral

logging.getLogger('bluezero.adapter').setLevel(logging.DEBUG)
logging.basicConfig(level=logging.INFO)

# Hardware
# Ryanteck Traffic Hat
led = LED(24)
button = Button(25)
# pimoroni/explorer-hat
# led = LED(4)

led_state = False


def ble_state_callback():
    if switch_characteristic.value is None:
        print('Switch Characteristic is None')
        state_characteristic.send_notify_event(0)
        led.off()
    elif led.is_lit and switch_characteristic.value == 0:
        print('BLE send: off')
        state_characteristic.send_notify_event(0)
        led.off()
    elif led.is_lit is False and switch_characteristic.value == 1:
github gpiozero / gpiozero / docs / examples / mock_demo.py View on Github external
from gpiozero.pins.mock import MockFactory
from gpiozero import Device, Button, LED
from time import sleep

# Set the default pin factory to a mock factory
Device.pin_factory = MockFactory()

# Construct a couple of devices attached to mock pins 16 and 17, and link the
# devices
led = LED(17)
btn = Button(16)
led.source = btn.values

# Here the button isn't "pushed" so the LED's value should be False
print(led.value)

# Get a reference to mock pin 16 (used by the button)
btn_pin = Device.pin_factory.pin(16)

# Drive the pin low (this is what would happen electrically when the button is
# pushed)
btn_pin.drive_low()
sleep(0.1) # give source some time to re-read the button state
print(led.value)

btn_pin.drive_high()
sleep(0.1)
github TonyLHansen / raspberry-pi-safe-off-switch / python / shutdown-led-simple.py View on Github external
holdTime = int(sys.argv[2]) if len(sys.argv) >= 3 else 6
ledGPIO = int(sys.argv[3]) if len(sys.argv) >= 4 else 2

def when_pressed():
    # start blinking with 1/2 second rate
    led.blink(on_time=0.5, off_time=0.5)

def when_released():
    # be sure to turn the LEDs off if we release early
    led.off()

def shutdown():
    os.system("sudo poweroff")

led = LED(ledGPIO)
btn = Button(offGPIO, hold_time=holdTime)
btn.when_held = shutdown
btn.when_pressed = when_pressed
btn.when_released = when_released
pause()