How to recognize the "Enter" keystroke


Closed Thread
Results 1 to 8 of 8
  1. #1

    Default How to recognize the "Enter" keystroke

    Hi,
    I have a list of variables stored in eeprom. In the code example below there are four variables shown but in the final program there will be eight.

    I have working code will allows each of the variables to be edited from a pc keyboard. The data is transmitted via hardware serial through serial-to-USB cable. It all works well.

    The problem with the present code is that for each variable a value must be entered, even if no change is needed. The old value must be re-entered. If numbers are not keyed in prior to the enter keystroke the program will not proceed.

    The process of re-entering the old values can get tedious when there is a long list of variables. So the question is this: Is there a way for the program to recognize an enter stroke without a preceding number and then just go ahead using the existing value stored in eeprom?

    Code:
     
    
    'write eeprom data.....done at program time not run time        
            eeprom 0,[0,0]    'default MTRplus = 0.0 seconds
            EEProm 2,[0,0]    'default FlightT = 0.0 seconds       
            EEProm 4,[3,242]  'default Pos1Time = 101.0 seconds                                     
            EEProm 6,[1,244]  'default Pos1 = 500 ticks
    
    
    'user programming with VT100 terminal
            hserout [27,91,50,74]     'clear terminal screen
            HSEROUT [27,91,72]        'go to start of text
    
    
            READ 0,MTRplus.byte1
            read 1,MTRplus.byte0
            READ 2,FlightT.byte1
            read 3,FlightT.byte0
            READ 4,Pos1Time.byte1
            read 5,Pos1Time.byte0
            READ 6,Pos1.byte1
            read 7,Pos1.byte0
    
            hserout [27,91,50,74]  'clear terminal screen
            HSEROUT [27,91,72]     'go to start of text
    
    new:    HSEROUT ["  Motor Runtime",13,10]
            HSEROUT [ "   ",dec MTRplus/10,46,DEC MTRplus dig 0," Seconds",13,10,10] 
            hserout ["  DT Time",13,10]  
            HSEROUT [ "   ",dec FlightT/10,46,dec FlightT DIG 0," Seconds",13,10,10] 
            hserout ["  Position #1 Time",13,10]  
            HSEROUT [ "   ",dec Pos1Time/10,46,dec Pos1Time DIG 0," Seconds",13,10,10] 
            hserout ["  Position #1 Angle",13,10]  
            HSEROUT [ "   ",dec Pos1/10,dec Pos1 DIG 0," Ticks",13,10,10] 
    
    re:     HSEROUT ["  Enter New Motor Runtime x 10",13,10]
            HSERIN [dec MTRplus]      'enter new MTRplus value if needed
           
            write 0,MTRplus.byte1     'write new value to eeprom
            write 1,MTRplus.byte0
            HSEROUT [ "   ",dec MTRplus/10,46,DEC MTRplus dig 0," Seconds",13,10,10]
            
    re2:    hserout ["  Enter New DT Time x 10",13,10] 
    
            hserin [dec FlightT]     'enter new DT time value if needed
       
            write 2,FlightT.byte1    'write new value to eeprom
            write 3,FlightT.byte0
            HSEROUT [ "   ",dec FlightT/10,46,DEC FlightT dig 0," Seconds",13,10,10]
    
    re3:    hserout ["  Enter New Position #1 Time x 10",13,10] 
    
            hserin [dec Pos1Time]     'enter new Pos time value if needed
            
            write 4,Pos1Time.byte1     'write new value to eeprom
            write 5,Pos1Time.byte0
    
            HSEROUT [ "   ",dec Pos1Time/10,46,DEC Pos1Time dig 0," Seconds",13,10,10]
    
    re4:    hserout ["  Enter New Position #1 Angle x 10",13,10] 
    
            hserin [dec Pos1]      'enter new Pos1 if needed
            
            write 6,Pos1.byte1     'write new value to eeprom
            write 7,Pos1.byte0
    
            hserout [27,91,50,74]  'clear terminal screen
            hserout [27,91,72]     'go to start of text
            hserout [10]           'move down 1 line
            goto new               'print new values then unplug cable and restart

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: How to recognize the "Enter" keystroke

    Hi,
    I feel there might be an easier way but I'm currently not quite up to speed on the string modifiers so here's one way:

    Code:
    i       VAR BYTE
    Value   VAR WORD
    Array   VAR BYTE[5]
    
    Main:
      GOSUB ClearArray
    
      HSEROUT["Enter new value or press enter to keep [ ", DEC Value," ] :",10,13]
      HSERIN[Str Array\7\13]
    
      ' Is the first character either a LF , CR or NULL? If so we abort and keep the old value.
      IF Array[0] = 10 or Array[0] = 13 OR Array[0] = 0 THEN Main
      ArrayRead Array,[DEC Value]
    Goto Main
    
    ClearArray:
        For i = 0 to 4
            Array[i] = 0
        NEXT
    RETURN
    /Henrik.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: How to recognize the "Enter" keystroke

    Henrick,
    Thanks.... your suggestion to use the ARRAYREAD function to parse the incoming strings I think will work, and is a good idea. I didn't know about that command until now. However, I can't quite follow all of your suggested code. There doesn't seem to be a way to leave the main loop in order to write the new values. Could you take a look again?

    Dick

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: How to recognize the "Enter" keystroke

    Henrick,
    I got your idea to work using a modification to your code. I need do do more testing, but it seems solid. The modified code is as follows:

    Code:
        i       VAR BYTE
        Value   VAR WORD
        Array   VAR BYTE[5]
       
        Main:
           gosub ClearArray 
           
           HSEROUT ["  Enter New Motor Runtime x 10",13,10]'enter new Value if needed
           
          HSERIN [STR array\7\13]     
           IF array[0] = 10 or array[0] = 13 OR array[0] = 0 THEN 
           arraywrite array,[dec Value] 'this is the old value
           endif
           
           ArrayRead array,[DEC Value] 'this is the new value
           Goto report 
    
            ClearArray:
                For i = 0 to 4
                    array[i] = 0
                NEXT
           RETURN
           
    report:       
           write 0,Value.byte1     'write new value to eeprom
           write 1,Value.byte0
           HSEROUT [ "   ",dec Value/10,46,DEC Value dig 0," Seconds",13,10,10]
    Last edited by Dick Ivers; - 2nd December 2014 at 01:22.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: How to recognize the "Enter" keystroke

    Hi,
    I don't see the need for the ArrayWrite in there. As long as you don't exectute the ArrayRead the Value variable is untouched, no need to write the same value to it again. And, likewise, there's no need to write the value back to EEPROM if it didn't actually change. How about this:
    Code:
    Main:
    	gosub ClearArray 
           
    	HSEROUT ["  Enter New Motor Runtime x 10",13,10]     		' Enter new Value if needed
           
    	HSERIN [STR array\7\13]     
    	IF array[0] = 10 or array[0] = 13 OR array[0] = 0 THEN report	' Value didn't change, print old value. 
           
    	ArrayRead array,[DEC Value] 					' Get new value from input string
    	Write 0, Value.byte1     					' Write new value to eeprom
    	Write 1, Value.byte0
    
    report:
           HSEROUT [ "   ",dec Value/10,46,DEC Value dig 0," Seconds",13,10,10]
    Goto Main
    
    
    ClearArray:
    	For i = 0 to 4
    		array[i] = 0
    	NEXT
    RETURN
    /Henrik.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: How to recognize the "Enter" keystroke

    Henrick,
    You are quite correct, there is no need to employ arrraywrite to get the desired result. I tried your new code and it works. Also, I don't think there is a need to test for the presence of a line feed character. I noticed earlier that when the enter key was hit the screen cursor did not drop to a new line. I'll try this next .. it's a minor point.
    Dick

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: How to recognize the "Enter" keystroke

    Hi Dick,

    As far as the linefeed goes I'd say it depends....
    If this is something for your own use then by all means, whatever works. But if you're shipping it out for others to use my advise is to make it as robust as you can (and there's probably more that can be done on that front).

    For example, the serial terminal in MicroCode Studio can be set up to end each line with CR, CR+LF, LF+CR, NULL or nothing at all.
    So if it, or any other terminal program, happens to be set to LF+CR instead of just CR or CR+LF then you would need to check for it.

    I couldn't make it work properly without also looking for LF. I only tried with the serial terminal in MCSP though.

    /Henrik.

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: How to recognize the "Enter" keystroke

    Henrick,
    You're right that terminals can be configured differently. I'm using TeraTermPro and Hyperterm VT100 terminals. Both are configured for the lines to end with a CR. Agreed that it would be best to cover all the possibilities as you suggest.

    I'm pleased with the way the Enter key recognition is now working. I'm sure that improvements are possible, but I'm leaving this subject for now to go on with my project. I've learned a lot from this exercise.
    Thx
    Dick

Similar Threads

  1. Replies: 0
    Last Post: - 14th November 2013, 03:32
  2. Replies: 3
    Last Post: - 15th October 2012, 08:06
  3. Replies: 8
    Last Post: - 3rd March 2011, 22:20
  4. Replies: 1
    Last Post: - 27th August 2009, 02:42
  5. Replies: 1
    Last Post: - 16th February 2005, 20:05

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