######################################### test_hand_controller.py ###################################
#  
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'

import pyaudio      #This is to make a beep sound
# Open the stream required, mono mode only...
stream=pyaudio.PyAudio().open(format=pyaudio.paInt8,channels=1,rate=16000,output=True)
#If the line above gives an error, you need to install pyaudio.  It is available on
#Halverson's web site in the Python Program Files area

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

controller = u3.U3() # Opens the first LabJackU3 found on the USB
u3setup(controller,['din','din','ain','ain','dout','dout','dout','dout'])
sleep(0.25)

# Hand controller variables
deadband_V=0.1   # This is the half width of the "deadband" inside which there is no motion
center_V = 2.44/2.0
activeband_V= 2.44/2.0 - deadband_V  #Width of active bands around the deadband

while True:
    #  Device 2: Controller built by Boris Bonillo ======================================
    #  LED code added by Mr. Cervantes
    elevation_controller_V=ain2(controller)       #Read analog inputs
    azimuth_controller_V=ain3(controller)
    switch0=din0(controller)
    switch1=din1(controller)
    print "Hand control:",switch1," ",switch0," ",azimuth_controller_V," ",elevation_controller_V
    # Add code here to turn on and off LEDs for warning motor is moving.
    if elevation_controller_V > (center_V + deadband_V):
      #Turn on LED to say elevation motor is moving up
      dout6(controller,True)
    else:
      #Turn off LED to say elevation motor is NOT moving up
      dout6(controller,False)
    if elevation_controller_V < (center_V - deadband_V):
       #Turn on LED to say elevation motor is moving down
      dout7(controller,True)
    else:
      #Turn off LED to say elevation motor is NOT moving down
      dout7(controller,False) 
    if azimuth_controller_V > (center_V + deadband_V):
       #Turn on LED to say azimuth motor is moving clockwise
      dout4(controller,True)
    else:
      #Turn off LED to say azimuth motor is NOT moving clockwise
      dout4(controller,False)
    if azimuth_controller_V < (center_V - deadband_V):
       #Turn on LED to say azimuth motor is moving count-clockwise
      dout5(controller,True)
    else:
      #Turn on LED to say azimuth motor is NOT moving count-clockwise
      dout5(controller,False) 
    sleep(0.25)
 