'STEPHOME.BAS  HOME and EXERCISE ACTUATOR
'OCT 27, 1999 (adapted from QBasic "Terminal.bas".

'Use the amplified position pot signal to determin the
'home position of the stepper actuator.
'Assumes the AD2 read value of 128 to be the home position.
'A future version should store the real home position
'value in the STP100 EEProm and read that value back when
'a 'Home' function is executed.

DEFINT A-Z

DECLARE SUB Filter (InString$)

COLOR 7, 1                      ' Set screen color.
CLS

Quit$ = CHR$(0) + CHR$(45)      ' Value returned by INKEY$
                                ' when ALT+X is pressed.
HOME$ = CHR$(0) + CHR$(35)      ' Home Actuator

' Set up prompt on bottom line of screen and turn cursor on:
LOCATE 24, 1, 1
PRINT STRING$(80, "_");
LOCATE 25, 3
PRINT "Press ALT+X to quit    Press ALT+H to Home Actuator";
PRINT TAB(58); "STP100 Stepper Test.";
VIEW PRINT 1 TO 23              ' Print between lines 1 & 23.

' Open communications (9600 baud, no parity, 8-bit data,
' 1 stop bit, 256-byte input buffer):
OPEN "COM1:9600,N,8,1,cs,ds,cd,rs" FOR RANDOM AS #1 LEN = 256


ID$ = "1"                       ' STP100 Board ID Default is 1

HOME:
CMD$ = "SD10000": GOSUB TXchar  ' Go Home Slow so AD2 has time to read

CMD$ = "AD2": GOSUB TXchar      ' Read A/D
INPUT #1, AD$
IF VAL(AD$) < 128 THEN
CMD$ = "H+": GOSUB TXchar       ' Extend Actuator
ELSEIF VAL(AD$) > 128 THEN
CMD$ = "H-": GOSUB TXchar       ' Retract Actuator
END IF


DO

    CMD$ = "AD2": GOSUB TXchar  ' Stop at Center. "HI" (halt immediately)
    INPUT #1, AD$
    IF VAL(AD$) = 128 THEN CMD$ = "HI": GOSUB TXchar: EXIT DO

LOOP

PRINT AD$

CMD$ = "HM0": GOSUB TXchar      ' Set Home Position to Zero
CMD$ = "SD512": GOSUB TXchar    ' Increase stepper speed

DO                              ' Main communications loop.
  
   KeyInput$ = INKEY$           ' Check the keyboard.

   IF KeyInput$ = Quit$ THEN    ' Exit the loop if the user
      EXIT DO                   ' pressed ALT+q.

   ELSEIF KeyInput$ = HOME$ THEN ' Go Home Actuator
      GOTO HOME

   ELSEIF KeyInput$ <> "" THEN  ' Otherwise, if the user has
      PRINT #1, KeyInput$;      ' pressed a key, send the
      PRINT KeyInput$;
   END IF                       ' character typed to the modem.
  
   ' Check the modem. If characters are waiting (EOF(1) is
   ' true), get them and print them to the screen:
   IF NOT EOF(1) THEN

      ' LOC(1) gives the number of characters waiting:
      ModemInput$ = INPUT$(LOC(1), #1)

      Filter ModemInput$        ' Filter out line feeds and
   
      PRINT ModemInput$;        ' backspaces, then print.
   END IF
     
LOOP

CLOSE                           ' End communications.
CLS
END

'============================================================================

TXchar:

PRINT #1, "BD"; ID$; CMD$

RETURN

'
' ========================= FILTER ==========================
'     Filters characters in an input string.
' ============================================================
'
SUB Filter (InString$) STATIC

   ' Look for backspace characters and recode them to
   ' CHR$(29) (the LEFT cursor key):
   DO
      BackSpace = INSTR(InString$, CHR$(8))
      IF BackSpace THEN
         MID$(InString$, BackSpace) = CHR$(29)
      END IF
   LOOP WHILE BackSpace

   ' Look for line-feed characters and remove any found:
   DO
      LineFeed = INSTR(InString$, CHR$(10))
      IF LineFeed THEN
         InString$ = LEFT$(InString$, LineFeed - 1) + MID$(InString$, LineFeed + 1)
      END IF
   LOOP WHILE LineFeed

END SUB

