######################################### ezu3test_two_devices.py ###################################
#  Test of Halverson's attempt at an easy interface to the LabJack U3 for student use.
#  This is the 2nd version.  The 1st version was called easyU3test.py     2/1/2015

import sys
mypath=sys.path[0]
if mypath.find('my_python') == -1:       #If true, then "my_python" could not be found
  print "Sorry.  You need to be in my_python for Halverson's program to work."
  print "(That''s because I am assuming that the tgraphlix.py and ezu3.py libraries are there.)"
  exit()
sys.path[0]=mypath[0:mypath.find('my_python')]+'my_python'

print "Use ^C to kill this program"
import u3
from ezu3_two_devices import *
from time import sleep


n_of_devices=u3.deviceCount(3)
print "Number of Labjack devices=",n_of_devices
devdict = u3.openAllU3()
device_number = 1
for i in devdict.items():
  print "Device #",device_number," has serial number ",i[0]
  device_number=device_number+1

print "If the program crashes now, its because you need to put the right serial numbers in the code."
print "The serial numbers must match the ones just printed out."

d1=devdict['320063848']
d2=devdict['320051371']
u3setup(d1,['ain','ain','din','din','dout','dout','dout','dout'])
u3setup(d2,['ain','ain','din','din','dout','dout','dout','dout'])
blink_state=True
while True:
   #  Device 1 ==================================================
   V0_1=ain0(d1)       #Read analog inputs
   V1_1=ain1(d1)       #Analog inputs range from 0 to 2.4 Volts
   B2_1=din2(d1)      #Read digital inputs
   B3_1=din3(d1)
   s=format(V0_1,"4.2f")               #Print the value of analog input 0
   print "Dev 1 ain0=%s|" % s ,
   for i in range(0,int(5*V0_1)):      #Print a bunch of spaces then a "0"
      print " ",
   print "0",
   for i in range(0,13-int(5*V0_1)):   #Pad with blanks
      print " ",
   s=format(V1_1,"4.2f")               #Print the value of analog input 1
   print "|ain1=%s|" % s ,
   for i in range(0,int(5*V1_1)):      #Print a bunch of spaces then a "1"
      print " ",
   print "1",
   for i in range(0,13-int(5*V1_1)):   #Pad with blanks
      print " ",
   print "|",
   if not B2_1:
      print "2   ",
   else:
      print "  2 ",
   if not B3_1:
      print "3  ",
   else:
      print "  3",
   Temp_1=temperatureF(d1)   
   s=format(Temp_1,"5.2f")               #Print the value of analog input 0
   print "|Temp=%s F" % s
   #Just for fun add blinking
   LED(d1,blink_state)
   dout4(d1,blink_state)
   dout5(d1,not blink_state)
   dout6(d1,blink_state)
   dout7(d1,not blink_state)
   blink_state = not blink_state
   dac0out(d1,V0_1*2)                 #Send analog input 0 to DAC 0 after multiplying by 2
   dac1out(d1,V1_1*2)                 #Send analog input 1 to DAC 1 after multiplying by 2
   
   #  Device 2 ==================================================
   V0_2=ain0(d2)       #Read analog inputs
   V1_2=ain1(d2)       #Analog inputs range from 0 to 2.4 Volts
   B2_2=din2(d2)      #Read digital inputs
   B3_2=din3(d2)
   s=format(V0_2,"4.2f")               #Print the value of analog input 0
   print "Dev 2 ain0=%s|" % s ,
   for i in range(0,int(5*V0_2)):      #Print a bunch of spaces then a "0"
      print " ",
   print "0",
   for i in range(0,13-int(5*V0_2)):   #Pad with blanks
      print " ",
   s=format(V1_2,"4.2f")               #Print the value of analog input 1
   print "|ain1=%s|" % s ,
   for i in range(0,int(5*V1_2)):      #Print a bunch of spaces then a "1"
      print " ",
   print "1",
   for i in range(0,13-int(5*V1_2)):   #Pad with blanks
      print " ",
   print "|",
   if not B2_2:
      print "2   ",
   else:
      print "  2 ",
   if not B3_2:
      print "3  ",
   else:
      print "  3",
   Temp_2=temperatureF(d2)   
   s=format(Temp_2,"5.2f")               #Print the value of analog input 0
   print "|Temp=%s F" % s
   #Just for fun add blinking
   LED(d2,blink_state)
   dout4(d2,blink_state)
   dout5(d2,not blink_state)
   dout6(d2,blink_state)
   dout7(d2,not blink_state)
   blink_state = not blink_state
   dac0out(d2,V0_2*2)                 #Send analog input 0 to DAC 0 after multiplying by 2
   dac1out(d2,V1_2*2)                 #Send analog input 1 to DAC 1 after multiplying by 2

   
   
   
   sleep(0.25)                     #Wait for 1/4 second before looping again




