#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

#x, y, and z values may not be "in order". The first three bytes from the packet

#of data sent from the watch seemed to be different than what was listed

#on the page, though the datatype byte was in the correct place. So YMMV.

#

#Written by Sean Brewer (seabre)

#seabre986@gmail.com

#





import serial

import array

import csv



#Open a CSV file for writing

accelcsvfile = csv.writer(open('acceldata.csv', 'w'))



def startAccessPoint():

    return array.array('B', [0xFF, 0x07, 0x03]).tostring()



def accDataRequest():

    return array.array('B', [0xFF, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00]).tostring()



#Open COM port 6 (check your system info to see which port

#yours is actually on.)

#argments are 5 (COM6), 115200 (bit rate), and timeout is set so

#the serial read function won't loop forever.

# ser = serial.Serial(5,115200,timeout=1)

ser = serial.Serial("/dev/ttyACM0",115200,timeout=1)



#Start access point

ser.write(startAccessPoint())



#Write the top row so you know what everything is.

accelcsvfile.writerow(["x","y","z", "M1", "M2", "S1"])



while True:

    #Send request for acceleration data

    ser.write(accDataRequest())

    accel = ser.read(7)

    M1 = 0

    M2 = 0

    S1 = 0

    unknown = 1 # indicate that the received data is of unkown type



    if len(accel) != 7:

        continue

    if ord(accel[6]) == 1 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:

	# bogus data?

        unknown = 0

        continue

    if ord(accel[6]) == 17 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:

	print "M1 pressed"

	M1 = 1

        unknown = 0

    if ord(accel[6]) == 33 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:

	print "M2 pressed"

	M2 = 1

        unknown = 0

    if ord(accel[6]) == 49 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:

	print "S1 pressed"

	S1 = 1

        unknown = 0

    if ord(accel[6]) == 255 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:

        # accelerometer data, but it is bogus

        unknown = 0

        continue

    if ord(accel[6]) == 255 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255:

        print "Accelerometer data: x: " + str(ord(accel[2])) + "\t y: " + str(ord(accel[1])) + "\t z: " + str(ord(accel[0]))

        unknown = 0



    if unknown == 1:

        print "Unknown data: 6: " + str(ord(accel[6])) + "\t5: " + str(ord(accel[5])) + "\t4: " + str(ord(accel[4])) + "\t3: " + str(ord(accel[3])) + "\t2: " + str(ord(accel[2])) + "\t1: " + str(ord(accel[1])) + "\t0: " + str(ord(accel[0]))

        continue



    #This actually writes the data to the CSV file.

    accelcsvfile.writerow([str(ord(accel[2])),str(ord(accel[1])),str(ord(accel[0])),str(M1),str(M2),str(S1)])

ser.close()
