-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (65 loc) · 2.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
#!/usr/bin/python
"""
DESCRIPTION
Script designed to run a CH-924 Coin Counter and provide information to LCD Display
as well as a web UI
EXAMPLES
python main.py
EXIT STATUS
Does not exit unless exception or user terminated
AUTHOR
TODO: Thomas Herr <tom@tlherr.com>
LICENSE
Read LICENCE
VERSION
$0.1$
"""
import sys, os, traceback, optparse, signal
import RPi.GPIO as GPIO
import time
import threading
from CoinMachineManager import CoinMachineManager
from LightManager import LightManager
from LcdManager import LcdManager
from HTTPServerManager import HTTPServerManager
def main():
# The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug the numbers printed on the board (e.g. P1)
# The GPIO.BCM option means that you are referring to the pins by the "Broadcom SOC channel" number,
print("Setting GPIO Mode: {0} on board type: {1}".format(GPIO.BCM, GPIO.RPI_REVISION))
GPIO.setmode(GPIO.BCM)
# Initialize our Classes, each manager runs its own loop in its own thread
lcd = LcdManager()
lights = LightManager(lcd)
coin_machine = CoinMachineManager(lcd, lights)
http_server = HTTPServerManager(coin_machine, lights)
lcd_thread = threading.Thread(target=lcd.run_screen, args=())
lcd_thread.daemon = True
lcd_thread.start()
light_thread = threading.Thread(target=lights.run_lights, args=())
light_thread.daemon = True
light_thread.start()
httpd_thread = threading.Thread(target=http_server.start_server, args=())
httpd_thread.daemon = True
httpd_thread.start()
coin_thread = threading.Thread(target=coin_machine.run_machine, args=())
coin_thread.daemon = True
coin_thread.start()
print("All Threads Running. Application Ready")
# Keep the main thread "alive", while stuff is done on the others
while True:
time.sleep(1)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt, e: # Ctrl-C
GPIO.cleanup()
raise e
except SystemExit, e: # sys.exit()
GPIO.cleanup()
raise e
except Exception, e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
GPIO.cleanup()
traceback.print_exc()
os._exit(1)