Shop OBEX P1 Docs P2 Docs Learn Events
I'm Lost trying to interupt a method than restarting it. — Parallax Forums

I'm Lost trying to interupt a method than restarting it.

lockadoclockadoc Posts: 115
edited 2010-02-11 09:06 in Propeller 1
I'm hitting a brick wall trying to get this,I made a sample object to try to fiquire it out( with no luck)
Can some-one modify what Ive written·?· this way I can see how its done .
I don't know Assembly at all ,and an example of it I have Just loses me.

What I am trying to do is
have one method running in a cog (method1)
have a second method running in a different cog (method2)

method1 runs a lightshow·until method2 gets an input on a button than it interupts·or stops method1
while it·runs the rest of method2 's light command, than returns or restarts method1's lightshow

Thanks
BillS Louisville KY

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-11 06:40
    You can't do it that way (as you've discovered). There's no way to restart a cog once it's been stopped except to completely reinitialize it and start it over from the beginning (with a COGNEW or COGINIT).

    Cog 1 really has to check for a signal of some sort from cog 2, then wait for cog 2 to remove the signal before it continues. You can use a lock (semaphore) for a signal or an otherwise unused I/O pin or a hub memory location. The simplest way is for the two cogs to share a long variable. If method1 is started first, it initializes the location to zero. When method2 is started, it's passed the address of the shared variable. When method2 gets its button input, it sets the shared variable to -1. When method2 is finished with its light command, it sets the shared variable back to zero. When method1 is doing its lightshow, it periodically checks the value of the shared variable. If it's zero, method1 continues with its show. If it's -1, method1 waits a little while (using WAITCNT), then checks the shared variable again.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-02-11 07:13
    I did not have a look into your code, so it's just a theoretical add-on to what Mike said.

    Maybe it's important to have a 2 way signalling?! Cog 2 signals that it received a button-event, but it only starts when COG1 aknowledges to be in wait-mode. You still need only one long, but one COG sets and clears bit 0 while the other one sets and clears bit 1.

    This would be important, when both COGs use the same I/O pins. Because user-input can occur anytime COG1 could be at the beginning of it's loop or at the end of it's loop. In one case it needs longer to stop.

    If timing is not an issue, the easy solution is: Let COG 2 wait for a while after setting the signal to make sure COG1 finished the code and is in wait-mode.

    Hope that does not confuse more than it helps ;o)
  • cgraceycgracey Posts: 14,256
    edited 2010-02-11 09:06
    I haven't looked at your code, but we ran into a problem like this at Parallax and it took us a long time to figure out what was going wrong...

    Basically, it you have a Spin method running and you want to re-initialize that cog to get it running another Spin method, you must be aware of some stack dynamics.

    When a Spin program is running, it is using its stack space all the time for branching and math. When you do a COGINIT(cog#, newmethod, stackpointer) on a cog that you want to re-start onto another method using the same stack space, things will blow up. The reason this happens is that the COGINIT instruction goes about initializing a new stack before actually doing the COGINIT. Meanwhile, the cog you want to re-start is using that same space for its own activity. This clobbers things. There are two ways around this:

    1) Do a COGSTOP before the COGINIT (easiest).

    2) Go ahead and just do a COGINIT, but use a different stack space. This is the way it must be done if the cog to be restarted is the one doing the restarting. You could have two stack spaces that you just toggle between when you do the COGINITs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
Sign In or Register to comment.