mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Add basic version of code
This commit is contained in:
32
examples/my_hat.py
Normal file
32
examples/my_hat.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from gpio_components import LED, Buzzer, Button
|
||||
from time import sleep
|
||||
|
||||
|
||||
class MyHat(object):
|
||||
def __init__(self):
|
||||
self.red = LED(9)
|
||||
self.yellow = LED(10)
|
||||
self.green = LED(11)
|
||||
self.buzzer = Buzzer(14)
|
||||
self.button = Button(15)
|
||||
|
||||
self.leds = [self.red, self.yellow, self.green]
|
||||
self.outputs = self.leds + [self.buzzer]
|
||||
|
||||
def all_on(self):
|
||||
for device in self.outputs:
|
||||
device.on()
|
||||
|
||||
def all_off(self):
|
||||
for device in self.outputs:
|
||||
device.off()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
hat = MyHat()
|
||||
|
||||
while True:
|
||||
if hat.button.is_pressed():
|
||||
hat.all_on()
|
||||
else:
|
||||
hat.all_off()
|
||||
18
gpio_components/input_devices.py
Normal file
18
gpio_components/input_devices.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from RPi import GPIO
|
||||
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
|
||||
|
||||
class InputDevice(object):
|
||||
def __init__(self, pin):
|
||||
self.pin = pin
|
||||
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
|
||||
|
||||
def is_pressed(self):
|
||||
return GPIO.input(self.pin) == 0
|
||||
|
||||
|
||||
class Button(InputDevice):
|
||||
pass
|
||||
25
gpio_components/output_devices.py
Normal file
25
gpio_components/output_devices.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from RPi import GPIO
|
||||
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
|
||||
|
||||
class OutputDevice(object):
|
||||
def __init__(self, pin):
|
||||
self.pin = pin
|
||||
GPIO.setup(pin, GPIO.OUT)
|
||||
|
||||
def on(self):
|
||||
GPIO.output(self.pin, True)
|
||||
|
||||
def off(self):
|
||||
GPIO.output(self.pin, False)
|
||||
|
||||
|
||||
class LED(OutputDevice):
|
||||
pass
|
||||
|
||||
|
||||
class Buzzer(OutputDevice):
|
||||
pass
|
||||
Reference in New Issue
Block a user