PDA

View Full Version : Spin "GOTO" Function??



Microcontrolled
03-16-2009, 07:02 AM
You don't have to be too experienced to awnswer this one: Is there a Spin function like the BASIC function GOTO?·It might be even more handy if ther where a GOSUB-like instruction. This is my code:

PUB GetState(Modi,areg)
·····················
· serbyte2 := ReadByte(28,EEPROM,areg)
· serout_str(2,@serbyte2,2400,1,8)
· serin2 := serin_char(1,2400,1,8)
· if serin2==1
··· Modi := @on
· elseif serin2==2
··· Modi := @off
· else
··· Modi := @xxx·

After each Modi := etc. instruction I want it to return to·the next Mod on a list of them to scan.·If i use a return function, it returns to the top of the PUB routine that they are in and simply redoes it. Please Help!http://forums.parallax.com/images/smilies/roll.gif·


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you for reading!·

···

Phil Pilgrim (PhiPi)
03-16-2009, 07:20 AM
There is no GOTO in Spin. It's simply not needed. Calling a PUBlic or PRIvate method in Spin is the equivalent of BASIC's GOSUB, but with the addition of parameters and a return value.

-Phil

Microcontrolled
03-16-2009, 07:54 AM
This dosn't work with my code:
GetState(Mod1,$8000)
GetState(Mod2,$8010)
GetState(Mod3,$8020)
Etc.
I want it to execute each command in order and I have VERY limited memory. Does anyone know how to do this??

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you for reading!·

···

Jetfire
03-16-2009, 07:58 AM
microcontroled1204 said...

GetState(Mod1,$8000)
GetState(Mod2,$8010)
GetState(Mod3,$8020)



Do you mean that this works, but you don't have enough memory to have each call to GetState for every Modx?

CannibalRobotics
03-16-2009, 07:54 PM
What about a CASE statement.

CASE Serin2
1:
Modi := @on
2:
Modi := @off
OTHER:
Modi := @xxx



I think it returns to the last call point and processes the list. OR, if you have a true condition at the top, it will comeback and address the next true condition.
If that does not work you could wrap in in a Repeat.

Repeat until serin2 > 0 & serin2 < 3
.....

Or, maybe I'm confused as to why a GoTo would be necessary. A larger view of code might go a long way.
Jim-

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Signature space for rent!
Send $1 to CannibalRobotics.com.

sylvie369
03-16-2009, 08:37 PM
You should be able to do what you're trying to do with some combination of calling methods and using the REPEAT statement.



Edit: I agree strongly with Ned (below). Working your way through chapter 3 of the manual will do wonders.

Post Edited (sylvie369) : 3/16/2009 2:28:50 PM GMT

