#!/usr/bin/env python
# Copyright (c) 2015, Intel Corporation

# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of Intel Corporation nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Created by Jose Humberto Garza Rosado

#modified by Joe McManus josephmc@alumni.cmu.edu 
#used to monitor bike tire pressure. 

from Spark_ADC import Adc
import time
import os
import argparse

parser = argparse.ArgumentParser(description='Simple LED Blinker for Galileo')
parser.add_argument('psi', help="Specify the psir, i.e. 0-13", type=int)
parser.add_argument('--version', action='version',version='%(prog)s 0.2')
args=parser.parse_args()
psi=args.psi


ain0_operational_status = 0b0
ain0_input_multiplexer_configuration = 0b100
ain0_programmable_gain_amplifier_configuration = 0b010
ain0_device_operating_mode = 0b0
ain0_data_rate = 0b100
ain0_comparator_mode = 0b0
ain0_compulator_polarity = 0b0
ain0_latching_comparator = 0b0
ain0_comparator_queue_and_disable = 0b11

pt1 = Adc()
pt1.set_config_command(
	ain0_operational_status, 
	ain0_input_multiplexer_configuration, 
	ain0_programmable_gain_amplifier_configuration, 
	ain0_device_operating_mode, 
	ain0_data_rate, 
	ain0_comparator_mode, 
	ain0_compulator_polarity, 
	ain0_latching_comparator,
	ain0_comparator_queue_and_disable
)

os.system("i2cset -y 1 0x48 1 0x8344 w")
fileName="tireLog-"+ str(psi) +".log"
fh=open(fileName, 'a')
while True:
	adcReading=pt1.adc_read()
	currentTime=time.strftime("%Y/%m/%d %H:%M:%S")	
	csvLine=(str(str(currentTime) + " , " + str(adcReading) + "\n"))
	print(csvLine)
	fh.write(csvLine)
	time.sleep(0.5)


