(* ;;-*-LISP-*- KEEP EMACS HAPPY ********************************
*
*     OSCILLOSCOPE
*
*(0) (SCOPE.SETUP YMIN YMAX)
*(1) (SCOPE.CLEAR)
*(2) (SCOPE.POINT Y)
*() (SCOPE.DRAWTO Y)
*(3) (SCOPE.STEP)
*(4) (SCOPE.WIDTH)
****************************************************************)

(DEFVAR SCOPE.WINDOW)
(DEFVAR SCOPE.U 0)
(DEFVAR SCOPE.V 0)
(DEFVAR SCOPE.YMIN -1.0)
(DEFVAR SCOPE.YMAX 1.0)

(DEFEXPR (SCOPE.SETUP YMIN YMAX)
  (PROG ()
    (COND ((NULL SCOPE.WINDOW)
	   (SETQ SCOPE.WINDOW
		 (CREATEW NIL "Oscilloscope Window"))))
    (SETQ SCOPE.YMIN YMIN)
    (SETQ SCOPE.YMAX YMAX)
    (SCOPE.CLEAR)
))

(DEFEXPR (SCOPE.CLEAR)
  (PROG (Y0)
    (CLEARW SCOPE.WINDOW)
    (SETQ SCOPE.U 0)
    (SETQ SCOPE.V 0)
    (SETQ WIDTH (WINDOWPROP SCOPE.WINDOW 'WIDTH))
    (SETQ HEIGHT (WINDOWPROP SCOPE.WINDOW 'HEIGHT))
    (SETQ Y0 (/ (x (- SCOPE.YMIN) HEIGHT) (- SCOPE.YMAX SCOPE.YMIN)))
    (DRAWLINE 0 Y0 WIDTH Y0 1 'PAINT SCOPE.WINDOW)
))

(DEFEXPR (SCOPE.POINT Y)
  (PROG (HEIGHT)
    (SETQ HEIGHT (WINDOWPROP SCOPE.WINDOW 'HEIGHT))
    (COND ((OR (<$ Y SCOPE.YMIN) (>$ Y SCOPE.YMAX))
	   (FORMAT PROMPTWINDOW "SCOPE: ~A " Y)
	   (RETURN)))
    (SETQ SCOPE.V (FIXR (x$ (/$ (-$ Y SCOPE.YMIN) 
				(-$ SCOPE.YMAX SCOPE.YMIN))
			    HEIGHT)))
    (BITMAPBIT SCOPE.WINDOW SCOPE.U SCOPE.V 1)
))

(DEFEXPR (SCOPE.DRAWTO Y)
  (PROG (HEIGHT)
    (SETQ HEIGHT (WINDOWPROP SCOPE.WINDOW 'HEIGHT))
    (SETQ SCOPE.YMIN (WINDOWPROP SCOPE.WINDOW 'YMIN))
    (SETQ SCOPE.YMAX (WINDOWPROP SCOPE.WINDOW 'YMAX)) 
   (COND ((<$ Y SCOPE.YMIN)
	   (FORMAT PROMPTWINDOW "SCOPE: ~A " Y)
	   (SETQ V 0))
	  ((>$ Y SCOPE.YMAX)
	   (FORMAT PROMPTWINDOW "SCOPE: ~A " Y)
	   (SETQ V HEIGHT))
	  (T (SETQ V (FIXR (x$ (/$ (-$ Y SCOPE.YMIN) 
				   (-$ SCOPE.YMAX SCOPE.YMIN))
			       HEIGHT)))))
    (COND ((NOT (ZEROP SCOPE.U))
	   (DRAWLINE (1- SCOPE.U)
		     SCOPE.V
		     SCOPE.U
		     V
		     1
		     'PAINT
		     SCOPE.WINDOW)))
    (SETQ SCOPE.V V)
    (SCOPE.STEP)
))

(DEFEXPR (SCOPE.STEP)
  (PROG ()
    (SETQ SCOPE.U (1+ SCOPE.U))
))

(DEFEXPR (SCOPE.WIDTH)
  (WINDOWPROP SCOPE.WINDOW 'WIDTH))

STOP