PDA

View Full Version : Please help me understand variables



Oldbitcollector (Jeff)
09-18-2007, 07:37 AM
Ok, I'm made some headway in spin in the last couple days working with various objects.

Could someone explain how one can grab keyboard data from 'comboKeyboard' or 'FullDuplexSerial' or even 'fsrw' and
drop it into memory for usage in other places. I just can't seem to get my brain around this concept! Perhaps
because my experience in programming has been various BASICs and Perl. (Two places that have variables)
and spin doesn't support this. (Perhaps a good object for something like this could be created, *or I really don't get it)

in 'comboKeyboard' using 'key.key' I can wait for a keystroke and that keystoke is assigned to a variable? am I wrong?

PRI terminal | local,name

repeat
local := key.key
if local <> 0
name := name + local ' <-- Why can't I simply add them into local to save a line of text?
text.out(local)

I can write the keystroke back to the screen, remote, SD. What would I do to fill 'local' with all the keystrokes
and display them later as 'text.out(local)?

If someone who understands either of the languages I mentioned could explain a solution in comparison, I'd really
be grateful. I'd really like to get my mind around this concept.

Thanks!
Oldbit

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

— Calvin, of 'Calvin and Hobbes.

Post Edited (Oldbitcollector) : 9/18/2007 4:08:44 AM GMT

BTX
09-18-2007, 07:48 AM
Hi Oldbit.

I don't know if I understand good your question... but...did you see the "Keyboard Buffer Demo.spin" ??
It saves the keystrokes into a buffer....you can do it as long as you want...or simply pass the data to another buffer, for save all keystrokes on it ...
Why not that ??



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Regards.

Alberto.

Oldbitcollector (Jeff)
09-18-2007, 07:54 AM
Where is this file... perhaps it may answer some questions. :)

Oldbitcollector


Is my confusion on this because I am working with numbers in actuality?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

— Calvin, of 'Calvin and Hobbes.

BTX
09-18-2007, 08:54 AM
Hi..Oldbit.

I can't remember the thread where I downloaded it ...but seems to be a demo from Parallax.

I've attached the files here..

I'm working with "numbers" too, in this momment...and I used and modified this code.



Good Luck !!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Regards.

Alberto.

Oldbitcollector (Jeff)
09-18-2007, 09:13 AM
That will help! Here's what I've come up with at the moment.
Glancing at that Buffer demo it looks like I'm heading in the right direction.




OBJ
key : "comboKeyboard"
text : "ymtv_text"

VAR
byte buffer[32]

PUB mainProgram

key.start(26)
text.start(12)
text.str(string("Start typing (up to 31 Characters)",13))
getData(@buffer,32)
text.str(string(13,"I saw",13))
text.str(@buffer)

PUB getData(ptr, size) | c

repeat until c == 13
c := key.getkey
if size > 1
text.out(c)
byte[ptr++] := c
size--




I think I'm starting to get it. Now if I can figure out why this works. :)

Oldbitcollector

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

— Calvin, of 'Calvin and Hobbes.

Fred Hawkins
09-18-2007, 10:51 AM
local := key.key
if local <> 0
name := name + local ' <-- Why can't I simply add them into local to save a line of text?
text.out(local)

Local gets an·ascii character value. Then you begin a sum of these characters. Name isn't a string, it is a sum. To get a string you need to build an array of saved character values.

Post Edited (Fred Hawkins) : 9/18/2007 10:06:46 AM GMT

Oldbitcollector (Jeff)
09-18-2007, 11:11 AM
Yes, I'm starting to see this...

I'm working on some spin code to create a type of BASIC 'INPUT' command.

I've got a funky problem with removing the last character from the @data block.
I take it that "byte[data--] := 0" is not ideal? It seems to work only part of the time
when used in my backspace routine.

Oldbitcollector

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

— Calvin, of 'Calvin and Hobbes.

Mike Green
09-18-2007, 11:18 AM
Here's the getline routine from FemtoBasic. It reads in a line of text from the keyboard and echos it to the display. You might look at the INPUT routine in FemtoBasic which will put out an optional prompt, then accept a series of expressions optionally separated by commas.



PRI getline | i, c
i := 0
repeat
c := key.getkey
if c == bspKey
if i > 0
dsp.str(string(backspace," ",backspace))
i--
elseif c == fReturn
dsp.out(c)
tline[ i ] := 0
tp := @tline
return
elseif i < linelen-1
dsp.out(c)
tline[ i++ ] := c



tline is the input buffer and linelen is the number of bytes in the buffer. tp is a pointer to the start of the input text. The rest should be pretty obvious.

Oldbitcollector (Jeff)
09-18-2007, 11:31 AM
Mike,

Here's where I'm at so far.. The routine is reasonable, but not quite perfect. :)




PUB input(data, size) | keystroke,len
keystroke:=0
len:=0
repeat until keystroke == 13 'Exit when Carriage Return is pressed
keystroke := key.key
if keystroke == $C8 ' Check for Backspace Key
if len > 0
byte[data--] := 0
size++
len--
text.out(8)
text.out(32)
text.out(8)
keystroke := 0

if keystroke == $C8 'Clean up backspace if all is cleared
keystroke:= 0

if keystroke > 10
if size > 1
text.out(keystroke)
if keystroke <> 13
byte[data++] := keystroke
size--
len++





I'm using 'key.key' so I can implement FullDuplex later.

Do you see any obvious mistakes?

Oldbitcollector

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

— Calvin, of 'Calvin and Hobbes.

deSilva
09-18-2007, 01:19 PM
Not really mistakes... I wonder why you say it works only "sometimes"...

(a) You can use "ELSE" (or "ELSEIF") or even a CASE-construction so you can avoid the clumsy "keystroke :=0"

(b) Usinga "repellent" loop is not always the best construction, SPIN has a "break" and a "continue" for reasons only known to it's inventors called QUIT and NEXT. This can sometimes improve readability:


REPEAT
CASE keystroke:= key.key
13: QUIT 'Exit when Carriage Return is pressed

$C8:
......
OTHER:
........


Post Edited (deSilva) : 9/18/2007 6:24:02 AM GMT