Skip to content
Permalink
Browse files

make switch able to handle multiple outs

  • Loading branch information...
root
root committed Dec 27, 2013
1 parent 9d71f7d commit 52df2d71a8a9c985efe21702ff4a9b017a738981
Showing with 23 additions and 6 deletions.
  1. +11 −3 rpitc/io/switch.py
  2. +12 −3 tests/test_switch.py
@@ -1,23 +1,31 @@
from rpitc.io import IO
from collections import Iterable

class Switch(object):

def __init__(self, out, status = IO.OFF):
self.out = out
if isinstance(out, Iterable):
self.out = out
else:
self.out = [out]
self.status = status

def on(self):
if self.status==IO.OFF:
self.status = IO.ON
self.out.trigger()
self._trigger()

def off(self):
if self.status==IO.ON:
self.status = IO.OFF
self.out.trigger()
self._trigger()

def toggle(self):
if self.status==IO.OFF:
self.on()
else:
self.off()

def _trigger(self):
for out in self.out:
out.trigger()
@@ -1,17 +1,18 @@
from rpitc.io import IO
from rpitc.io.out import Out
from rpitc.io.trigger import Trigger
from rpitc.io.switch import Switch
import RPi.GPIO as GPIO

class TestSwitch:

def setup_method(self, method):
self.out = Out(7, status=IO.OFF)
self.trigger = Trigger(5)
self.switch = Switch(self.out, status=IO.OFF)

def teardown_method(self, method):
GPIO.output(self.out.pin, GPIO.LOW)
GPIO.cleanup()
self.out.off()
self.trigger.off()

def test_init(self):
assert isinstance(self.switch, Switch)
@@ -32,3 +33,11 @@ def test_toggle(self):
assert self.out.status == IO.ON
assert self.switch.status == IO.ON

def test_multiple_out(self):
self.out.off()
self.switch = Switch([self.out, self.trigger])
self.switch.toggle()
assert self.switch.status == IO.ON
assert self.out.status == IO.ON
assert self.trigger.status == IO.ON

0 comments on commit 52df2d7

Please sign in to comment.
You can’t perform that action at this time.