Skip to content

Commit

Permalink
Working on the firmata stuff ...
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzoic committed Aug 17, 2016
1 parent f2090ee commit 742f34f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
4 changes: 3 additions & 1 deletion py/main.py
Expand Up @@ -69,7 +69,8 @@ def web_server_worker(sck):
req_content_length = int(req_headers.get(b'content-length',0))
req_body = b''
while len(req_body) < req_content_length:
req_body += sck.read(req_content_length - len(req_body))
x = sck.read(req_content_length - len(req_body))
if x: req_body += x
yield

http_status, res_headers, res_body = handle_request(
Expand Down Expand Up @@ -165,6 +166,7 @@ def executor():
yield

def loop():
mbot.initialize()
tasks = [
web_server('0.0.0.0', 80),
mbot.talker(prog_loc),
Expand Down
44 changes: 28 additions & 16 deletions py/mbot.py
@@ -1,23 +1,28 @@
try:
import serial
port = serial.Serial("/dev/ttyUSB0", baudrate=115200, timeout=0)
except ImportError:
import machine
port = machine.UART(0, 115200)
except ImportError:
import serial
port = serial.Serial("/dev/ttyUSB0", baudrate=115200, timeout=0)

import time

time.sleep(5)

port.write(bytearray([
0xF4, 0x04, 0x01, # pin 4 = digital out (motor 2 dir)
0xF4, 0x05, 0x03, # pin 5 = pwm out (motor 2 pwm)
0xF4, 0x06, 0x03, # pin 6 = pwm out (motor 1 pwm)
0xF4, 0x07, 0x01, # pin 7 = digital out (motor 1 dir)
0xF4, 0x09, 0x00, # pin 9 = digital in (line left)
0xF4, 0x0A, 0x00, # pin 10 = digital in (line right)
0xD1, 0x01, # enable reporting of pins
]))
def initialize():
time.sleep(5)
buf = bytearray([
0xC6, 0x01, # Enable reporting of ambient light pin
0xF4, 0x09, 0x00, # pin 9 = digital in (line left)
0xF4, 0x0A, 0x00, # pin 10 = digital in (line right)
0xD1, 0x01, # enable reporting of digital pins
0xF4, 0x04, 0x01, # pin 4 = digital out (motor 2 dir)
0xF4, 0x05, 0x03, # pin 5 = pwm out (motor 2 pwm)
0xF4, 0x06, 0x03, # pin 6 = pwm out (motor 1 pwm)
0xF4, 0x07, 0x01, # pin 7 = digital out (motor 1 dir)
])
while buf:
x = port.write(buf)
buf = buf[x:]

def listener(vlocals):
buf = b""
Expand All @@ -31,12 +36,12 @@ def listener(vlocals):
if buf:
if buf[0] == '\x91':
if len(buf) >= 3:
vlocals["line_left"] = ord(buf[1]) & 0x02
vlocals["line_right"] = ord(buf[1]) & 0x04
vlocals["line_left"] = ord(buf[1]) & 0x02 != 0
vlocals["line_right"] = ord(buf[1]) & 0x04 != 0
buf = buf[3:]
elif buf[0] == '\xe6':
if len(buf) >= 3:
vlocals["ambient"] = ord(buf[2]) << 7 + ord(buf[1])
vlocals["ambient"] = ord(buf[2]) * 128 + ord(buf[1])
buf = buf[3:]
else:
buf = buf[1:]
Expand All @@ -60,3 +65,10 @@ def talker(vlocals):
x = port.write(buf)
buf = buf[x:]
yield

if __name__ == "__main__":
v = {}
l = listener(v)
while True:
l.send(None)
print(repr(v))
4 changes: 4 additions & 0 deletions py/upload.py
Expand Up @@ -6,6 +6,8 @@

port = serial.Serial("/dev/ttyUSB0", 115200)

port.write(b'\x03')

for fn in argv[1:]:
fh = open(fn, "r")

Expand All @@ -19,4 +21,6 @@

port.write('_fh.close()\r')

port.write(b'\x04')

port.close()
22 changes: 12 additions & 10 deletions py/www/flobot.js
Expand Up @@ -503,29 +503,31 @@ window.onload = function () {
}

Prog.prototype.poll = function() {
ajax_post('http://10.107.1.33/prog', this.upload_callback.bind(this));
ajax_post('http://10.107.1.33/prog', '', this.upload_callback.bind(this));
}

Prog.prototype.upload_callback = function (status, text) {
if (status == 200) {
//this.update(text);
this.update(text);
if (poll_timer) clearTimeout(poll_timer);
poll_timer = setTimeout(this.poll.bind(this), 1000);
} else {
document.getElementById('return').textContent = status + "\n\n" + text;
if (poll_timer) clearTimeout(poll_timer);
}
var s = this.serialize() + "\n\n" + (status == 200 ? text : status);
document.getElementById('debug').textContent = s;
}

Prog.prototype.update = function (text) {
var ports_dict = {};
var pp = text.split(/\s+/);
for (var i=0; i<pp.length; i+=2) {
var val = parseInt(pp[i+1], 16);
if (val > 0x7FFF) val -= 0x10000;
ports_dict[1*pp[i]] = val / 100;
var ports_dict = JSON.parse(text);

var s = "";
for (var k in ports_dict) {
if (ports_dict.hasOwnProperty(k)) {
s += k + " = " + ports_dict[k] + "\n";
}
}
document.getElementById('return').textContent = s;

this.nodes.forEach(function (n) {
n.output_ports.forEach(function (p) {
if (p.port_id) p.show_value(ports_dict[p.port_id] || 0);
Expand Down
4 changes: 4 additions & 0 deletions py/www/index.html
Expand Up @@ -13,8 +13,12 @@
<li>Ports at each end must be the same type (shape)</li>
<li>Cycles are not allowed.</li>
</ul>
<hr/>
<span>Compiled python:</span>
<pre id="debug"></pre>
<hr/>
<span>Response:</span>
<pre id="return"></pre>
</div>
</body>
</html>

0 comments on commit 742f34f

Please sign in to comment.