Shop OBEX P1 Docs P2 Docs Learn Events
trying to figure a few things out in spin — Parallax Forums

trying to figure a few things out in spin

i182willswi182willsw Posts: 3
edited 2011-02-23 09:00 in Propeller 1
Hey guys, I'm new to Spin and the Propeller all together. I'm used to programming in C, C#, and Java and so there are a few things in Spin that I haven't been able to figure out.

1. Is there a way to have a method return a value or an object? I've seen people just make global variables and then have the methods just write to it. But I'm trying to make a linked list object and want to be able to add and retreave data.

2.
Pub checkping | leftdistance, rightdistance
In a tutorial on Gadgetganster they have this, but leftdistance, and rightdistance are never instantiated as a variable. So is it instantiating it right now and what variable type is it.

3. Just any tips or tricks you guys have found, a website that has all the libraries and it's methods....
Thanks guys, I've very excited with the thought of multiple cogs.

Comments

  • KyeKye Posts: 2,200
    edited 2011-02-22 17:33
    Use the built "result" variable. Look in the propeller manual about it.

    Also. Just type return.

    Everything in SPIN works like you expect in C expect for a few cases. The ">=" and "<=" operators in SPIN are not the same as they are in C. (They assign in spin... you have todo "=>" "=<" instead).
  • AntoineDoinelAntoineDoinel Posts: 312
    edited 2011-02-22 17:52
    Hi and welcome!

    i182willsw wrote: »
    1. Is there a way to have a method return a value or an object? I've seen people just make global variables and then have the methods just write to it. But I'm trying to make a linked list object and want to be able to add and retreave data.

    Sure it's possible to return a value, the following forms are functionally equivalent AFAIK:

    As an argument to "return":
    PUB checkping
      return 42
    

    By assigning the default internal variable "result":
    PUB checkping
      result := 42
    

    Using a named return variable:
    PUB checkping : pong
         pong := 42
    

    Is not normally possible to return an object, but people have figured out a few tricks to implement this.

    i182willsw wrote: »
    2.
    Pub checkping | leftdistance, rightdistance
    
    In a tutorial on Gadgetganster they have this, but leftdistance, and rightdistance are never instantiated as a variable. So is it instantiating it right now and what variable type is it.

    that IS the instantiation of leftdistance and rightdistance, they are local variables to the function "checkping", and the type is always long integer.

    i182willsw wrote: »
    3. Just any tips or tricks you guys have found, a website that has all the libraries and it's methods....
    Thanks guys, I've very excited with the thought of multiple cogs.

    Tips and tricks are just too many to list... you could start with these:

    http://propeller.wikispaces.com/
    http://www.rayslogic.com/propeller/Programming/Programming.htm
    http://ucontroller.com/Propeller%20Protoboard%20Designs%20for%20the%20Beginner.pdf

    Usually the newest stuff is here on the forum, just search or ask right away.


    Regards
    Alessandro
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-22 18:00
    Also worth mentioning, global variables are initialised to zeroA, local variables are not (with the exception of result).

    A There is the exception that - if running from EEPROM - you could modify global variables in the EEPROM image which effectively changes their initial value.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-23 01:56
    Hi i182willsw,

    welcome to propeller world!

    I think when you talk about C, C# and Java you also talk about machines having a lot of memory and even more swap-space. All those languages have memory management build in, dynamic object generation and so on.
    In propeller-world we talk about a microcontroller with very limited resources. I know there are some effords in making the propeller a general purpose computer and make programming the prop as "easy" as for other computers.
    But I think you should brake free from this. Dynamic data handling needs additional memory for managing the dynamic data and functions which take care of the available RAM. So in the end this type of programming eats up RAM and RUNTIME. Not a problem for a PC but it leads to problems on a uC very fast.
    In my opinion the most effective way of programming the prop is to avoid those PC concepts.

    Why do you need a linked list?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-23 07:45
    Kye wrote: »
    Everything in SPIN works like you expect in C expect for a few cases. The ">=" and "<=" operators in SPIN are not the same as they are in C. (They assign in spin... you have todo "=>" "=<" instead).
    There are a few other differences between Spin and C. The expression "repeat i from first to last step incr" is similar to "for (i = first; i <= last; i += incr)", but the Spin version has some odd quirks. Also, "if flag AND i++" is not the same as "if (flag && i++)". The i++ will not execute in C if flag is zero, but it will execute in Spin. There are no global variables, and everything is case insensitive. The \sub and abort are something like setjmp and longjmp, but not the same. Other than that, C and Spin are the same. :)

    The line "Pub checkping | leftdistance, rightdistance" defines checkping as a method with no parameters. leftdistance and rightdistance are long values that are created on the stack. There is also an implicit stack variable name "result", which is initialized to zero and can be used to return a value if an explicit "return value" is not done. In C it would look something like this.
    int checkping(void)
    {
        int result = 0;
        int leftdistance, rightdistance;
        ...
        return result;
    }
    
  • i182willswi182willsw Posts: 3
    edited 2011-02-23 09:00
    WOW, thanks guys it sounds like Spin does have it's place in the MC community, Not to mention I've never gotten so many responses in such a short amount of time. It's nice to see there is an active and knowledgeable community using this chip. As for the linked list I just needed some form of data structure to store menu items or objects that could point to their children object and so on.

    I can tell I'll have to tweak how I attack a program but this should be very fun. Thanks again for all your help.
Sign In or Register to comment.