Commit a8dad347 authored by Carl Hetherington's avatar Carl Hetherington

Add IR sensor support.

parent 76f316f1
......@@ -10,24 +10,42 @@ if len(sys.argv) != 2:
sys.exit(1)
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def respond(self, reply):
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
self.wfile.write(reply)
self.wfile.close()
def find_sensor(self, driver_name):
for s in range(0, 4):
dir = '/sys/class/lego-sensor/sensor%d' % s
f = open('%s/driver_name' % dir, 'r')
if f.readline().strip() == driver_name:
return dir
return None
def do_GET(self):
parts = self.path[1:].split('/')
print parts
if len(parts) == 4 and parts[0] == 'motor':
print 'Run %s to %s at speed %s' % (parts[1], parts[2], parts[3])
d = MediumMotor(parts[1])
d.run_position_limited(int(parts[2]), int(parts[3]), absolute=False)
d.run_position_limited(int(parts[2]), int(parts[3]), absolute=False)
self.respond('OK')
elif len(parts) == 2 and parts[0] == 'say':
phrase = parts[1]
phrase = phrase.replace('%20', ' ')
print 'Say %s' % phrase
os.system('espeak -a 200 -s 130 -v en-sc --stdout "%s" | aplay' % phrase)
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write('OK')
self.wfile.close()
self.respond('OK')
elif len(parts) == 2 and parts[0] == 'ir-sensor' and parts[1] == 'proximity':
dir = self.find_sensor('lego-ev3-ir')
f = open('%s/value0' % dir, 'r')
v = f.readline().strip()
f.close()
self.respond(v)
class TCPServer(SocketServer.TCPServer):
def server_bind(self):
......
......@@ -25,11 +25,14 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.copyfile(f, self.wfile)
f.close()
else:
urllib2.urlopen("http://10.42.0.51:%d%s" % (EV3_PORT, self.path), timeout=5).read()
response = urllib2.urlopen("http://10.42.0.51:%d%s" % (EV3_PORT, self.path), timeout=5).read()
print '%s -> %s' % (self.path, response)
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Length', str(len(response)))
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write('OK')
self.wfile.write(response)
self.wfile.close()
class TCPServer(SocketServer.TCPServer):
......
......@@ -47,4 +47,15 @@
</block>
</script>
</block-definition>
<block-definition category="sensing" s="IR sensor proximity" type="reporter">
<inputs></inputs>
<script>
<block s="doReport">
<block s="reportURL">
<l>localhost:1330/ir-sensor/proximity</l>
</block>
</block>
</script>
<watcher s="IR sensor proximity" style="normal"/>
</block-definition>
</blocks>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment