-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lit Dash.py
169 lines (123 loc) · 3.15 KB
/
Lit Dash.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# DragonBoard Code
# gpio example in python using mraa
#
# Author: Manivannan Sadhasivam <[email protected]>
# Modified by: Team 3 UCSD HARD Hacks 2018
# Usage: Toggles GPIO 23 and 24
#
# Execution: sudo python mraa_gpio.py
import json
import urllib2
#import urllib.parse
#import urllib.request
import mraa
import time
# ========================SET-UP=========================
BASE_URL = "http://proxoshop.com:3001/directions"
# Left LED Block
L1_led = mraa.Gpio(23)
L2_led = mraa.Gpio(24)
L3_led = mraa.Gpio(27)
# Right LED Block
R1_led = mraa.Gpio(26)
R2_led = mraa.Gpio(25)
R3_led = mraa.Gpio(30)
# U-turn LED Block
U1_led = mraa.Gpio(29)
U2_led = mraa.Gpio(32)
U3_led = mraa.Gpio(31)
# set LEFT LED to output
L1_led.dir(mraa.DIR_OUT)
L2_led.dir(mraa.DIR_OUT)
L3_led.dir(mraa.DIR_OUT)
# set right_led to output
R1_led.dir(mraa.DIR_OUT)
R2_led.dir(mraa.DIR_OUT)
R3_led.dir(mraa.DIR_OUT)
# set u_led to output
U1_led.dir(mraa.DIR_OUT)
U2_led.dir(mraa.DIR_OUT)
U3_led.dir(mraa.DIR_OUT)
def light_up(dir_num):
"""
This function light's up certain LED blocks based on the direction number:
- nothing: 0
- left: 1
- right: 2
- u-turn: 3
"""
all_off()
if dir_num == 1:
L1_led.write(1)
elif dir_num == 2:
L1_led.write(1)
L2_led.write(1)
elif dir_num == 3:
L1_led.write(1)
L2_led.write(1)
L3_led.write(1)
elif dir_num == 4:
R1_led.write(1)
elif dir_num == 5:
R1_led.write(1)
R2_led.write(1)
elif dir_num == 6:
R1_led.write(1)
R2_led.write(1)
R3_led.write(1)
elif dir_num == 7:
U1_led.write(1)
elif dir_num == 8:
U1_led.write(1)
U2_led.write(1)
elif dir_num == 9:
U1_led.write(1)
U2_led.write(1)
U3_led.write(1)
elif dir_num == 10:
L1_led.write(1)
U1_led.write(1)
elif dir_num == 11:
L1_led.write(1)
U1_led.write(1)
L2_led.write(1)
U2_led.write(1)
elif dir_num == 12:
L1_led.write(1)
U1_led.write(1)
L2_led.write(1)
U2_led.write(1)
L3_led.write(1)
U3_led.write(1)
def all_off():
"""Turns off all the leds"""
L1_led.write(0)
L2_led.write(0)
L3_led.write(0)
R1_led.write(0)
R2_led.write(0)
R3_led.write(0)
U1_led.write(0)
U2_led.write(0)
U3_led.write(0)
def get_result(url):
response = None
try:
response = urllib2.urlopen(url)
json_text = response.read().decode(encoding = 'utf-8')
return json.loads(json_text)
finally:
if response != None:
response.close()
if __name__ == "__main__":
# place_holder = 0
while True:
recent_result = get_result(BASE_URL)
direction = recent_result[0]["direction"]
# if the direction is -1, break out of the while loop
if direction == -1:
break
# else, call light_up function passing in the direction
light_up(direction)
# print("alrighty, i got the result. here it is:", recent_result)
# input("stall") #for stalling the while loop