! HP state - HP model 7221 plotter diagnostic

%external %routine %spec set mode (%string (255) MODE)
%external %routine %spec TTPUT (%integer CH)
%external %routine %spec FLUSH OUTPUT
%external %routine %spec PROMPT (%string (15) S)

! Control characters
%const %integer ESC =27
%const %integer DEL = 127

%routine HP INSTRUCTION (%integer WHICH)
   TTPUT (ESC);   TTPUT ('.');   TTPUT (WHICH)
%end

%external %routine HP STATE (%string (255) PARM FOR EMAS)
      %string (1) ESCS
      %string (127) RESPONSE, A, B, C
      %integer STATUS, D, M, Y, I

      %routine READ LINE (%string (127) %name ST)
          ST = ""
          SKIP SYMBOL %while NEXT SYMBOL <= ' '
          ST = ST.TO STRING (NEXT SYMBOL) %and SKIP SYMBOL %until NEXT SYMBOL = NL
      %end

      %on 1,2,3,4,5,6,7,8,9 %start
          PRINT STRING ("Plotter didn't respond as the manual said it should!")
          NEWLINE
          SET MODE ("E=ON")
          %stop
      %finish

      set mode ("E=OFF")
      ESCS = TO STRING (ESC)

      PRINT STRING ("                           HP plotter status")
      NEWLINES (3)

      SELECT INPUT (0)
      HP INSTRUCTION ('(')
      FLUSH OUTPUT
      PROMPT (ESCS.".A")
      READ LINE (RESPONSE) %until RESPONSE -> A.(",").B %and B -> B.(",").C %and A#""
      HP INSTRUCTION (')')
      FLUSH OUTPUT
      PRINT STRING ("Model: ".A."      Firm ware date: ")
      STATUS = LENGTH (B)
      Y = 1970 + CHARNO (B, STATUS) - '0'
      D = (CHARNO (B, STATUS-2) - '0') * 10 + (CHARNO (B, STATUS-1) - '0')
      M = CHARNO (B,1) - '0'
      M = M * 10 + CHARNO (B,2) - '0' %if STATUS>=5
      WRITE (D,0);   WRITE (M,1);   WRITE (Y,1)
      PRINT STRING ("      ROM: ")
      C = "none" %if C = "0"
      PRINT STRING (C)
      NEWLINES(3)

      HP INSTRUCTION ('(')
      FLUSH OUTPUT
      HP INSTRUCTION ('F')
      FLUSH OUTPUT
      PROMPT (TO STRING(DEL))
      READ (STATUS)
      HP INSTRUCTION (')')
      FLUSH OUTPUT

      %if status & 15 = 0 %start
          print string ("No pen is currently selected")
      %finish %else %start
          print string ("Pen")
          write ((status&14)>>1, 1)
          print string (" is selected and is ")
          %if status&1=0 %then print string ("up") %else print string ("down")
      %finish
      newline

      print string ("The following pens are at home:")
      %for I=1,1,4 %cycle
           write (i, 1) %if status&1<<(3+I)#0
      %repeat
      newline

      %if status&16_100=0 %start
          print string ("There is currently no servicing of the front pannel")
      %finish %else %start
          print string ("The front pannel is currently being serviced")
      %finish
      newline

      print string ("The pen position is ")
      %if status&16_200=0 %then print string ("within") %c
                          %else print string ("out of")
      print string (" limits")
      newline

      print string ("A macro is curently being defined") %and newline %if status&16_400=1
      print string ("Plotter front pannel controls have been used") %and newline %if status&16_800=1
      print string ("The data buffer has been aborted") %and newline %if status&16_1000=1
      %if status&16_E000=0 %start
          print string ("No errors have occured")
          newlines (2)
          set mode ("E=ON")
          %stop
      %finish

      print string ("There have been errors"); ! To be extended
      newlines(3)

      HP INSTRUCTION ('(')
      FLUSH OUTPUT
      HP INSTRUCTION ('E')
      FLUSH OUTPUT
      PROMPT (TO STRING (DEL))
      read line (a)
      HP INSTRUCTION (')')
      FLUSH OUTPUT
      print string ("Error type, faulty byte, num errors : ".a)
      newlines (2)
      set mode ("E=ON")
%end


%end %of %file

