• Input routine and command parser

    This routine takes keyboard input, including backspaces, and processes them through a command parser. It converts all chars (but not numbers) to uppercase before processing.

    It will run on your PICDem board.

    This example is actually 3 parts in one.

    Part number 1 (GetCharInput) is a routine that allows you to type and send text to the PIC. It echoes back valid characters, and lets you backspace if you make an error. It "rubs out" the backspace(d) characters, and it terminates when you hit <ENTER> or <ESC>. If you hit <ENTER> it returns the input in an array. If you hit <ESC> it wipes out the array and returns. If you do nothing for 9 seconds, it returns with an empty string.

    Part number 2 (ConvertToUpper) takes the input in mixed-case and converts all the alpha characters to upper case. It leaves numbers and punctuation "as-is". This is handy when you do compares, since people may or may not capitalize the right letters when they type something in.

    Part number 3 (ParseIt) takes the array and searches for any number of strings. When it finds a match, it returns a number that indicates which of the strings matched. This is really useful if you get random data from something like a GPS receiver, or from a network port - or anything.



    Enjoy.

    Code:
     '****************************************************************
    ;
    ;Name    : Code Example                                      
    '*  Author  : Charles Linquist                                  
    '*  Date    : 9/10/2011                                         
    '*  Version :1.0                                               
    '*  Notes  :  For the Forum                                                
    '****************************************************************
     
            DEFINE OSC 40                                                           
     
            DEFINE NO_CLRWDT 1
            DEFINE _18F8723 1
            DEFINE HSER_RCSTA 90H
            DEFINE HSER_TXSTA 20H
            DEFINE HSER_BAUD 9600
            DEFINE HSER_CLROERR 1
            DEFINE LOADER_USED 1                                               
     
     
            TRISC = %10111111 
     
            MaxChars var byte
            Inputstring var byte [12]
            Timeout var word
            X var byte
            CharInput var byte
            InputString_Len Var Byte
            CMD VAR BYTE
     
     
            MaxChars = 10
            InputString_Len = MaxChars
            Timeout = 9000
     
            BS con 8
            CR CON 13
            LF CON 10
            ESC CON 27
            SP CON 32
     
     
    topp:
     
     
        Gosub GetCharInput
         Gosub ConvertToUpper
        Gosub Parseit
     
        goto topp
     
     
    ;-------------------------------------------------------------------
    GetCharInput:
     
    HSEROUT [CR,LF,"Input ? "]
     
     
    TextIn:
            ArrayWrite InputString,[REP 0\MaxChars] ; zero out the string to start with
            X = 0 
     
    TextIn2:
            HSERIN Timeout,NoInput,[CharInput]
            IF CharInput = CR Then Goto DoneInput
            If CharInput = ESC THEN goto NoInput
               IF CharInput = BS THEN 
                   IF X = 0 THEN
                    Goto TextIn2 
               ELSE   
                    HSEROUT [BS,SP,BS]
                   X = X - 1
                  InputString[x] = 0 ; zero out the backspaced char
                  GOTO TextIn2
              ENDIF 
         ENDIF    
     
    InputString[X] = CharInput
            HSEROUT [CharInput]      ; Show what we got                                                    
     
            IF X >= MaxChars then goto DoneInput
            X = X + 1
            GOTO TextIn2
    DoneInput:
            Return
     
     
    NoInput:
            ArrayWrite InputString,[REP 0\MaxChars]  ; wipe everything 
    out
            hserout [CR,LF,"No Input!",CR,LF]
     
    Return
     
     
    ;------------------------------------------------------------------------------
     
     
    ConvertToUpper:
         For X = 0 to MaxChars
           IF InputString[X] > 96 and INputString[X] < 123 then
              InputString [X] = Inputstring[X] & %11011111
           ENDIF     
     
         Next X
     
         Return
     
     
    ;-----------------------------------------------------------------------------
     
     
    Parseit:
     
    Parse01: Cmd=01 : ARRAYREAD InputString,InputString_Len,Parse02,[WAIT("AGE")] : GOTO Foundit
    Parse02: Cmd=02 : ARRAYREAD InputString,InputString_Len,Parse03,[WAIT("BIRTHDAY")] : GOTO Foundit
    Parse03: Cmd=03 : ARRAYREAD InputString,InputString_Len,Parse04,[WAIT("CITY")] : GOTO Foundit
    Parse04: Cmd=04 : ARRAYREAD InputString,InputString_Len,Parse05,[WAIT("STATE")] : GOTO Foundit
    Parse05: Cmd=05 : ARRAYREAD InputString,InputString_Len,Parse06,[WAIT("COUNTRY")] : GOTO Foundit
    Parse06: 
    Cmd=255
     
     
     
    Foundit:
     
     
      Select Case Cmd
          Case 1
            Hserout [CR,LF,"You typed 'AGE'",CR,Lf]
          CASE 2
            Hserout [CR,LF,"You typed 'BIRTHDAY'",CR,Lf]
          CASE 3
            HSEROUT [CR,LF,"You typed 'CITY'",CR,Lf]
          CASE 4
            HSEROUT [CR,LF,"You typed 'STATE'",CR,Lf]
          CASE 5
            HSEROUT [CR,LF,"You typed 'COUNTRY'",CR,Lf]
          CASE ELSE
            HSEROUT [CR,LF,"Invalid command",CR,LF]
       END SELECT
     
     
    RETURN
    This article was originally published in forum thread: Input routine and command parser started by Charles Linquis View original post