From b87bb0c4c3fcaeb7cd4477afb447d55fbacf307d Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Tue, 15 Sep 2015 08:40:35 +0100 Subject: [PATCH] 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). --- .gitignore | 29 ++++++++++++++++++++++++++++- README.md | 2 +- gpio_components/__init__.py | 20 ++++++++++++++++++++ gpio_components/input_devices.py | 12 ++---------- gpio_components/output_devices.py | 4 ---- 5 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 gpio_components/__init__.py diff --git a/.gitignore b/.gitignore index 0d20b64..3772885 100644 --- a/.gitignore +++ b/.gitignore @@ -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 + diff --git a/README.md b/README.md index d5425ae..1ae6537 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ from gpio_components import MotionSensor pir = MotionSensor(5) while True: - if pir.motion_detected(): + if pir.motion_detected: print("Motion detected") ``` diff --git a/gpio_components/__init__.py b/gpio_components/__init__.py new file mode 100644 index 0000000..2e26e74 --- /dev/null +++ b/gpio_components/__init__.py @@ -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, + ) + diff --git a/gpio_components/input_devices.py b/gpio_components/input_devices.py index f586d6b..bbc188a 100644 --- a/gpio_components/input_devices.py +++ b/gpio_components/input_devices.py @@ -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 diff --git a/gpio_components/output_devices.py b/gpio_components/output_devices.py index 2a09950..6129423 100644 --- a/gpio_components/output_devices.py +++ b/gpio_components/output_devices.py @@ -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