Skip to content

Commit

Permalink
talking to teensy from pi3
Browse files Browse the repository at this point in the history
  • Loading branch information
dkavanagh committed Sep 8, 2017
1 parent 628d04d commit 549b3f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
38 changes: 29 additions & 9 deletions pybowieconsole/server.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import json
import serial
import struct
import _thread
import time

from flask import Flask, g
from flask import request, render_template, send_from_directory

TEENSY_DEVICE = '/dev/ttyACM0'


app = Flask(__name__, static_folder='static', static_url_path='')

@app.teardown_appcontext
def close_serial(exception):
fd = getattr(g, 'serial_fd', None)
if fd is not None:
fd.close()
#print('closing serial port !!!!!!!!!!!!!!!!!!!!!!!')
#if fd is not None:
# fd.close()

def init_port():
g.serial_fd = open('/dev/ttyACM0', 'rb+')
g.serial_fd = serial.Serial(TEENSY_DEVICE)
print("setting up logger")
def logserial(port):
while True:
data = port.read()
print("Logger "+":".join("{:02x}".format(ord(c)) for c in data))
#g.logger = thread.start_new_thread(logserial, (g.serial_fd,))
data = port.readline()
print(data)
time.sleep(1)
#print("Logger "+":".join("{:02x}".format(c) for c in data))
g.logger = _thread.start_new_thread(logserial, (g.serial_fd,))


def write_command(action, cmd1, key1, value1, cmd2, key2, value2):
Expand All @@ -32,15 +40,27 @@ def write_command(action, cmd1, key1, value1, cmd2, key2, value2):


def _write_command(port, action, cmd1, key1, value1, cmd2, key2, value2):
data = struct.pack('<ccBcHccBcHc', action.encode(), cmd1.encode(), key1, ',', value1, ',', cmd2.encode(), key2, ',', value2, '!')
data = struct.pack('<ccBcHccBcHc',
action.encode('ascii'),
cmd1.encode('ascii'),
key1,
','.encode('ascii'),
value1,
','.encode('ascii'),
cmd2.encode('ascii'),
key2,
','.encode('ascii'),
value2,
'!'.encode('ascii')
)
print("data len "+str(len(data)))
print(":".join("{:02x}".format(ord(c)) for c in data))
print(":".join("{:02x}".format(c) for c in data))
port.write(data)


@app.route('/bowieaction', methods=['POST'])
def bowie_action():
req = json.loads(request.data)
req = json.loads(str(request.data, 'utf-8'))
write_command(req['action'], req['cmd1'], int(req['key1']), int(req['val1']),
req['cmd2'], int(req['key2']), int(req['val2']))
return '{"status": "ok"}'
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@


requires = [
'flask',
'python3-flask',
'python3-serial'
]

setup(
Expand Down

0 comments on commit 549b3f5

Please sign in to comment.