PDA

View Full Version : How to let propeller chip distinguish what I have already input though a keyboard?



daniel ding
11-12-2011, 03:17 AM
Hi everyone
I have use a lcd.out(key.newkey) command to display the keyboard input. now I want to let the propeller distinguish what the lcd is displaying.
For one, if I press the "g" key on keyboard and lcd display a "g" charactor, and the chip know taht I input a "g" charactor ,and if I press enter key next the chip will light a led.
more I want to add a number after charactor "g" to decide the frequency of the led light.
thanks
daniel

SRLM
11-12-2011, 03:44 AM
You'll need to capture the keyboard input key in a variable, then test it to determine what to do. Something like this (untested psuedo-ish code)



VAR
byte key_input

PUB Main
repeat
key_input := key.newkey
if(key_input == 'g')
--do something
else if(key_input == '/n')
--do something else
--aka, wait for the key following:
key_input := key.newkey
if(key_input == '1')
--do something
else
--do something else
else
--it's not something special, so display it
lcd.out(key_input)


If you have lots of different conditions and sequences you may want to draw out a state machine for your tasks and code that up instead.

daniel ding
11-12-2011, 06:44 AM
SRLM
thanks for ur reply, but you know the"key.newkey" is a number of ASCII.
the command of you gave key_input=="g"????
also need a display of "g" the same time do something

MagIO2
11-12-2011, 12:53 PM
ASCII is simply a definition of which byte should corrospond to which character. If you have a keyboard that creates ASCII numbers for each key .. or if you have an LCD which accepts ASCII numbers ... or a terminal program ..... or a compiler which compiles strings to ASCII arrays ... in all these cases you have no problem taking a character on one hand and directly output it to the other hand.
In your example you can directly take the keyboard-input and send it to the LCD.
And as the propeller tool also creates ASCII-characters (at least for the number range defined by ASCII) you can also compare the keyboard-input with the single character in the code:
key_input == 'g'
And it will be true if you entered a g and id will be false if you entered something else.

To output the entered key you simply add:
key_input := key.newkey
lcd.out( key_input )
...

The point is that you have to store the key input somewhere if you want to do 2 things with it. In your example from the begining you send the key.newkey directly to the lcd and then it's gone. Now way to check the content afterwards - except reading it back from LCD, which is a lot of overhead.