Skip to content

Instantly share code, notes, and snippets.

@brianpeiris
Last active January 1, 2016 10:59
Show Gist options
  • Save brianpeiris/8134970 to your computer and use it in GitHub Desktop.
Save brianpeiris/8134970 to your computer and use it in GitHub Desktop.
/*
Get acceleration data from Chronos watch.
Taken from info posted at: http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/32714.aspx
Based off of the latest Python code on the Chronos wiki that I wrote.
Copyright (c) 2010 Sean Brewer
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
If you want you may contact me at [email protected]
or on reddit: seabre
Modications made by Oliver Smith to provide graphics
http://chemicaloliver.net
*/
import processing.serial.*;
//Horizontal coordinate for graphics
int xPos = 1;
//Take what we know about the packets for starting the access point
//and put it in its integer representation
int startAccessPointNum[] = {255, 7, 3};
//Take what we know about the packets for aquiring data from the chronos
//and put it in its integer representation
int accDataRequestNum[] = {255, 8, 7, 0, 0, 0, 0};
//Convert it all to bytes so that watch will understand
//what we're talking about..
byte startAccessPoint[] = byte(startAccessPointNum);
byte accDataRequest[] = byte(accDataRequestNum);
// We want to talk to the chronos...
Serial chronos;
void setup() {
//Draw window
size(400, 900, P3D);
//In linux /dev/ttyACM0 is not correctly detected by java so
//setting this property solves that
//You'll need to change this if you are using osx/windows or
//another serial port
System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");
chronos = new Serial(this, "/dev/ttyACM0", 115200);
//Start the access point..
chronos.write(startAccessPoint);
//Until the port is still availible...
//Send data request to chronos.
chronos.write(accDataRequest);
background(0);
}
float lx = 0;
float ly = 0;
float lz = 0;
void draw() {
if (chronos.available() >= 0) {
//Accelerometer data is 7 bytes long. This looks really lame, but it works.
int[] buf = new int[7];
for (int i = 0; i < 7; i++) {
buf[i] = chronos.read();
}
//Fourth byte indicates if there are coordinates. If the byte is 0xFF (255)
//Then there is no data. If it is 1, then data is valid.
//Sometimes the datatype comes back as other values...not sure if that
//means anything or not.
//Also, the Python version of this code ALWAYS returns 0xFF (255)..
//Don't know why that is.
if (buf[3] == 1) {
//println("Datatype: " + str(buf[3]));
//println ("x: " + str(buf[4]) + " y: " + str(buf[5]) + " z: " + str(buf[6]));
float x = parseFloat(str(buf[4]));
float y = parseFloat(str(buf[6]));
float z = parseFloat(str(buf[5]));
float _x = x;
x = (lx + x) / 2;
lx = _x;
float _y = y;
y = (ly + y) / 2;
ly = _y;
float _z = z;
z = (lz + z) / 2;
lz = _z;
float xa = map(x, 0, 255, TWO_PI, 0);
float ya = map(y, 0, 255, 0, TWO_PI);
float za = map(z, 0, 255, TWO_PI, 0);
//draw x line
int offset = 400;
float lh = (height - offset) / 3;
float xl = map(x, 0, 255, 0, lh);
stroke(255, 0, 0);
line(xPos, offset + lh, xPos, offset + lh - xl);
//draw y line
float yl = map(y, 0, 255, 0, lh);
stroke(0, 255, 0);
line(xPos, offset + lh * 2 , xPos, offset + lh * 2 - yl);
//draw z line
float zl = map(z, 0, 255, 0, lh);
stroke(0, 0, 255);
line(xPos, offset + lh * 3, xPos, offset + lh * 3 - zl);
//if xpos has reached end of window start again at 0
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
rect(0, 0, 400, 400);
translate(200, 250, 200);
rotateX(xa);
rotateY(ya);
rotateZ(za);
box(150);
}
else {
chronos.write(accDataRequest);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment