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:
29
.gitignore
vendored
29
.gitignore
vendored
@@ -1 +1,28 @@
|
|||||||
*.pyc
|
*.py[cdo]
|
||||||
|
|
||||||
|
# Editor detritus
|
||||||
|
*.vim
|
||||||
|
*.swp
|
||||||
|
tags
|
||||||
|
|
||||||
|
# Packaging detritus
|
||||||
|
*.egg
|
||||||
|
*.egg-info
|
||||||
|
dist
|
||||||
|
build
|
||||||
|
eggs
|
||||||
|
parts
|
||||||
|
bin
|
||||||
|
var
|
||||||
|
sdist
|
||||||
|
develop-eggs
|
||||||
|
.installed.cfg
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
coverage
|
||||||
|
.coverage
|
||||||
|
.tox
|
||||||
|
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ from gpio_components import MotionSensor
|
|||||||
pir = MotionSensor(5)
|
pir = MotionSensor(5)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if pir.motion_detected():
|
if pir.motion_detected:
|
||||||
print("Motion detected")
|
print("Motion detected")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
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
|
from w1thermsensor import W1ThermSensor
|
||||||
|
|
||||||
|
|
||||||
GPIO.setmode(GPIO.BCM)
|
|
||||||
GPIO.setwarnings(False)
|
|
||||||
|
|
||||||
|
|
||||||
class InputDevice(object):
|
class InputDevice(object):
|
||||||
def __init__(self, pin=None):
|
def __init__(self, pin=None):
|
||||||
if pin is None:
|
if pin is None:
|
||||||
@@ -42,8 +38,9 @@ class Button(InputDevice):
|
|||||||
class MotionSensor(InputDevice):
|
class MotionSensor(InputDevice):
|
||||||
def _is_active_with_pause(self):
|
def _is_active_with_pause(self):
|
||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
return self.is_active()
|
return self.is_active
|
||||||
|
|
||||||
|
@property
|
||||||
def motion_detected(self):
|
def motion_detected(self):
|
||||||
n = 20
|
n = 20
|
||||||
return sum(self._is_active_with_pause() for i in range(n)) > n/2
|
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)
|
average_value = sum(values) / len(values)
|
||||||
return average_value
|
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):
|
class TemperatureSensor(W1ThermSensor):
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
from RPi import GPIO
|
from RPi import GPIO
|
||||||
|
|
||||||
|
|
||||||
GPIO.setmode(GPIO.BCM)
|
|
||||||
GPIO.setwarnings(False)
|
|
||||||
|
|
||||||
|
|
||||||
class OutputDevice(object):
|
class OutputDevice(object):
|
||||||
def __init__(self, pin):
|
def __init__(self, pin):
|
||||||
self.pin = pin
|
self.pin = pin
|
||||||
|
|||||||
Reference in New Issue
Block a user