From 5f3a561b708ff00f5123c4f44ae0bacc81002f80 Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Thu, 17 Dec 2015 19:28:07 +0000 Subject: [PATCH] First draft of Dot and DotsBoard implementation --- gpiozero/__init__.py | 2 + gpiozero/boards.py | 88 ++++++++++++++++++++++++++++++++++++++- gpiozero/input_devices.py | 14 +++++++ 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/gpiozero/__init__.py b/gpiozero/__init__.py index 0682763..0650744 100644 --- a/gpiozero/__init__.py +++ b/gpiozero/__init__.py @@ -20,6 +20,7 @@ from .input_devices import ( AnalogInputDevice, MCP3008, MCP3004, + Dot, ) from .output_devices import ( OutputDeviceError, @@ -42,4 +43,5 @@ from .boards import ( Robot, RyanteckRobot, CamJamKitRobot, + DotsBoard, ) diff --git a/gpiozero/boards.py b/gpiozero/boards.py index 349a8f0..bd3cdcb 100644 --- a/gpiozero/boards.py +++ b/gpiozero/boards.py @@ -12,7 +12,7 @@ except ImportError: from time import sleep from collections import namedtuple -from .input_devices import InputDeviceError, Button +from .input_devices import InputDeviceError, Button, Dot from .output_devices import OutputDeviceError, LED, PWMLED, Buzzer, Motor from .devices import CompositeDevice, SourceMixin @@ -419,3 +419,89 @@ class CamJamKitRobot(Robot): """ def __init__(self): super(CamJamKitRobot, self).__init__(left=(9, 10), right=(7, 8)) + +class DotsBoard(CompositeDevice): + """ + DOTs Board - conductive paint dot-to-dot activity board by Rachel Rayns + """ + def __init__(self, dots_required=5): + self._color_pins = { + 27: 'red', + 10: 'green', + 0: 'blue', + 19: 'orange', + } + + self._dot_pins = { + 1: 7, + 4: 2, + 5: 9, + 6: 14, + 7: 6, + 8: 18, + 9: 17, + 11: 8, + 12: 10, + 14: 1, + 15: 3, + 16: 13, + 17: 4, + 18: 20, + 21: 12, + 22: 15, + 23: 16, + 24: 19, + 25: 5, + 26: 11, + } + + self._extra_pins = { + 20: 'bear', + 13: 'cloud', + } + + pins = ( + list(self._color_pins.keys()) + + list(self._dot_pins.keys()) + + list(self._extra_pins.keys()) + ) + + self.dots = {pin: Dot(pin) for pin in pins} + + self.dots_required = dots_required + + def _selected_dots(self, pins, selected=True): + dots = [self.dots[pin] for pin in pins.keys()] + return sorted(pins[dot.pin] for dot in dots if dot.is_active) + + def _unselected_dots(self, pins, selected=True): + dots = [self.dots[pin] for pin in pins.keys()] + return sorted(pins[dot.pin] for dot in dots if not dot.is_active) + + @property + def selected_dots(self): + return self._selected_dots(self._dot_pins) + + @property + def selected_colors(self): + return self._selected_dots(self._color_pins) + + @property + def selected_extras(self): + return self._selected_dots(self._extra_pins) + + @property + def unselected_dots(self): + return self._unselected_dots(self._dot_pins) + + @property + def unselected_colors(self): + return self._unselected_dots(self._color_pins) + + @property + def unselected_extras(self): + return self._unselected_dots(self._extra_pins) + + @property + def enough_dots_selected(self): + return len(self.selected_dots) >= self.dots_required diff --git a/gpiozero/input_devices.py b/gpiozero/input_devices.py index 7e60ad5..9bc0b07 100644 --- a/gpiozero/input_devices.py +++ b/gpiozero/input_devices.py @@ -610,3 +610,17 @@ class MCP3004(MCP3008): if not 0 <= channel < 4: raise InputDeviceError('channel must be between 0 and 3') super(MCP3004, self).__init__(channel, device) + + +class Dot(object): + def __init__(self, pin=None): + if pin is None: + raise GPIODeviceError('No GPIO pin number given') + self.pin = pin + + @property + def is_active(self): + dot = InputDevice(self.pin, pull_up=True) + status = dot.is_active + dot.close() + return status