Input routine and command parser


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107

    Default Input routine and command parser

    This should probably be moved to the 'Examples' section, but I'll post it here for awhile so it will get noticed.

    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.

    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
    Charles Linquist

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Input routine and command parser

    Thanks Charles,

    Would you happen to have a schematic to go with this? Looks like it would make a good article for the projects section.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Input routine and command parser

    Not really. It does run on hardware, of course, but that hardware is really proprietary. But no hardware is needed for this. 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.

    I figured that lots of people use things like this (or maybe I'm the only one). At any rate, I use similar routines a lot. They are modified to protect the innocent, and the copyright property of the company I work for.
    Charles Linquist

  4. #4
    Join Date
    Aug 2011
    Location
    Guadalajara, México
    Posts
    28


    Did you find this post helpful? Yes | No

    Default Re: Input routine and command parser

    Thanks for sharing "Master" Charles, this code is very useful indeed, in fact should be great to see this sort of capabilities bundled with PBP.
    I'll try it in the first chance...

    Regards!
    My English doesn't sucks, it's just fugly...

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts