import serial
import struct
import pygame

ser = serial.Serial('/dev/ttyACM0', 9600)

ser.write("1")      # write a string
pygame.init()
#pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=3675)
#sound = pygame.mixer.Sound('saturn.wav')
f = open('data', 'w')
print "test"
time =  0

i = 0 

while i < 2000:						#simply a limit on the while loop because I haven't figured out how to use keystrokes in Python
  
    data = ser.read(6)
    #sound.play()
    delay = struct.unpack('<i', data[:4])[0]           #reads an integer (here 4 bytes)
    power = struct.unpack('<h', data[4:])[0]	       #reads a short (two bytes)
    #print str(delay) + ' ' +  str(power)	       
    f.write(str(power) + ' ' + str(delay) + '\n')      #prints the data to a file to log.
    
    i = i + 1
    

ser.write("0")					       #tell the motor to stop. (otherwise I just unplug the battery)
ser.close()             # close port
f.close()


