Commit a8dad347 authored by Carl Hetherington's avatar Carl Hetherington

Add IR sensor support.

parent 76f316f1
...@@ -10,6 +10,22 @@ if len(sys.argv) != 2: ...@@ -10,6 +10,22 @@ if len(sys.argv) != 2:
sys.exit(1) sys.exit(1)
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): 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): def do_GET(self):
parts = self.path[1:].split('/') parts = self.path[1:].split('/')
print parts print parts
...@@ -17,17 +33,19 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): ...@@ -17,17 +33,19 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
print 'Run %s to %s at speed %s' % (parts[1], parts[2], parts[3]) print 'Run %s to %s at speed %s' % (parts[1], parts[2], parts[3])
d = MediumMotor(parts[1]) 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': elif len(parts) == 2 and parts[0] == 'say':
phrase = parts[1] phrase = parts[1]
phrase = phrase.replace('%20', ' ') phrase = phrase.replace('%20', ' ')
print 'Say %s' % phrase print 'Say %s' % phrase
os.system('espeak -a 200 -s 130 -v en-sc --stdout "%s" | aplay' % phrase) os.system('espeak -a 200 -s 130 -v en-sc --stdout "%s" | aplay' % phrase)
self.respond('OK')
self.send_response(200) elif len(parts) == 2 and parts[0] == 'ir-sensor' and parts[1] == 'proximity':
self.send_header('Content-Type', 'text/html') dir = self.find_sensor('lego-ev3-ir')
self.end_headers() f = open('%s/value0' % dir, 'r')
self.wfile.write('OK') v = f.readline().strip()
self.wfile.close() f.close()
self.respond(v)
class TCPServer(SocketServer.TCPServer): class TCPServer(SocketServer.TCPServer):
def server_bind(self): def server_bind(self):
......
...@@ -25,11 +25,14 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): ...@@ -25,11 +25,14 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.copyfile(f, self.wfile) self.copyfile(f, self.wfile)
f.close() f.close()
else: 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_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.end_headers()
self.wfile.write('OK') self.wfile.write(response)
self.wfile.close() self.wfile.close()
class TCPServer(SocketServer.TCPServer): class TCPServer(SocketServer.TCPServer):
......
...@@ -47,4 +47,15 @@ ...@@ -47,4 +47,15 @@
</block> </block>
</script> </script>
</block-definition> </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> </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