/
Snap-NXT.py
165 lines (160 loc) · 5.37 KB
/
Snap-NXT.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
157
158
159
160
161
162
163
164
#Snap-NXT by Technoboy10
import SimpleHTTPServer
class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
path = self.path
print path
ospath = os.path.abspath('')
if 'move' in path:
regex = re.compile("\/move([abc])0([0-9]+)0([0-9]+)0([+-])")
m = regex.match(path)
if m.group(4) == '-':
power = -1 * int(m.group(2))
elif m.group(4) == "+":
power = int(m.group(2))
degrees = int(m.group(3))
motor = m.group(1)
if motor == "a":
m_a.turn(power, degrees)
elif motor == "b":
m_b.turn(power, degrees)
if motor == "c":
m_c.turn(power, degrees)
elif 'tone' in path:
regex = re.compile('\/tone([0-9]{1,})a([0-9]{1,})')
m = regex.match(path)
tone = int(m.group(1))
time = int(m.group(2))
print time
print tone
b.play_tone_and_wait(tone, time)
elif 'illuminate' in path:
regex = re.compile('\/illuminate(on|off)')
m = regex.match(path)
setl = m.group(1)
if setl == 'on':
l.set_illuminated(True)
elif setl == 'off':
l.set_illuminated(False)
elif path == '/nxttouch': #touch
f = open(ospath + '/nxtreturn', 'w+')
f.write(str(t.get_sample()))
f.close()
f = open(ospath + '/nxtreturn', 'rb')
ctype = self.guess_type(ospath + '/nxtreturn')
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
elif path == '/nxtsound': #sound
f = open(ospath + '/nxtreturn', 'w+')
f.write(str(s.get_sample()))
f.close()
f = open(ospath + '/nxtreturn', 'rb')
ctype = self.guess_type(ospath + '/nxtreturn')
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
elif path == '/nxtlight': #light
f = open(ospath + '/nxtreturn', 'w+')
f.write(str(l.get_sample()))
f.close()
f = open(ospath + '/nxtreturn', 'rb')
ctype = self.guess_type(ospath + '/nxtreturn')
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
elif path == '/nxtultrasonic': #ultrasonic
f = open(ospath + '/nxtreturn', 'w+')
f.write(str(u.get_sample()))
f.close()
f = open(ospath + '/nxtreturn', 'rb')
ctype = self.guess_type(ospath + '/nxtreturn')
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
elif path == '/nxtbattery': #battery level
f = open(ospath + '/nxtreturn', 'w+')
f.write(str(b.get_battery_level()))
f.close()
f = open(ospath + '/nxtreturn', 'rb')
ctype = self.guess_type(ospath + '/nxtreturn')
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
elif path == '/Snap-NXT':
f = open(ospath + '/Snap-NXT.xml', 'rb')
ctype = self.guess_type(ospath + '/nxtreturn')
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f
if __name__ == "__main__":
print "Snap-NXT by Technoboy10"
import re
import os
import SocketServer
import nxt.bluesock
import nxt.locator
from nxt.motor import *
from nxt.sensor import *
try:
b = nxt.locator.find_one_brick()
except:
print "NXT brick not found. Please connect an NXT brick and try again."
quit()
try:
t = Touch(b, PORT_1)
except:
pass
try:
s = Sound(b, PORT_2)
except:
pass
try:
l = Light(b, PORT_3)
except:
pass
try:
u = Ultrasonic(b, PORT_4)
except:
pass
m_a = Motor(b, PORT_A)
m_b = Motor(b, PORT_B)
m_c = Motor(b, PORT_C)
PORT = 1330 #N reminded me of 13, X in Roman numerals is 10, and T is the 20th letter. I added X and T together.
Handler = CORSHTTPRequestHandler
#Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
print "Go ahead and launch Snap!."
print "<a>http://snap.berkeley.edu/snapsource/snap.html#open:http://localhost:1330/Snap-NXT</a>"
httpd.serve_forever()