-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should we provide composite classes for more devices? #522
Comments
Related/unrelated: I guess "board" is really the wrong word for this anyway... LEDBoard was intended for commonality between add-on boards like pi-liter, but used more generally for a collection of LEDs. |
|
@lurch I've Been thinking more about composite devices today. Looking at the
the value property is simply an n-tuple as per However, as I suggested in #628, I see scenarios where you want to have a single value for the device. Imagine a composite motion sensor where the value was the average value of the sensors: class CompositeMotionSensor(CompositeDevice):
def __init__(self, *pins):
sensors = (MotionSensor(pin) for pin in pins)
super(CompositeMotionSensor, self).__init__(*sensors)
@property
def value(self):
return sum(d.value for d in self) / len(self)
>>> pirs = CompositeMotionSensor(4, 5)
>>> pirs.value
0.0 # both inactive
>>> pirs.value
0.5 # one active
>>> pirs.value
1.0 # both active Rather than the value Also you can pass this value into, say, an graph.source = pirs It occurs to me that the "2 buttons AND gate LED" recipe I refer to a lot could be solved with composite device too: class AndGate(CompositeDevice):
def __init__(self, pin1, pin2):
super(AndGate, self).__init__(Button(pin1), Button(pin2))
@property
def value(self):
return all(btn.value for btn in self)
>>> led = LED(2)
>>> and_gate = AndGate(3, 4)
>>> led.source = and_gate As an alternative to using the Just putting some thoughts down. Need to revisit later. |
Just out of curiosity, could you provide a practical example of where that'd be useful? |
If you had a number of motion sensors and wanted and an identical number of LEDs you could (currently) pair each sensor with an LED to indicate motion per sensor. If you had a number of motion sensors and, say, one buzzer, and you wanted the buzzer to go off if any of the sensors were triggered, you could do this with multiple sensors with the same Also see #760 (comment) |
We have
LEDBoard
andButtonBoard
. But what aboutMotionSensorBoard
? It sounds silly, but I just created an example where I wanted an LED for each Motion Sensor, and I can either create aLEDBoard
and a list ofMotionSensor
s (inconsistent):or I can just use lists for consistency (not making use of
LEDBoard
):Would it be worth creating more composite devices for single devices like this? When would it be worth it / not worth it? LightSensor? Energenie? MCP3008? I can imagine uses for them all. What's special about LED and Button?
The text was updated successfully, but these errors were encountered: