      PROGRAM TABLET
C     Digitize points with a Summagraphics MM 1201.
C     Gives X-Y coordinates in 1000ths of an inch 
C     every time a button is pressed.
C
C     Uses the following routines:
C        COMMSUB from Forlib Plus by Alpha Systems to read comm port
C
      INTEGER*2 IX, IY, IB
      LOGICAL*2 BREAK
C
      WRITE (*,*)
     +' You will get the X-Y coordinates every time a button is pressed'
      WRITE (*,*) '    ESC or ^C to stop'
C
C     Initialize the serial port with Forlib+ routines.
C     Requires INTEGER*2 arguments, but INTEGER*4 constants work.
C     Arguments, in order, set 9600 baud, odd parity, 1 stop bit, 
C     8 bits, comm 1. These are factory settings on the tablet.
      CALL STBAUD (7, 1, 1, 8, 0, CSTAT, CERR)
      IF (CERR .EQ. 0) THEN
         WRITE (*,*) ' Error in initializing comm. port'
         GO TO 50
      ENDIF
C     Now go to interrupt-driven input.
      CALL COMON
C
C     Initialize the Summagraphics MM 1201 tablet. Point mode, 1000 lpi.
C     We do not care about SSTAT, the word of status bits.
      CALL SNDBYT ('B', 0, SSTAT)
      CALL SNDBYT ('j', 0, SSTAT)
C
   10 CALL GETPNT (IX, IY, IB, BREAK)
      IF (BREAK) GO TO 50
      WRITE (*,*) IX, IY, IB
      GO TO 10
C
C     Reset comm port, close file, and stop.
   50 CALL OFFINT
      STOP
      END
      SUBROUTINE GETPNT (IX, IY, IBUT, BREAK)
C     Read a point from MM 1201 tablet using Forlib+ routines.
C
      INTEGER*2 IX, IY, IBUT
      CHARACTER*1 B(5)
      INTEGER*2 AVAIL, GETC, I, IB(5), IC, IER, NBYTES
      LOGICAL*2 BREAK, KEYHIT
C
      BREAK = .FALSE.
      NBYTES = 0
C     Test for bytes in input buffer. After 100 tests, check keyboard.
   10 DO 20 I = 1,100
      CALL BFSTAT (AVAIL)
      IF (AVAIL .EQ. 0) GO TO 20
      NBYTES = NBYTES + 1
      CALL FCHCHR (B(NBYTES), IER)
      IF (NBYTES .EQ. 5) GO TO 50
   20 CONTINUE
C
      IF (.NOT. KEYHIT(0)) GO TO 10
      IC = MOD(GETC(0),256)
      BREAK = (IC .EQ. 3) .OR. (IC .EQ. 27)
      IF (BREAK) RETURN
      GO TO 10
C
C     Convert the 5 binary-mode bytes sent by the tablet.
   50 DO 55 I = 1,5
      IB(I) = ICHAR(B(I))
   55 CONTINUE
C     First byte has proximity indicator and button number.
      IF (MOD(IB(1),128) .GE. 64) THEN
         WRITE (*,*)' Cursor was not in proximity to tablet'
         IX = 0
         IY = 0
         IBUT = 0
         RETURN
      ENDIF
      IBUT = MOD(IB(1),8)
      IX = IB(2) + IB(3)*128
      IY = IB(4) + IB(5)*128
      RETURN
      END
