#!/usr/bin/env python
# coding: Latin-1

# Load library functions we want
import sys
import time

# Settings for the Replay recorder
interval = 0.01                         # Number of seconds between updates, smaller makes a more accurate recording but recordings are larger

# Get user input
if len(sys.argv) > 1:
    recordPath = sys.argv[1]
else:
    # No file specified, print usage details and exit
    print 'No file specified for recording, usage:'
    print '%s fileToRecord' % (sys.argv[0])
    sys.exit()

# Open the file for recording
recordFile = open(recordPath, 'w')
print 'Recording to "' + recordPath + '"'
print 'Press CTRL+C to end recording'

# Function to read the LedBorg colour
def GetLedColour():
    LedBorg = open('/dev/ledborg', 'r')     # Open the LedBorg device for reading from
    colour = LedBorg.read()                 # Read the colour string from the LedBorg device
    LedBorg.close()                         # Close the LedBorg device
    return colour                           # Return the read colour

try:
    # Write the recorded speed at the start of the recording
    recordFile.write('%f\n' % (interval))
    # Loop indefinitely
    while True:
        # Get the colour of the LedBorg
        colour = GetLedColour()
        # Write the colour to file
        recordFile.write(colour + '\n')     # '\n' is a new line, without this all entries would use the same line 
        # Wait for interval
        time.sleep(interval)
except KeyboardInterrupt:
    # CTRL+C exit, stop recording and close the file
    recordFile.close()
    print 'Recording stopped'