Skip to content
Snippets Groups Projects
Select Git revision
  • 94057a6ebf93166533d7422d038e33899ea35077
  • master default
  • 0.1
3 results

test.py

Blame
  • test.py 2.15 KiB
    #!/usr/bin/env python2
    
    import RPi.GPIO as g
    import time
    import threading
    
    # For lazer array
    # if no bottle present then all are high
    # if bottle at least one low
    
    # For regular button
    # Down means bottle
    # up means no bottle
    
    g.setmode(g.BCM)
    
    pins = [ 17 ]
    btn_pin = 22
    
    def callback(pin):
        state = g.input(pin)
        if state == g.LOW:
            fallwait.set()
        else:
            risewait.set()
    
    g.setup(btn_pin, g.IN, pull_up_down=g.PUD_DOWN)
    for pin in pins:
        g.setup(pin, g.IN, pull_up_down=g.PUD_DOWN)
        g.add_event_detect(pin, g.BOTH, callback=callback)
    
    risewait = threading.Event()
    fallwait = threading.Event()
    
    def bottle_check(pins):
        return not all([g.input(pin) for pin in pins])
    
    class States:
        no_hafv = 0
        hafv_ready = 1
        hafv_active = 2
    
    has_bottle = bottle_check(pins)
    
    state = States.no_hafv
    start_time = 0
    end_time = 0
    
    def time_ms():
        t = int(time.time() * 1000)
        # print "Time: ", t
        return t
    
    if has_bottle:
        state = States.hafv_ready
    
    def wait_rise():
        risewait.clear()
        risewait.wait()
    
    def wait_fall():
        fallwait.clear()
        fallwait.wait()
    
    # should these be the other way around?
    
    def wait_for_bottle_place():
        #return wait_rise()
        return wait_fall()
    
    def wait_for_bottle_remove():
        #return wait_fall()
        return wait_rise()
    
    while True:
        if state == States.no_hafv:
            print
            print "Inget Hafv"
            wait_for_bottle_place()
            state = States.hafv_ready
    
        elif state == States.hafv_ready:
            print "Hafv redo"
            out = g.wait_for_edge(btn_pin, g.RISING)
            state = States.hafv_active
            start_time = time_ms()
    
        elif state == States.hafv_active:
            print "currently Hafving"
            if not bottle_check(pins):
                print "Early start"
                state = States.no_hafv
                continue
            print "Raise the bottle"
            wait_for_bottle_remove()
            print "BOTTLE RAISED"
            print "Place back bottle after hafv"
            wait_for_bottle_place()
            print "BOTTLE PLACED"
    
            end_time = time_ms()
    
            t = (end_time - start_time) / 1000.0
            print "Hafvtid: {0} s".format(t)
            state = States.no_hafv
            time.sleep(1)