PDA

View Full Version : How to recognize the "Enter" keystroke



Dick Ivers
- 1st December 2014, 16:20
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?




'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

HenrikOlsson
- 1st December 2014, 19:13
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:



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.

Dick Ivers
- 2nd December 2014, 00:03
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

Dick Ivers
- 2nd December 2014, 01:12
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:


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]

HenrikOlsson
- 2nd December 2014, 06:14
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:

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.

Dick Ivers
- 2nd December 2014, 13:49
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

HenrikOlsson
- 2nd December 2014, 18:57
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.

Dick Ivers
- 2nd December 2014, 23:19
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