#! /usr/bin/env python3

import sys, time
from time import sleep
from quick2wire.parts.pcf8591 import *
from quick2wire.i2c import I2CMaster
#from vis import volt_plot
from writefile import *
from tkinter import *

address = int(sys.argv[1]) if len(sys.argv) > 1 else BASE_ADDRESS
pin_index1 = int(sys.argv[2]) if len(sys.argv) > 2 else 0
pin_index2 = int(sys.argv[3]) if len(sys.argv) > 3 else 1
create_file('test')
root = Tk()
subvocal = ''

def key(event):
  if event.char == 'a':
    subvocal = 'Y'
    voltage = pin1.value * 3.3
    print("read: {} : {}".format(count, voltage))
    current = time.clock() - start
    write_file('test', current, voltage, subvocal)
  else:
    subvocal = "N"
    voltage = pin1.value * 3.3
    print("read: {} : {}".format(count, voltage))
    current = time.clock() - start
    write_file('test', current, voltage, subvocal)

with I2CMaster() as i2c:
    adc = PCF8591(i2c, THREE_DIFFERENTIAL)
    pin1 = adc.differential_input(1)
    #pin2 = adc.single_ended_input(pin_index2)

    count = 1
    #data = {}
    start = time.clock()

    frame = Frame(root, width=100, height=100)
    frame.bind("<Key>", key)
    frame.pack()
    root.mainloop()


    # while True:
    #     voltage = pin1.value * 3.3
    #     print("read: {} : {}".format(count, voltage))
    #     #sleep(0.1)
    #     count += 1
    #     current = time.clock() - start
    #
    #     write_file('test', current, voltage, )
    #     #data[count] = pin1.raw_value
    #     #volt_plot(count, data)

@@ -437,16 +437,173 @@
 l = ''%0A%0A
+%0Awith I2CMaster() as i2c:%0A    adc = PCF8591(i2c, THREE_DIFFERENTIAL)%0A    pin1 = adc.differential_input(1)%0A    #pin2 = adc.single_ended_input(pin_index2)%0A    
 def key(
@@ -610,16 +610,20 @@
 event):%0A
+    
   if eve
@@ -638,16 +638,20 @@
 == 'a':%0A
+    
     subv
@@ -661,24 +661,28 @@
 l = 'Y'%0A    
+   
+ 
 voltage = pi
@@ -688,32 +688,36 @@
 in1.value * 3.3%0A
+    
     print(%22read:
@@ -746,32 +746,36 @@
 , voltage))%0A    
+ 
+   
 current = time.c
@@ -785,32 +785,36 @@
 k() - start%0A    
+  
+  
 write_file('test
@@ -848,16 +848,20 @@
 al)%0A
+    
   else:%0A
     
@@ -856,16 +856,20 @@
   else:%0A
+    
     subv
@@ -879,24 +879,28 @@
 l = %22N%22%0A    
+  
+  
 voltage = pi
@@ -910,24 +910,28 @@
 value * 3.3%0A
+    
     print(%22r
@@ -964,24 +964,28 @@
 , voltage))%0A
+    
     current 
@@ -1007,24 +1007,28 @@
 - start%0A    
+ 
+   
 write_file('
@@ -1067,161 +1067,8 @@
 l)%0A%0A
-with I2CMaster() as i2c:%0A    adc = PCF8591(i2c, THREE_DIFFERENTIAL)%0A    pin1 = adc.differential_input(1)%0A    #pin2 = adc.single_ended_input(pin_index2)%0A%0A
     

Move 'key' function inside I2C loop