(set aserver-name "yimou.media.mit.edu")
(set vserver-name "ray-harryhausen.media.mit.edu")

(set mousescale 1)

#############################
# Initialize Gyropoint port #
#############################

(set port (open-input "/dev/ttyd2"))
(set-tty-mode port 0 0 (| tty-cs7 tty-cread tty-clocal tty-hupcl) 0 tty-b1200 tty-b1200)

(set display-byte
  (proc (inint)
    (+ (* (bit-right (bit-and 1 inint) 0)   1)
       (* (bit-right (bit-and 2 inint) 1)   10)
       (* (bit-right (bit-and 4 inint) 2)   100)
       (* (bit-right (bit-and 8 inint) 3)   1000)
       (* (bit-right (bit-and 16 inint) 4)  10000)
       (* (bit-right (bit-and 32 inint) 5)  100000)
       (* (bit-right (bit-and 64 inint) 6)  1000000)
       (* (bit-right (bit-and 128 inint) 7) 10000000))))


#######################################
# Open TCP connection to audio server #
#######################################

(print "opening to audio server" newline)

(set aserv (tcp-open aserver-name 3663))

(print "opening to video server" newline)

(set vservx (tcp-open vserver-name 3670))
(set vservy (tcp-open vserver-name 3671))
(set vservz (tcp-open vserver-name 3663))

(print "connected to fish server")



#########################################
# Initialize virtual screen coordinates #
#########################################

(set xpos 240)
(set ypos 320)
(set zpos 0)

(set counter 0)

###################
# Main event loop #
###################

(while True
  (begin
    (set oldzpos zpos)
    (set oldxpos xpos)
    (set oldypos ypos)
    (set zpos (- (real zpos) 0.1))


    (set buffer1 (malloc 1))
    (set buffer2 (malloc 1))
    (set buffer3 (malloc 1))

    (if (pending port)
	(begin
	  (read-raw port 1 buffer1)
	  (sleep 0.01)
	  (if (pending port)
	      (begin

		(read-raw port 1 buffer2)
		(sleep 0.01)
		(if (pending port)
		    (begin
		      (read-raw port 1 buffer3)
		      
		      (set byte1 (peek c-byte buffer1))
		      (set byte2 (peek c-byte buffer2))
		      (set byte3 (peek c-byte buffer3))
	  
		      (set compx (bit-or (bit-and 63 byte2) 
					 (bit-left (bit-and 1 byte1) 6)))
		      (set compy (bit-or (bit-and 63 byte3) 
					 (bit-left (bit-and 4 byte1) 4)))
		      
		      (if (= (bit-right (bit-and 2 byte1) 1) 1)
			  (set compx (- compx 128)))
		      (if (= (bit-right (bit-and 8 byte1) 3) 1)
			  (set compy (- compy 128)))

		      (set mouse1 (bit-right (bit-and 32 byte1) 5))
		      (set mouse2 (bit-right (bit-and 16 byte1) 4))

		      (set xpos (+ xpos (* mousescale compx)))
		      (set ypos (+ ypos (* mousescale compy)))
		      (set zpos (+ zpos (* 0.2 mousescale (+ (abs compx) 
							     (abs compy)))))

		      (if (< xpos 0) (set xpos 0))
		      (if (> xpos 1280) (set xpos 1280))
		      (if (< ypos 0) (set ypos 0))
		      (if (> ypos 1024) (set ypos 1024))

		      )
		    (print "No third byte" newline)
		    )
		)
	      (print "No second byte" newline)
	      )
	  )
	)
    

    (if (< zpos 0) (set zpos 0))
    (if (> zpos 255) (set zpos 255))



    (print 
     #" byte1: " (display-byte byte1) 
     #" byte2: " (display-byte byte2)
     #" byte3: " (display-byte byte3) 
     #" compx: " compx
     #" compy: " compy
     #" butn1: " mouse1
     #" butn2: " mouse2 
     " xpos: " xpos
     " ypos: " ypos
     " zpos: " zpos
     newline)

    (set counter (+ counter 1))
	    
    (if (!= oldzpos zpos) 
	(write aserv (uneval (/ (real zpos) 255))))
    (if (> counter 1000)
	(begin
	  (print "checking for video" newline)
	  (if (!= oldzpos zpos) 
	      (write vservz (uneval (/ (real zpos) 255))))
	  (if (!= oldypos ypos)
	      (write vservy (uneval ypos)))
	  (if (!= oldxpos xpos)
	      (write vservx (uneval xpos)))
	  (set counter 0)))


    (free buffer1)
    (free buffer2)
    (free buffer3)

    ))






