Skip to content
Snippets Groups Projects
Select Git revision
  • 017b3a59bb451d98612fde9c94c84fd95155adfa
  • master default protected
  • 9.0
  • 8.0
  • nt-tools
  • 7.8
  • 7.6
  • 7.4
  • 7.2
  • 7.0
  • 0.6
  • rosuav/latex-markdown-renderer
  • rxnpatch/rxnpatch
  • marcus/gobject-introspection
  • rxnpatch/8.0
  • rosuav/pre-listening-ports
  • rosuav/async-annotations
  • rosuav/pgsql-ssl
  • rxnpatch/rxnpatch-broken/2023-10-06T094250
  • grubba/fdlib
  • grubba/wip/sakura/8.0
  • v8.0.2020
  • v8.0.2018
  • v8.0.2016
  • v8.0.2014
  • v8.0.2012
  • v8.0.2008
  • v8.0.2006
  • v8.0.2004
  • v8.0.2002
  • v8.0.2000
  • v8.0.1998
  • v8.0.1996
  • v8.0.1994
  • v8.0.1992
  • v8.0.1990
  • v8.0.1988
  • v8.0.1986
  • rxnpatch/clusters/8.0/2025-04-29T124414
  • rxnpatch/2025-04-29T124414
  • v8.0.1984
41 results

builtin_functions.c

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)