Shop OBEX P1 Docs P2 Docs Learn Events
Vars from one cog to another — Parallax Forums

Vars from one cog to another

TJHJTJHJ Posts: 243
edited 2008-03-14 22:45 in Propeller 1
Ok I am new.... To the whole prop world, and would like to say thank you for your time.

I have been reading and trying to wrap my head around this as much as I can but I just cant seem to get it. So here I am for help.

The simple question is how do I get variable Information from one cog to another.

Where I get lost is that the hub, must pass the cog, in order for it to take the data and then pass it on. In a quick infinite loop code how do I not miss this window? Or will it set it there and the hub will grab it next pass? I cant seem to find a command that would be Wait for Hub pickup.

I am sorry this is so Noob question, but Im just so lost now.
So regardless of if the hub picks it up or not, How do I use it.

So as for a coding example for the real side of it.
Object Timer to be loaded into cog 1
// Timer.Spin
Var
Word Timestart, Timeend, Time

Pub Timer // Have tried Pub Timer(Time) or Pub Timer |Time see further down
  Repeat
    Waitpeq(%000001,<|5, 0) '' Wait for the pin to go high, to start timing the high pulse. 
    Timestart := Cnt '' Store the Time to one var. 
    Waitpeq(%000000, <|5,0) '' Wait for the end of the pulse, low side
    Timeend := Cnt '' Store the end time
    Time := Timeend-timestart 
       // So here is where im lost, How do I get this value out so I can use it in a different cog. 





Object 2 to be loaded into cog 2.


// usetime.spin
Pub Main    // Have tried Pub main(Time) or Pub Main |Time see further down
  Repeat
    If Time >= 100   // So if the pulse is long enough do something. 
      DO SOMETHING // anything I guess





Ok so now my boot program, or main program would look like this.

OBJ
Timer : "Timer"
Use : "Usetime"

Var
Word Time
long Stack[noparse][[/noparse]9]

Pub Start
  Cognew(Timer.timer, @Stack) '' Put this timer thing into a new cog. Hopefully 1. 
  Cognew(Use.Main,@stack) '' Put this one into another cog, 2






Now at this point there is no way it makes any sense that Use can access timer. According to the manual, cant use vars outside of that object.

So then maybe


OBJ
Timer : "Timer"
Use : "Usetime"

Var
Word Time
long Stack[noparse][[/noparse]9]

Pub Start
  Cognew(Timer.timer(Time), @Stack) ''. So It would be a Var in a var?? 
  Cognew(Use.Main(Time) ,@stack) '' Use this Var in Var I got. 




But that doesnt seem to get me anywhere.
So what if I Use the return from the timer obj


OBJ
Timer : "Timer"
Use : "Usetime"

Var
Word Time
long Stack[noparse][[/noparse]9]

Pub Start
  Cognew(Timer.timer |Time , @Stack) '' I think I only get this variable 1 time... Not a constant update
  Cognew(Use.Main(Time),@stack) '' Try and use time





So last thought was Repeat it, but then all I do is keep writing cog after cog and starting it.
and I still have not been able to use the value I got from the time obj.



OBJ
Timer : "Timer"
Use : "Usetime"

Var
Word Time
long Stack[noparse][[/noparse]9]

Pub Start
  Repeat ''Use a whole bunch of cogs
    Cognew(Timer.timer|Time, @Stack)  
    Cognew(Use.Main(Timer),@stack) 





So this is where I am at, I thank anyone who takes the time to read this for at least doing that, and if anyone can help me I would greatly appreciate it.
I am sorry this is so basic.

Thank you
TJ

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-03-14 22:10
    First of all, don't be sorry, this is what this forum is forsmile.gif and welcome to the forums.

    1. Please make the stack for the new cogs more than 9. You also need a different stack for each cog so you need
    VAR
      long Stack1[noparse][[/noparse]20]  '20 is really the absolute minimum you should use
      long Stack2[noparse][[/noparse]20]
    



    2. Unfourtantly, although it doesn't say it in the manual you can't do
    cogNew(Timer.timer,@Stack)
    


    The method in cogNew must always be in the same object. The way around this is to simply add a new method to the object that you want to run like this
    '''Timer.spin 'another note, comments are ', '', {....} and {{...}} in spin. // is the modulo operator.
    Var
    Word Timestart, Timeend, Time
    
    PUB Start(stackPtr)
      cognew(Timer,stackPtr)
    
    Pub Timer // Have tried Pub Timer(Time) or Pub Timer |Time see further down
      Repeat
        Waitpeq(%000001,<|5, 0) '' Wait for the pin to go high, to start timing the high pulse. 
        Timestart := Cnt '' Store the Time to one var. 
        Waitpeq(%000000, <|5,0) '' Wait for the end of the pulse, low side
        Timeend := Cnt '' Store the end time
        Time := Timeend-timestart
    


    Now you simply need to call the Start method. You should really also store the cogId and add a stop method but this is all in the propeller manual in the spin section.

    3. Now to what your question is about. You need to learn about pointers and passing by reference. Have a read of my post in this thread http://forums.parallax.com/showthread.php?p=715624 about pointers. If that helps good, if it doesn't than we can discuss it some more heresmile.gif

    4. Watch out for the difference between '>=' and '=>'. One is an assignment operator and the other is a comparative operator.

    Good Lucksmile.gif
  • TJHJTJHJ Posts: 243
    edited 2008-03-14 22:32
    Thanks for the quick reply,
    But wont each time I call the start method, I will place that code into a new cog? and start the repeat loop?
    or does it skip that instruction knowing that its started?
    I tried to keep it simple so I don't to lost.

    When I call the Start method. It would excaute and return. I think, but would requre a start trigger each time.

    What I think I want to do, Is start timer in Cog one and have it constantly looping, and updating the "Variable".
    Then Cog 2, each time it gets to where ever it is in its loop, can read the Time from cog one's Variable. Regardless if has been updated or not, if not it would just get the previous value.

    Maybe Im looking at this wrong.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-03-14 22:45
    TJ said...
    But wont each time I call the start method, I will place that code into a new cog? and start the repeat loop?
    That is correct. And since your repeat loop never stops the cog will never stop so it will just keep starting new cogs. That is until you run out of cogs.
    TJ said...
    What I think I want to do, Is start timer in Cog one and have it constantly looping, and updating the "Variable".
    Then Cog 2, each time it gets to where ever it is in its loop, can read the Time from cog one's Variable. Regardless if has been updated or not, if not it would just get the previous value.
    If you put this in your main object than that is exactly what it will do.
    OBJ
    Timer : "Timer"
    Use : "Usetime"
    
    Var
    Word Time
    long Stack1[noparse][[/noparse]20]
    long stack2[noparse][[/noparse]20]
    long timerValue
    
    Pub Start
      Timer.Start(@stack1,@timerValue) '' Put this timer thing into a new cog. Hopefully 1. 
      Use.Start(@stack2,@timerValue) '' Put this one into another cog, 2
    


    So in cog1 this will start to run. It will start Timer in cog2, Use in cog3 and will then stop so you will have just 2 cogs running.

    Note that I have added an extra parameter '@timerValue'. This is a pointer and will allow you to share the timerValue variable. But, you need to understand about pointers to use it.
Sign In or Register to comment.