traffic.py
1: #!/usr/bin/python
2: ##########################################################
3: # * Python code for Traffic Signal Simulation using
4: # * Beaglebone Black running Debian 7 Linux distribution
5: ##########################################################
6: # * Developed by MicroEmbedded Technologies
7: ##########################################################
8: import sys
9: import time
10: from gpio import *
11: from ledpins import *
12: def lightExit (gpio):
13: gpioSetVal(gpio, val="0")
14: gpioUnexport(gpio)
15: return
16: def lightInit (gpio):
17: gpioExport(gpio)
18: gpioSetDir(gpio, flag="out")
19: gpioSetVal(gpio, val="0")
20: return
21: def lightOn (gpio):
22: gpioSetVal(gpio, val="1")
23: return
24: def lightOff (gpio):
25: gpioSetVal(gpio, val="0")
26: return
27: def trafficInitAll():
28: lightInit(str(NORTH_GREEN))
29: lightInit(str(NORTH_YELLOW))
30: lightInit(str(NORTH_RED))
31: lightInit(str(EAST_GREEN))
32: lightInit(str(EAST_YELLOW))
33: lightInit(str(EAST_RED))
34: lightInit(str(SOUTH_GREEN))
35: lightInit(str(SOUTH_YELLOW))
36: lightInit(str(SOUTH_RED))
37: lightInit(str(WEST_GREEN))
38: lightInit(str(WEST_YELLOW))
39: lightInit(str(WEST_RED))
40: return
41: def trafficExitAll():
42: lightExit(str(NORTH_GREEN))
43: lightExit(str(NORTH_YELLOW))
44: lightExit(str(NORTH_RED))
45: lightExit(str(EAST_GREEN))
46: lightExit(str(EAST_YELLOW))
47: lightExit(str(EAST_RED))
48: lightExit(str(SOUTH_GREEN))
49: lightExit(str(SOUTH_YELLOW))
50: lightExit(str(SOUTH_RED))
51: lightExit(str(WEST_GREEN))
52: lightExit(str(WEST_YELLOW))
53: lightExit(str(WEST_RED))
54: return
55: def northSouthOn():
56: lightOff(str(EAST_YELLOW))
57: lightOff(str(WEST_YELLOW))
58: lightOff(str(NORTH_RED))
59: lightOff(str(SOUTH_RED))
60: lightOn(str(EAST_RED))
61: lightOn(str(WEST_RED))
62: lightOn(str(NORTH_GREEN))
63: lightOn(str(SOUTH_GREEN))
64: time.sleep(10)
65: lightOff(str(NORTH_GREEN))
66: lightOff(str(SOUTH_GREEN))
67: lightOn(str(NORTH_YELLOW))
68: lightOn(str(SOUTH_YELLOW))
69: time.sleep(1)
70: return
71: def eastWestOn():
72: lightOff(str(NORTH_YELLOW))
73: lightOff(str(SOUTH_YELLOW))
74: lightOff(str(EAST_RED))
75: lightOff(str(WEST_RED))
76: lightOn(str(NORTH_RED))
77: lightOn(str(SOUTH_RED))
78: lightOn(str(EAST_GREEN))
79: lightOn(str(WEST_GREEN))
80: time.sleep(10)
81: lightOff(str(EAST_GREEN))
82: lightOff(str(WEST_GREEN))
83: lightOn(str(EAST_YELLOW))
84: lightOn(str(WEST_YELLOW))
85: time.sleep(1)
86: return
87: try:
88: print "\nTraffic Signal Light Simulation using Python"
89: print "-----------------------------------------------"
90: trafficExitAll()
91: trafficInitAll()
92: for x in range(0, 10):
93: print "\nNORTH-SOUTH --> [GO]"
94: print "EAST-WEST --> [STOP]\n"
95: northSouthOn()
96: time.sleep(1)
97: print "\nEAST-WEST --> [GO]"
98: print "NORTH-SOUTH --> [STOP]\n"
99: eastWestOn()
100: time.sleep(1)
101: trafficExitAll()
102: print "done"
103: exit()
104: except KeyboardInterrupt:
105: trafficExitAll()
106: print "Program Exit due to CTRL-C"
107: exit()
108: sys.exit(0)
traffic.py
1: ##########################################################
2: # * Python GPIO Functions for Traffic Signal Simulation
3: # * using Baglebone Black running Debian 7 Linux distribution
4: ##########################################################
5: # * Developed by MicroEmbedded Technologies
6: ##########################################################
7: import sys
8: import os
9: SYSFS_GPIO_DIR = "/sys/class/gpio"
10: def gpioUnexport (gpio):
11: try:
12: fo = open(SYSFS_GPIO_DIR + "/unexport","w")
13: fo.write(gpio)
14: fo.close()
15: return
16: except IOError:
17: return
18: def gpioExport (gpio):
19: try:
20: fo = open(SYSFS_GPIO_DIR + "/export","w")
21: fo.write(gpio)
22: fo.close()
23: return
24: except IOError:
25: return
26: def gpioSetDir (gpio, flag):
27: try:
28: fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/direction" ,"w")
29: fo.write(flag)
30: fo.close()
31: return
32: except IOError:
33: return
34: def gpioSetVal (gpio, val):
35: try:
36: fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/value" ,"w")
37: fo.write(val)
38: fo.close()
39: return
40: except IOError:
41: return
ledpins.py
1: ##########################################################
2: # * Python File for Traffic Signal Simulation Add-on Card
3: # * for GPIO PIN Configurations and Other Definitions
4: ##########################################################
5: # * Developed by MicroEmbedded Technologies
6: ##########################################################
7: LED_1 = (0 * 32) + 3
8: LED_2 = (0 * 32) + 23
9: LED_3 = (0 * 32) + 2
10: LED_4 = (0 * 32) + 26
11: LED_5 = (1 * 32) + 17
12: LED_6 = (1 * 32) + 15
13: LED_7 = (0 * 32) + 15
14: LED_8 = (1 * 32) + 14
15: LED_9 = (0 * 32) + 14
16: LED_10 = (0 * 32) + 27
17: LED_11 = (0 * 32) + 22
18: LED_12 = (2 * 32) + 1
19: NORTH_GREEN = LED_1
20: NORTH_YELLOW = LED_5
21: NORTH_RED = LED_9
22: EAST_GREEN = LED_2
23: EAST_YELLOW = LED_6
24: EAST_RED = LED_10
25: SOUTH_GREEN = LED_3
26: SOUTH_YELLOW = LED_7
27: SOUTH_RED = LED_11
28: WEST_GREEN = LED_4
29: WEST_YELLOW = LED_8
30: WEST_RED = LED_12
Tags:
PROGRAMMING