IOPress

Article Index

  • PythonGZeroPrograms 2e
  • Chapter 2
  • Chapter 3
  • Chapter 5
  • Chapter 6
  • Chapter 8
  • Chapter 9
  • Chapter 10
  • Chapter 11
  • Chapter 12
  • Chapter 13
  • Chapter 14
  • Chapter 15
  • All Pages
Page 6 of 13

 

Page 98

from gpiozero import Button
from signal import pause
button = Button(4, bounce_time=0.25)

def pressed():
    print(“Button pressed”)

def released():
    print(“Button released”)

button.when_pressed = pressed
button.when_released = released
pause()

Page 102

from gpiozero import Button
from gpiozero import LED
from time import sleep

button = Button(4)
led1 = LED(17, initial_value=True)
led2 = LED(27)
led3 = LED(22)

state = 0
buttonState = button.value
while True:
    buttonNow = button.value
    buttonDelta = buttonNow-buttonState
    buttonState = buttonNow
    if state == 0:
        if buttonDelta == 1:
            state = 1
            led1.off()
            led2.on()
            led3.off()

        continue
    if state == 1:
        if buttonDelta == 1:
            state = 2
            led1.off()
            led2.on()
            led3.off()
        continue
    if state == 2:
        if buttonDelta == 1:
            state = 0
            led1.on()
            led2.off()
            led3.off()
        continue
    sleep(0.1)

 

Page 103 

from gpiozero import Button
from time import perf_counter_ns

button = Button(4)
while True:
    button.wait_for_press()
button.wait_for_release()
t1 = perf_counter_ns()
button.wait_for_press()
t2 = perf_counter_ns()
print((t2-t1)/1000000)
 

Page 104 

from gpiozero import Button
from time import perf_counter_ns
button = Button(4)
while True:
    while not button.value:
        pass
    while button.value:
        pass
    t1 = perf_counter_ns()
    while not button.value:
        pass
    t2 = perf_counter_ns()
    print((t2-t1)/1000000)
 

Page 104

from gpiozero import DigitalInputDevice
from time import perf_counter_ns
pulse = DigitalInputDevice(4)
while True:
    while not pulse.value:
        pass
    while pulse.value:
        pass
    t1 = perf_counter_ns()
    while not pulse.value:
        pass
    t2 = perf_counter_ns()
    print((t2-t1)/1000000)
 

Page 105

 
from gpiozero import Device
from time import perf_counter_ns

Device()
pulse = Device.pin_factory.pin(4)
pulse.input_with_pull("up")
while True:
    while not pulse.state:
        pass
    while pulse.state:
        pass
    t1 = perf_counter_ns()
    while not pulse.state:
        pass
    t2 = perf_counter_ns()
    print((t2-t1)/1000000)
 

Page 105

from gpiozero import DigitalInputDevice


class Door(DigitalInputDevice):
    def __init__(self, pin=None, pull_up=True, active_state=None,
                 bounce_time=None, pin_factory=None):
        super(Door, self).__init__(
            pin, pull_up=pull_up, active_state=active_state,
            bounce_time=bounce_time, pin_factory=pin_factory)

    @property
    def value(self):
        return super(Door, self).value


Door.is_closed = Door.is_active
Door.when_open = Door.when_deactivated
Door.when_closed = Door.when_activated
Door.wait_for_open = Door.wait_for_inactive
Door.wait_for_close = Door.wait_for_active
 
 
  • << Prev
  • Next >>

Main Menu

  • Home
  • Books
    • Explore Intel Edison
    • Financial Functions
    • Micro:bit IoT In C Second Edition
    • Python
      • Programmer’s Python: Async
      • Programmer's Python: Everything is an Object 2e
      • Programmer’s Python: Everything is Data
      • Extending & Embedding Python Using C
    • Other Languages
      • Programmer's Guide To Kotlin 3ed
      • Deep C#
    • Theory
      • The Programmer’s Guide To Theory
      • The Trick Of The Mind
    • ESP32
      • Programming the ESP32 in MicroPython
      • Programming The ESP32 In C Using The Espressif IDF
      • Programming The ESP32 In C Using The Arduino Library
    • JavaScript
      • JavaScript Async
      • JavaScript Bitmap Graphics With Canvas
      • JavaScript Jems
      • Just jQuery: Events, Async & AJAX
      • Just jQuery: The Core UI
      • Just JavaScript
    • Android
      • Android Programming In Java
      • Android Programming Kotlin
    • C
      • Applying C For The IoT With Linux
      • Fundamental C: Getting Closer To The Machine
      • Deep C Dives: Adventures in C
    • Raspberry Pi
      • Raspberry Pi IoT in C Third Edition
      • Raspberry CM5 IoT In C
      • Raspberry Pi IoT In C Using Linux Drivers 2e
      • Raspberry Pi IoT In Python Using GPIO Zero 2e
      • Raspberry Pi IoT In Python Using Linux Drivers 2e
      • Raspberry Pi 5 IoT In C
    • Pico
      • Programming The Raspberry Pi Pico/W In C 2ed
      • Programming The Raspberry Pi Pico/W In MicroPython Second Edition
      • Master the Raspberry Pi Pico in C: WiFi with lwIP & mbedtls
  • Author's Pages
    • Ian Elliot
    • Mike James
    • Harry Fairhead
  • Contact Us
Just JavaScript
JustjQueryCoreUI

Back to Top

© 2025 IOPress