Skip to content
Permalink
Browse files

add classes for IO out

  • Loading branch information...
root
root committed Dec 27, 2013
1 parent 1659e22 commit 9d71f7d9669f33bc4d722934db3a8ab813e33093
Showing with 178 additions and 20 deletions.
  1. +2 −0 .gitignore
  2. +1 −3 RPi.TC.egg-info/SOURCES.txt
  3. +3 −0 rpitc/io/__init__.py
  4. +30 −1 rpitc/io/out.py
  5. +23 −0 rpitc/io/switch.py
  6. +27 −0 rpitc/io/trigger.py
  7. +35 −16 tests/test_out.py
  8. +34 −0 tests/test_switch.py
  9. +23 −0 tests/test_trigger.py
@@ -34,3 +34,5 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

*.swp
@@ -8,6 +8,4 @@ RPi.TC.egg-info/not-zip-safe
RPi.TC.egg-info/top_level.txt
rpitc/__init__.py
rpitc/io/__init__.py
rpitc/io/out.py
rpitc/tests/__init__.py
rpitc/tests/out_test.py
rpitc/io/out.py
@@ -0,0 +1,3 @@
class IO(object):
ON = True
OFF = False
@@ -1,2 +1,31 @@
from rpitc.io import IO
import RPi.GPIO as GPIO

class Out(object):
pass
status = IO.OFF
pin = 0

def __init__(self, pin, status=IO.OFF):
self.pin = pin
self.status = status
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.pin, GPIO.OUT)
if status==IO.ON:
self.on()

def on(self):
self.status = IO.ON
GPIO.output(self.pin, IO.ON)

def off(self):
self.status = IO.OFF
GPIO.output(self.pin, IO.OFF)

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

def trigger(self):
self.toggle()
@@ -0,0 +1,23 @@
from rpitc.io import IO

class Switch(object):

def __init__(self, out, status = IO.OFF):
self.out = out
self.status = status

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

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

def toggle(self):
if self.status==IO.OFF:
self.on()
else:
self.off()
@@ -0,0 +1,27 @@
from rpitc.io import IO
from rpitc.io.out import Out
import threading
import time

class Trigger(Out):

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

def trigger(self):
thread = TriggerThread(self)
thread.start()


class TriggerThread(threading.Thread):

def __init__(self, trigger):
threading.Thread.__init__(self)
self.trigger = trigger

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

@@ -1,19 +1,38 @@
#import unittest
#from rpitc.io.out import Out
#
#class OutTest(unittest.TestCase):
#
# def test_out(self):
# out = Out()
# self.assertIsInstance(out, Out)
#
#
#if __name__ == "__main__":
# unittest.main()

from rpitc.io import IO
from rpitc.io.out import Out
import RPi.GPIO as GPIO

class TestOut:
def test_init(self):
out = Out()
assert isinstance(out, Out)

def setup_method(self, method):
self.out = Out(7)

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

def test_init(self):
assert isinstance(self.out, Out)

def test_init_on(self):
out = Out(7, status=IO.ON)
assert out.status == IO.ON
out.off()

def test_on(self):
self.out.on()
assert self.out.status == IO.ON

def test_off(self):
self.out.off()
assert self.out.status == IO.OFF


def test_trigger(self):
self.out.off()

self.out.trigger()
assert self.out.status == IO.ON

self.out.trigger()
assert self.out.status == IO.OFF
@@ -0,0 +1,34 @@
from rpitc.io import IO
from rpitc.io.out import Out
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.switch = Switch(self.out, status=IO.OFF)

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

def test_init(self):
assert isinstance(self.switch, Switch)

def test_on(self):
self.switch.on()
assert self.out.status == IO.ON
assert self.switch.status == IO.ON

def test_off(self):
self.switch.off()
assert self.out.status == IO.OFF
assert self.switch.status == IO.OFF

def test_toggle(self):
self.switch.off()
self.switch.toggle()
assert self.out.status == IO.ON
assert self.switch.status == IO.ON

@@ -0,0 +1,23 @@
from rpitc.io import IO
from rpitc.io.trigger import Trigger
import RPi.GPIO as GPIO
import time

class TestTrigger:

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

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

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

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

0 comments on commit 9d71f7d

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