machine.py 4.25 KB
Newer Older
Hugo Hornquist's avatar
Hugo Hornquist committed
1 2 3 4 5
#!/usr/bin/env python2

import RPi.GPIO as g
import time
import threading
6
import sys
Hugo Hornquist's avatar
Hugo Hornquist committed
7

Hugo Hörnquist's avatar
Hugo Hörnquist committed
8 9
import Adafruit_CharLCD as LCD

Hugo Hornquist's avatar
Hugo Hornquist committed
10 11 12 13 14 15 16 17 18 19
# 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)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
pins = [ 17, 27, 22, 10, 9, 11, 5, 6, 13, 19 ]
btn_pin = 4

test = False

lcd_rs = 12
lcd_en = 7
lcd_d4 = 8
lcd_d5 = 25
lcd_d6 = 24
lcd_d7 = 23

lcd_backlight = 2

# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2

lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

risewait = threading.Event()
fallwait = threading.Event()
Hugo Hornquist's avatar
Hugo Hornquist committed
42 43 44 45

def callback(pin):
    state = g.input(pin)
    if state == g.LOW:
Hugo Hörnquist's avatar
Hugo Hörnquist committed
46
        print "falling edge"
Hugo Hornquist's avatar
Hugo Hornquist committed
47 48
        fallwait.set()
    else:
Hugo Hörnquist's avatar
Hugo Hörnquist committed
49
        print "rising edge"
Hugo Hornquist's avatar
Hugo Hornquist committed
50 51 52
        risewait.set()

g.setup(btn_pin, g.IN, pull_up_down=g.PUD_DOWN)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
53
g.setup(3, g.IN, pull_up_down=g.PUD_DOWN)
Hugo Hornquist's avatar
Hugo Hornquist committed
54 55 56 57 58
for pin in pins:
    g.setup(pin, g.IN, pull_up_down=g.PUD_DOWN)
    g.add_event_detect(pin, g.BOTH, callback=callback)

def bottle_check(pins):
Hugo Hörnquist's avatar
Hugo Hörnquist committed
59 60 61 62
    if test:
        return any([g.input(pin) for pin in pins])
    else:
        return not all([g.input(pin) for pin in pins])
Hugo Hornquist's avatar
Hugo Hornquist committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

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():
Hugo Hörnquist's avatar
Hugo Hörnquist committed
94
    return wait_rise() if test else wait_fall()
Hugo Hornquist's avatar
Hugo Hornquist committed
95 96

def wait_for_bottle_remove():
Hugo Hörnquist's avatar
Hugo Hörnquist committed
97 98
    return wait_fall() if test else wait_rise()

Hugo Hörnquist's avatar
Hugo Hörnquist committed
99
def lcd_info(msg, last_time):
Hugo Hörnquist's avatar
Hugo Hörnquist committed
100 101 102 103 104 105 106 107 108 109 110
    lcd.clear()
    lcd.home()
    lcd.set_cursor(0, 0)
    lcd.message("Tid: {0} s".format(last_time))
    lcd.set_cursor(0, 1)
    lcd.message(msg)

def wait_btn():
    return g.wait_for_edge(btn_pin, g.RISING, bouncetime=500)
    #g.wait_for_edge(btn_pin, g.RISING)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
111 112 113 114
class Modes:
    button = 0
    flask_raise = 1

115 116
def main():
    global state, pins
Hugo Hörnquist's avatar
Hugo Hörnquist committed
117
    last_time = 0
118
    mode = Modes.button if (sys.argv + [False])[1] else Modes.flask_raise
Hugo Hörnquist's avatar
Hugo Hörnquist committed
119 120 121 122
    while True:
        if state == States.no_hafv:
            print
            print "Inget Hafv"
Hugo Hörnquist's avatar
Hugo Hörnquist committed
123
            lcd_info("Vilar", last_time)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
124 125 126 127 128
            wait_for_bottle_place()
            state = States.hafv_ready

        elif state == States.hafv_ready:
            print "Hafv redo"
Hugo Hörnquist's avatar
Hugo Hörnquist committed
129 130 131 132 133 134 135
            if mode == Modes.button:
                lcd_info("Starta med knapp", last_time)
                wait_btn()
            elif mode == Modes.flask_raise:
                lcd_info("Starta med lyft", last_time)
                wait_for_bottle_remove()

Hugo Hörnquist's avatar
Hugo Hörnquist committed
136 137 138 139 140
            state = States.hafv_active
            start_time = time_ms()

        elif state == States.hafv_active:
            print "currently Hafving"
Hugo Hörnquist's avatar
Hugo Hörnquist committed
141
            lcd_info("Klockar", last_time)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
142 143

            if mode == Mode.button and not bottle_check(pins):
Hugo Hörnquist's avatar
Hugo Hörnquist committed
144
                print "Early start"
Hugo Hörnquist's avatar
Hugo Hörnquist committed
145
                lcd_info("Tjuvstart!", last_time)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
146 147 148
                wait_btn()
                state = States.no_hafv
                continue
Hugo Hörnquist's avatar
Hugo Hörnquist committed
149

Hugo Hörnquist's avatar
Hugo Hörnquist committed
150
            print "Raise the bottle"
Hugo Hörnquist's avatar
Hugo Hörnquist committed
151 152 153 154 155 156 157

            # TODO we actually don't need this, but it was
            # here before so it stays until I refactor this.
            if mode == Modes.button:
                wait_for_bottle_remove()
                print "BOTTLE RAISED"

Hugo Hörnquist's avatar
Hugo Hörnquist committed
158 159
            print "Place back bottle after hafv"

Hugo Hörnquist's avatar
Hugo Hörnquist committed
160 161 162 163 164 165 166 167 168 169 170 171
            # Wait for bottle placed is in a loop because if the time
            # is under 1 secound then we want to wait again. This is
            # to prevent a finger or similar from stopping the time.
            while True:
                wait_for_bottle_place()
                print "BOTTLE PLACED"

                end_time = time_ms()
                t = (end_time - start_time) / 1000.0
                if t > 1:
                    # Allow timing to halt
                    break
Hugo Hörnquist's avatar
Hugo Hörnquist committed
172 173 174

            print "Hafvtid: {0} s".format(t)
            last_time = t
Hugo Hörnquist's avatar
Hugo Hörnquist committed
175
            lcd_info("Avvakta", last_time)
Hugo Hornquist's avatar
Hugo Hornquist committed
176
            state = States.no_hafv
Hugo Hörnquist's avatar
Hugo Hörnquist committed
177
            time.sleep(1)
178 179 180

if __name__ == "__main__":
    main()