mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
First draft of Dot and DotsBoard implementation
This commit is contained in:
@@ -20,6 +20,7 @@ from .input_devices import (
|
|||||||
AnalogInputDevice,
|
AnalogInputDevice,
|
||||||
MCP3008,
|
MCP3008,
|
||||||
MCP3004,
|
MCP3004,
|
||||||
|
Dot,
|
||||||
)
|
)
|
||||||
from .output_devices import (
|
from .output_devices import (
|
||||||
OutputDeviceError,
|
OutputDeviceError,
|
||||||
@@ -42,4 +43,5 @@ from .boards import (
|
|||||||
Robot,
|
Robot,
|
||||||
RyanteckRobot,
|
RyanteckRobot,
|
||||||
CamJamKitRobot,
|
CamJamKitRobot,
|
||||||
|
DotsBoard,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ except ImportError:
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
from collections import namedtuple
|
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 .output_devices import OutputDeviceError, LED, PWMLED, Buzzer, Motor
|
||||||
from .devices import CompositeDevice, SourceMixin
|
from .devices import CompositeDevice, SourceMixin
|
||||||
|
|
||||||
@@ -419,3 +419,89 @@ class CamJamKitRobot(Robot):
|
|||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(CamJamKitRobot, self).__init__(left=(9, 10), right=(7, 8))
|
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
|
||||||
|
|||||||
@@ -610,3 +610,17 @@ class MCP3004(MCP3008):
|
|||||||
if not 0 <= channel < 4:
|
if not 0 <= channel < 4:
|
||||||
raise InputDeviceError('channel must be between 0 and 3')
|
raise InputDeviceError('channel must be between 0 and 3')
|
||||||
super(MCP3004, self).__init__(channel, device)
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user