#!/usr/bin/python

import spidev
import time
import os
import math
from datetime import datetime
ref_volt = 3.311

spi = spidev.SpiDev()
spi.open(0,0)

def ReadChannel(channel):
    adc = spi.xfer2([1,(8+channel) << 4,0])
    data = ((adc[1]&3)<<8) +adc[2]
    return data

def ConvertVolts(data,places):
    volts = (data*ref_volt)/float(1023)
    volts = round(volts,places)
    return volts

channel = 0
count = 0
delay = 1

f = open('ProbeToPlot.csv', 'w')

while count < 10:
    start_time = datetime.now()
    
    NUMBER_OF_SAMPLES = 1000
    for n in range (0, NUMBER_OF_SAMPLES):
        i = ReadChannel(channel)
        v = ConvertVolts(i, 9)
        
        f.write("{},".format(v))

    f.write("\n")
    
    stoptime = datetime.now()
    print("Probe time : {} ms ".format((stoptime-start_time).microseconds/1000))
    
    time.sleep(delay)
    count = count + 1 

f.close()

