MEL PICBASIC Forum - Include File Submission

  • KeyboardNumberInput.bas

    For those who need to have a means to input numerical user responses into their program from a serial terminal.
    Most of the commonly-used methods (like HSERIN [DEC numdata] work, but provide no user feedback as to what they are typing. It also doesn't allow for backspacing. This include file provides both.

    As an added bonus, the file lets you input negative numbers by typing a "-" before the number. And, because people who are used to occasionally putting a "-" before their entry may also put a "+", the file will accept this also.

    This routine will work with most serial terminal programs, like HYPERTERM and Bray's TERMINAL. No fancy emulation modes (VT-XX) need to be selected.

    The INCLUDE has several defaults that can be overridden by changing the values of certain variables before the INCLUDE is called via a GOSUB. These defaults include the maximum number of characters (MAXCHAR), whether or not "-" and "+" will be allowed before the number entry (ALLLOWSIGN), and the timeout - how long the routine will wait with no input before returning.

    If the MAXCHAR limit is reached, the value is returned as if an <ENTER> had been pressed. Of course, an <ENTER> can be pressed at any time. 5 is the default.

    If ALLOWSIGN = 1, then a "+" or "-" will be allowed - BUT ONLY IN THE LEFTMOST POSITION. A "+" is displayed, but "thrown away" later. A "-" on the other hand, will return the twos compliment of the number.

    Pressing <ESC> will return the value of zero and set bit 2 of the variable KERROR. Likewise, entering nothing will cause a timeout and will set KERROR bit 1 and return a value of zero. Entering a negative number that is too large will set KERROR bit 0.

    Kerror bit 7 will be set if the returned value is negative.

    The returned value is KRETURNED.

    To invoke the utility :

    This routine is written for hardware serial ports. Although software ports (SERIN2) CAN be used, I discourage this horrible practice whenever I can.


    <Set up serial port>

    INCLUDE "KBNumberInput.bas"

    <Change defaults, if desired>


    Hserout [CR,LF,"Give me the number "]
    Gosub GetNum

    IF !Kerror then
    HSEROUT [CR,LF,"The number you typed in was ",#KResult]
    if Kerror.7 then
    HSEROUT [CR,LF,"The NEGATIVE number you typed in was ",SDEC Keyresult]
    <Process errors here>

    ; Numeric Input Routine by Charles Linquist
    ; Set MaxNum to maximum number of digits you will take (default = 5)
    ; Set KInputTimeout to the number of secs you will wait before giving up.
    ; If you will accept a sign ("+" or "-") then set AllowSign to 1 in your
    ; program it will over-ride these values
    ; KRESULT is , well, the result.
            Keyin var byte
            Maxnum var byte
            YY var word
            POSN var byte
            AllowSign var bit
            Kerror var Byte
            KInputString var byte [10]
            KResult Var word
            KInputTimeout var Word
            XN var byte
            BS CON 8
            ESC cON 27
            LF con 10
            SP con 32
            CR con 13
            MaxNum = 5     ; max number of chars
            KInputTimeout = 10000 ; 10 sec
            AllowSign = 0 
            goto OverKInput 
            KResult = 0
            Kerror = 0
            YY = 1
            ArrayWrite KInputString,[Rep $FF\8]  ; Fill with $FF
            HSERIN KInputTimeout,DeadAtKeyboard,[Keyin]
                IF Keyin = ESC THEN Escaped
                If Keyin = CR THEN DunInputn
                If AllowSign then
                IF Posn = 0 THEN 
                  If Keyin = "-" THEN
                      Kerror.7 = 1
                      Hserout ["-"]
                      goto GetMoreN
                   if Keyin = "+" then
                      Hserout ["+"]   
                      goto GetMoreN
                IF Keyin = BS THEN
                     IF POSN > 0 THEN
                      HSEROUT [BS,SP,BS]
                      POSN = POSN - 1
                      KInputString [POSN] = $FF
                      GOTO GetMoreN
            iF POSN >= MaxNum THEN goto DunInputN    
            IF Keyin < "0" or Keyin > "9" THEN GetMoreN 
            HSEROUT [Keyin]
            KInputString [POSN] = Keyin - "0"
            POSN = POSN +1
            GOTO GetMoreN
            For xN = 4 to 0 STEP - 1 
               IF KInputString[XN]= 255 THEN NotANum
                 KResult = KResult + (KInputString[xN]*YY)
                 YY = YY *10 
            Next XN
            If Kerror.7 then
              If Kresult < 32768 then
                   KResult = 0 - KResult
                   Kerror.0 = 1
                   goto zeronum
            Return    ; success
            Kerror.1 = 1
            Goto ZeroNum
           Kerror.2 = 1
            KResult = 0
    No attempt has been made to prevent entries over 65535. An easy way to prevent this mistake is to set MAXNUM to 4. This will make it impossible to enter numbers over 9999.