Skip to content

Instantly share code, notes, and snippets.

@marcdjulien
Last active August 29, 2015 14:20
Show Gist options
  • Save marcdjulien/bc4bc34e155b22804606 to your computer and use it in GitHub Desktop.
Save marcdjulien/bc4bc34e155b22804606 to your computer and use it in GitHub Desktop.
SoleMate - Arudio Code
const uint8_t BUF_LEN = 6;
/* Pin values */
int d0 = 0;
int d1 = 0;
int d2 = 0;
int d3 = 0;
int d4 = 0;
int d5 = 0;
unsigned long time = 0;
int val;
uint8_t buffer[BUF_LEN];
bool changed = false;
void setup()
{
Serial.begin(57600);
// Digital pins
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
buffer[0] = 0x48; //Magic number
}
/* Loops through reading the digital values and sends data when something changes */
void loop()
{
val = digitalRead(0);
if(val != d0)
send_data();
val = digitalRead(1);
if(val != d1)
send_data();
val = digitalRead(2);
if(val != d2)
send_data();
val = digitalRead(3);
if(val != d3)
send_data();
val = digitalRead(4);
if(val != d4)
send_data();
val = digitalRead(5);
if(val != d5)
send_data();
Bean.sleep(10);
}
void send_data()
{
buffer[1] = 0;
buffer[1] |= digitalRead(0);
buffer[1] |= (digitalRead(1) << 1);
buffer[1] |= (digitalRead(2) << 2);
buffer[1] |= (digitalRead(3) << 3);
buffer[1] |= (digitalRead(4) << 4);
buffer[1] |= (digitalRead(5) << 5);
time = millis();
buffer[2] = (time >> 24) & 0xFF;
buffer[3] = (time >> 16) & 0xFF;
buffer[4] = (time >> 8) & 0xFF;
buffer[5] = (time) & 0xFF;
Bean.setScratchData(1, buffer, BUF_LEN);
Serial.println("Data Written\n");
}
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import logging
from google.appengine.ext import ndb
import matplotlib.pyplot as plt
from google.appengine.ext import db
import numpy as np
TITLE = ["Upper Foot", "Toe", "L Middle", "R Middle", "Heel"]
MAIN_PAGE_FOOTER_TEMPLATE = """\
<html>
<body>
<form action="/" method="POST">
<input type="hidden" name="pt0" value="0">
<input type="hidden" name="pt1" value="0">
<input type="hidden" name="pt2" value="0">
<input type="hidden" name="pt3" value="0">
<input type="hidden" name="pt4" value="0">
<input type="hidden" name="time" value="0">
<input type="hidden" name="user" value="dname">
<input type="submit" value="Sign Guestbook">
</form>
</body>
</html>
"""
HTML_START =\
"""
<html>
<head>
"""
JAVA_START1 = \
"""
<script type="text/javascript"
src="https://www.google.com/jsapi?autoload={
'modules':[{
'name':'visualization',
'version':'1',
'packages':['corechart']
}]
}"></script>
<script type="text/javascript">
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
%s,
"""
JAVA_END1 = \
"""
]);
var options =
{
title: '%s',
curveType: 'none',
legend: { position: 'bottom' },
vAxis:
{
title: 'Time Down',
textStyle: {color: '#000000', fontSize: 12, bold: true}
},
hAxis:
{
title: 'Time',
textStyle: {color: '#000000', fontSize: 8, bold: false}
}
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
</script>
"""
JAVA_START2 = \
"""
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Point', 'Average Time'],
"""
JAVA_END2 = \
"""
]);
var options = {
title: 'Average Throughout Run'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
"""
JAVA3 = """\
<script type="text/javascript">
function draw()
{
var canvas = document.getElementById('footcanvas');
if (canvas.getContext)
{
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "rgb(0,0,0)";
ctx.fillStyle = "rgb(0, 0, 0)";
ctx.rect(25, 30, 18, 18);
ctx.rect(50, 20, 20, 20);
ctx.rect(75, 14, 23, 23);
ctx.stroke();
ctx.fillStyle = "rgb(0, %d, %d)";
ctx.fillRect (110, 10, 25, 25);
ctx.fillStyle = "rgb(0, %d, %d)";
ctx.fillRect(40, 60, 90, 50);
ctx.fillStyle = "rgb(0, %d, %d)";
ctx.fillRect(45, 120, 40, 40);
ctx.fillStyle = "rgb(0, %d, %d)";
ctx.fillRect(90, 120, 40, 40);
ctx.fillStyle = "rgb(0, %d, %d)";
ctx.fillRect(60, 170, 50, 40);
}
}
</script>
<style type="text/css">
canvas { border: 0px solid black; }
</style>
"""
HTML_END = """\
</head>
<body onload="draw();">
<table align="center" style="float:center; width: 100%%">
<tr>
<td colspan=4>
<div id="curve_chart" style="width: 100%%; height: 600px"></div>
</td>
</tr>
<tr>
<td style="width: 25%;"></td>
<td style="width: 25%;">
<canvas id="footcanvas" width="200px" height="300px"></canvas>
</td>
<td style="width: 25%;">
<div id="piechart" style="width: 600px; height: 300px;"></div>
</td>
<td style="width: 25%;"></td>
</tr>
</table>
<a href="/">Back</a>
</body>
</html>
"""
def footdata_key(user):
return ndb.Key('FootData', user or "nouser")
class FootData(ndb.Model):
time = ndb.IntegerProperty()
pt0 = ndb.IntegerProperty()
pt1 = ndb.IntegerProperty()
pt2 = ndb.IntegerProperty()
pt3 = ndb.IntegerProperty()
pt4 = ndb.IntegerProperty()
user = ndb.StringProperty()
class GraphHandler(webapp2.RequestHandler):
def get(self):
user = self.request.get("user")
footdata_query = FootData.query(ancestor=footdata_key(user)).filter(ndb.GenericProperty("user") == user)
footdata_query = footdata_query.order(FootData.time)
footdata = footdata_query.fetch()
pt0 = [];
pt1 = [];
pt2 = [];
pt3 = [];
pt4 = [];
# Add html code headers
HTML = HTML_START
# Add javascript code headers
tl = ["Time"]
tl.extend(TITLE)
HTML += JAVA_START1%(str(tl))
# Calculate data and add to graph list
first_data = footdata[0]
i = 0
for data in footdata:
i+=1
pt0.append(data.pt0/1000.0)
pt1.append(data.pt1/1000.0)
pt2.append(data.pt2/1000.0)
pt3.append(data.pt3/1000.0)
pt4.append(data.pt4/1000.0)
HTML += "['%d', %6.4f, %6.4f, %6.4f, %6.4f, %6.4f],"%(i, pt0[-1],pt1[-1],pt2[-1],pt3[-1],pt4[-1])
HTML = HTML[0:-1]
# Add java script code footers
HTML += JAVA_END1%(user)
# Add java script code header
HTML += JAVA_START2
pt0 = np.array(pt0)
pt1 = np.array(pt1)
pt2 = np.array(pt2)
pt3 = np.array(pt3)
pt4 = np.array(pt4)
# Add data
HTML += "['%s', %6.4f],"%(TITLE[0], np.mean(pt0))
HTML += "['%s', %6.4f],"%(TITLE[1], np.mean(pt1))
HTML += "['%s', %6.4f],"%(TITLE[2], np.mean(pt2))
HTML += "['%s', %6.4f],"%(TITLE[3], np.mean(pt3))
HTML += "['%s', %6.4f]"%(TITLE[4], np.mean(pt4))
# Add footers
HTML += JAVA_END2
# Foot drawing
total = np.mean(pt0)+np.mean(pt1)+np.mean(pt2)+np.mean(pt3)+np.mean(pt4);
m0 = int(255*np.mean(pt1)/total)
m1 = int(255*np.mean(pt0)/total)
m2 = int(255*np.mean(pt2)/total)
m3 = int(255*np.mean(pt3)/total)
m4 = int(255*np.mean(pt4)/total)
HTML += JAVA3%(m0, m0, m1, m1, m2, m2, m3, m3, m4, m4)
HTML += HTML_END
idx = np.arange(len(pt0))
"""
xlabels = ["Toe", "L Mid", "R Mid", "L Back", "Heel"]
points = [pt0, pt1, pt2, pt3, pt4]
subplot = [321, 322, 323, 324, 325]
plt.subplots(1,2,figsize=(20,15))
for i in xrange(5):
plt.subplot(subplot[i])
plt.xlabel(xlabels[i])
plt.ylabel("Time Down (ms)")
plt.plot(idx, points[i]/1000.0)
"""
# Send image
#self.response.headers['Content-Type'] = 'image/png'
#plt.savefig(self.response.out)
self.response.write(HTML)
class MainHandler(webapp2.RequestHandler):
def get(self):
# Get logs from DB
query_set = FootData.query(projection=["user"], distinct=True)
logs = [data.user for data in query_set]
# Construct html code
HTML_CODE = "<html><body><h1>Running Log Database</h1>"
for log in logs:
HTML_CODE += "<a style='font-size: 20px; text-decoration: none;' href='/graph?user=%s'>%s</a><br>"%(log,log)
HTML_CODE += "</body></html>"
# Send code
self.response.write(HTML_CODE)
logging.info(self.request)
def post(self):
try:
user = self.request.get('user');
new_data = FootData(parent=footdata_key(user))
new_data.pt0 = int(self.request.get('pt0'))
new_data.pt1 = int(self.request.get('pt1'))
new_data.pt2 = int(self.request.get('pt2'))
new_data.pt3 = int(self.request.get('pt3'))
new_data.pt4 = int(self.request.get('pt4'))
new_data.user = user
new_data.time = int(self.request.get('time'))
new_data.put()
logging.info("Success")
except Exception as e:
logging.info("Error storing")
logging.info(str(e))
logging.info(type(e))
logging.info(self.request)
self.response.write("Succesfully recorded sensor data\n")
app = webapp2.WSGIApplication([
('/', MainHandler),('/graph', GraphHandler) ], debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment