mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Minor clean-up
Expand gitignore to include the usual py developing stuff, add an __init__ to make this a legitimate package, move GPIO initialization into the package init, import all stuff from input and output devices in the package to enable the access demonstrated in the example, remove a duplicate method from LightSensor, and make motion_detected a property (for consistency with is_active which it presumably parallels).
This commit is contained in:
20
gpio_components/__init__.py
Normal file
20
gpio_components/__init__.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from RPi import GPIO
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
|
||||
from .input_devices import (
|
||||
InputDeviceError,
|
||||
InputDevice,
|
||||
Button,
|
||||
MotionSensor,
|
||||
LightSensor,
|
||||
TemperatureSensor,
|
||||
)
|
||||
from .output_devices import (
|
||||
OutputDevice,
|
||||
LED,
|
||||
Buzzer,
|
||||
Motor,
|
||||
)
|
||||
|
||||
@@ -2,10 +2,6 @@ from RPi import GPIO
|
||||
from w1thermsensor import W1ThermSensor
|
||||
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
|
||||
|
||||
class InputDevice(object):
|
||||
def __init__(self, pin=None):
|
||||
if pin is None:
|
||||
@@ -42,8 +38,9 @@ class Button(InputDevice):
|
||||
class MotionSensor(InputDevice):
|
||||
def _is_active_with_pause(self):
|
||||
sleep(0.1)
|
||||
return self.is_active()
|
||||
return self.is_active
|
||||
|
||||
@property
|
||||
def motion_detected(self):
|
||||
n = 20
|
||||
return sum(self._is_active_with_pause() for i in range(n)) > n/2
|
||||
@@ -86,11 +83,6 @@ class LightSensor(object):
|
||||
average_value = sum(values) / len(values)
|
||||
return average_value
|
||||
|
||||
def _get_average_light_level(self, num):
|
||||
values = [self._get_light_level() for n in range(num)]
|
||||
average_value = sum(values) / len(values)
|
||||
return average_value
|
||||
|
||||
|
||||
class TemperatureSensor(W1ThermSensor):
|
||||
@property
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
from RPi import GPIO
|
||||
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
|
||||
|
||||
class OutputDevice(object):
|
||||
def __init__(self, pin):
|
||||
self.pin = pin
|
||||
|
||||
Reference in New Issue
Block a user