WNed
03-16-2009, 08:51 PM
Just adding to PhiPi's explanation:
you can think of a Method as being just like a Subroutine, but you call it by name instead of a line number.
In structured languages, like Spin, what you end up doing is writing a bunch of small programs (subroutines, functions, methods, classes - they're just little programs), then you write a Main program, often just a loop, that calls each of the smaller programs when needed. If you've written anything big in Basic, you already know this concept, and I'm guessing you're pretty good with a BS2, yes?
Running through the tutorials in CH. 3 of the Propeller manual is worth every minute spent.
The Case statement, as CannibalRobotics pointed out, is your best bet any time you have stacked if's (p. 171 of the Prop manual)
Hanging on to GoTo and GoSub is like asking everybody to speak Latin. They are a hold over from a time long passed, and the more time you spend learning structured language concepts, the better prepared you'll be to work with a wide range of newer technologies. Work your way through Ch. 3 of the Prop Manual, and you'll be answering questions here, rather than asking them.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"They may have computers, and other weapons of mass destruction." - Janet Reno

Kye
03-16-2009, 09:05 PM
Want goto and gosub? Use assembly, hehe...

That's fun, very fun...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

Mike Green
03-16-2009, 09:35 PM
Spin has a variety of structured "goto"s that do specific looping. Most looping is lumped into the REPEAT statement. Along with the NEXT and QUIT statements, you can do pretty much any sort of loop that you'd do with a GOTO and it makes more sense, is documented better. There's also the CASE statement which is very flexible.

In your case, you'd probably be using a simple REPEAT and a CASE statement.

One thing I noticed ... You use the STR call for SEROUT. This expects a zero-terminated string. It's not clear from your code whether your SERBYTE2 variable includes this. I don't know what serial I/O routines you're using, but there's probably one that takes a single character rather than the address of a character and that's the one you should use in this case (and not use the @).

Your assignments to Modi won't work either. Spin, like C, passes values to subroutines. If you want to be able to store into something, you need to pass the address, then use that. In your case, it might be easier to use the RETURN statement and that will both exit from GetState and provide a value to the caller. Since you're talking about using GOTOs, you may be wanting to return the label of a state routine. You can't do that in Spin the way you want. Rather than just present some examples that you might not want, how about explaining what you're trying to do and giving some of your own examples conceptually?

Microcontrolled
03-16-2009, 10:25 PM
The CASE statement looks like my best bet. I do not, however, have a Propeller manual. I started Propeller with the downloaded version and then got a HYDRA after about 3 months of that. It gave me a near full understanding of the Propeller (in the book) but itself in the book said that the Propeller manuel would be best if you wanted to do other things with the Propeller. To jetfire, it does not work, it "freezes" the repeat loop. I would post the whole code and tell you what I'm trying to build exept that I am building it for a contest.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you for reading!·

···

Mike Green
03-16-2009, 10:30 PM
For state machines, a CASE statement inside a REPEAT statement is the way to go. Each case is a state and you can use the NEXT statement if you need to go to the next state from inside some complicated IF/THEN condition.

If you have a lot of states, it may be easier to split the CASE statement into several smaller CASE statements covering different ranges like:


PRI part1(x)
CASE x
0:
1:
...

PRI part2(x)
CASE x
10:
11:
...

PRI ...
CASE state/10
0: part1(state)
1: part2(state)
...


Post Edited (Mike Green) : 3/16/2009 3:38:10 PM GMT

sylvie369
03-16-2009, 11:02 PM
microcontroled1204 said...
The CASE statement looks like my best bet. I do not, however, have a Propeller manual.
Now you do:

http://www.parallax.com/tabid/442/Default.aspx

This, again, is what makes Parallax so valuable to those of us just learning about these things: significant effort to document and educate, and then simply giving away that documentation.

Rayman
03-16-2009, 11:16 PM
Don't use that manual! :)

Use this one:

http://www.rayslogic.com/propeller/Propeller%20Manual%20v1.01bmarked.pdf

I've added bookmarks that make it a whole lot easier to use!

Oldbitcollector (Jeff)
03-16-2009, 11:45 PM
And there is always the PE Labs Fundamentals Book (http://www.parallax.com/Portals/0/Downloads/docs/prod/prop/Web-PELabsFunBook-v1.0.pdf).
(Parallax is cool to give these away as free downloads.)

@Ray, could we talk you into bookmarking this one as well?

With my background in old computers and various dialects of BASIC, I'm surprised
I don't miss GOTO and GOSUB more. I guess if I can adapt, anyone can. :)

OBC

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?

Check out: Protoboard Introduction (http://jeffledger.googlepages.com/Protoboard_Introduction.pdf) , Propeller Cookbook 1.4 (http://ucontroller.com/Propeller%20Protoboard%20Designs%20for%20the%20Beg inner.pdf) & Software Index (http://forums.parallax.com/showthread.php?p=770318)
Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us (http://propeller.warrantyvoid.us)
Got an SD card connected? - PropDOS (http://www.orrtech.net/propdos/)

Erik Friesen
03-16-2009, 11:46 PM
Rayman said...
Don't use that manual! :)


Good Idea Rayman.

However, the very best is the paper manual. You won't be sorry.

Microcontrolled
03-17-2009, 06:36 AM
I said in my last thread that I started with my downloaded version; I ment with the Propeller manual. Thank you for the bookmarked copy, though! It will make it so much easier to find the right chapter and topic!! I do, however, agree with Eric Friesen. It is so much better to have the paper copy because you don't have to stare at a screen for hours to read it. I would just buy it, if I had some money. For the time being, I'll use the downloaded version.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you for reading!·

···

Rayman
03-17-2009, 06:55 AM
OBC: I'll put that on my TODO list (but it's very long!)

I really feel sorry for people trying to get by without the bookmarked version... I still remember how it was driving me crazy trying to find things...