From d11f9725e5a075d4180283439f8f50ec6a0451d3 Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Mon, 14 Sep 2015 21:01:24 +0100 Subject: [PATCH] Implement TemperatureSesnor and Motor and expand readme --- README.md | 169 +++++++++++++++++++++++++++++- gpio_components/input_devices.py | 50 +++++++++ gpio_components/output_devices.py | 4 + 3 files changed, 220 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a9daedb..d5425ae 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,173 @@ # gpio-components -Abstraction classes for everyday GPIO components, based on RPi.GPIO +Abstraction for everyday GPIO components, based on Ben Croston's `RPi.GPIO` library. -## Components +## Why? + +The "hello world" program in Java is at least 5 lines long, and contains 11 jargon words which are to be ignored. The "hello world" program in Python is one simple line. However, the "hello world" of physical computing in Python (flashing an LED) is similar to the Java program: + +```python +import RPi.GPIO as GPIO + +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) + +red = 2 + +GPIO.setup(red, GPIO.OUT) + +GPIO.output(red, True) +``` + +6 lines of code to flash an LED. And skipping over why `GPIO` is used twice in the first line; what `BCM` means; why set warnings to False; and so on. Young children and beginners shouldn't need to sit and copy out several lines of text they're told to ignore. They should be able to read their code and understand what it means. This module provides a simple interface to everyday components. The LED example becomes: + +```python +from gpio_components import LED + +red = LED(2) + +red.on() +``` + +Any guesses how to turn it off? + +## Implemented Components - LED - Buzzer - Button -- MotionSensor +- Motion Sensor +- Light Sensor +- Temperature Sensor +- Motor + +## Usage + +### LED + +Turn an LED on and off repeatedly: + +```python +from gpio_components import LED +from time import sleep + +red = LED(2) + +while True: + red.on() + sleep(1) + red.off() + sleep(1) +``` + +### Buzzer + +Turn a buzzer on and off repeatedly: + +```python +from gpio_components import Buzzer +from time import sleep + +buzzer = Buzzer(3) + +while True: + buzzer.on() + sleep(1) + buzzer.off() + sleep(1) +``` + +### Button + +Check if a button is pressed: + +```python +from gpio_components import Button + +button = Button(4) + +if button.is_active: + print("Button is pressed") +else: + print("Button is not pressed") +``` + +Wait for a button to be pressed before continuing: + +```python +from gpio_components import Button + +button = Button(4) + +button.wait_for_input() +print("Button was pressed") +``` + +Run a function every time the button is pressed: + +```python +from gpio_components import Button + +def hello(pin): + print("Button was pressed") + +button = Button(4) + +button.add_callback(hello) +``` + +### Motion Sensor + +Detect motion: + +```python +from gpio_components import MotionSensor + +pir = MotionSensor(5) + +while True: + if pir.motion_detected(): + print("Motion detected") +``` + +### Light Sensor + +Retrieve light sensor value: + +```python +from gpio_components import LightSensor + +light = LightSensor(6) + +print(light.value) +``` + +### Temperature Sensor + +Retrieve light sensor value: + +```python +from gpio_components import TemperatureSensor + +temperature = TemperatureSensor(6) + +print(temperature.value) +``` + +### Motor + +Drive two motors forwards for 5 seconds: + +```python +from gpio_components import Motor +from time import sleep + +left_motor = Motor(7) +right_motor = Motor(8) + +left_motor.on() +right_motor.on() +sleep(5) +left_motor.off() +right_motor.off() +``` diff --git a/gpio_components/input_devices.py b/gpio_components/input_devices.py index 40a31b2..28295ed 100644 --- a/gpio_components/input_devices.py +++ b/gpio_components/input_devices.py @@ -1,4 +1,5 @@ from RPi import GPIO +from w1thermsensor import W1ThermSensor GPIO.setmode(GPIO.BCM) @@ -47,5 +48,54 @@ class MotionSensor(InputDevice): return sum(self._is_active_with_pause() for i in range(n)) > n/2 +class LightSensor(object): + def __init__(self, pin=None, darkness_level=0.01): + if pin is None: + raise InputDeviceError('No GPIO pin number given') + + self.pin = pin + self.darkness_level = darkness_level + + @property + def value(self): + return self._get_average_light_level(5) + + def _get_light_level(self): + time_taken = self._time_charging_light_capacitor() + value = 100 * time_taken / self.darkness_level + return 100 - value + + def _time_charging_light_capacitor(self): + GPIO.setup(self.pin, GPIO.OUT) + GPIO.output(self.pin, GPIO.LOW) + sleep(0.1) + GPIO.setup(self.pin, GPIO.IN) + start_time = time() + end_time = time() + while ( + GPIO.input(self.pin) == GPIO.LOW and + time() - start_time < self.darkness_level + ): + end_time = time() + time_taken = end_time - start_time + return min(time_taken, self.darkness_level) + + 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 + + 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 + def value(self): + return self.get_temperature() + + class InputDeviceError(Exception): pass diff --git a/gpio_components/output_devices.py b/gpio_components/output_devices.py index 55336b0..2a09950 100644 --- a/gpio_components/output_devices.py +++ b/gpio_components/output_devices.py @@ -23,3 +23,7 @@ class LED(OutputDevice): class Buzzer(OutputDevice): pass + + +class Motor(OutputDevice): + pass