Skip to content
Permalink
Browse files

fix trigger logic

  • Loading branch information...
root
root committed Dec 27, 2013
1 parent 619a1fa commit d752ab6af6dea3cb862e526da26ce6086b2617e7
Showing with 53 additions and 13 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 rpitc/io/switch.py
  3. +24 −8 rpitc/io/trigger.py
  4. +27 −4 tests/test_trigger.py
@@ -36,3 +36,4 @@ nosetests.xml
.pydevproject

*.swp
*.swo
@@ -28,4 +28,4 @@ def toggle(self):

def _trigger(self):
for out in self.out:
out.trigger()
out.toggle()
@@ -3,15 +3,31 @@
import threading
import time

class Trigger(Out):
class Trigger(object):

def __init__(self, pin, status=IO.OFF, delay=0.2):
Out.__init__(self, pin)
def __init__(self, pin, status=IO.OFF, trigger_on=IO.ON, delay=0.2):
self.out = Out(pin, status)
self.status = status
self.trigger_on = trigger_on
self.delay = delay

def trigger(self):
thread = TriggerThread(self)
thread.start()
def on(self):
if self.status==IO.OFF and self.trigger_on==IO.ON:
thread = TriggerThread(self)
thread.start()
self.status = IO.ON

def off(self):
if self.status==IO.ON and self.trigger_on==IO.OFF:
thread = TriggerThread(self)
thread.start()
self.status = IO.OFF

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


class TriggerThread(threading.Thread):
@@ -21,7 +37,7 @@ def __init__(self, trigger):
self.trigger = trigger

def run(self):
self.trigger.toggle()
self.trigger.out.toggle()
time.sleep(self.trigger.delay)
self.trigger.toggle()
self.trigger.out.toggle()

@@ -6,16 +6,39 @@ class TestTrigger:

def setup_method(self, method):
self.trigger = Trigger(7, delay=1, status=IO.OFF)
self.trigger2 = Trigger(5, status=IO.ON, trigger_on=IO.OFF)

def teardown_method(self, method):
self.trigger.off()
self.trigger.out.off()
self.trigger2.out.off()

def test_init(self):
assert isinstance(self.trigger, Trigger)

def test_trigger(self):
self.trigger.trigger()
def test_trigger_on(self):
self.trigger.off()
self.trigger.on()
assert self.trigger.status == IO.ON
assert self.trigger.out.status == IO.ON
time.sleep(self.trigger.delay + 0.1)
assert self.trigger.status == IO.OFF
assert self.trigger.status == IO.ON
assert self.trigger.out.status == IO.OFF

def test_trigger_not_on(self):
self.trigger2.off()
time.sleep(self.trigger2.delay + 0.1)
self.trigger2.on()
assert self.trigger2.status == IO.ON
assert self.trigger2.out.status == IO.ON
time.sleep(self.trigger2.delay + 0.1)
assert self.trigger2.status == IO.ON
assert self.trigger2.out.status == IO.ON

def test_trigger_off(self):
self.trigger2.on()
self.trigger2.off()
assert self.trigger2.status == IO.OFF
assert self.trigger2.out.status == IO.OFF
time.sleep(self.trigger2.delay + 0.1)
assert self.trigger2.status == IO.OFF
assert self.trigger2.out.status == IO.ON

0 comments on commit d752ab6

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