Shop OBEX P1 Docs P2 Docs Learn Events
Purpose of "Stop" method? — Parallax Forums

Purpose of "Stop" method?

lardomlardom Posts: 1,659
edited 2012-11-18 09:13 in Propeller 1
I ran into a problem trying to find a way to 'restart' a cog after I used cogstop(1). The only way I could get the repeat loop to run was to comment out the "Stop" method call. Why is this?

Parent object:
OBJ
    ct : "CognewTest"   

PUB main 

  dira[11]~~                     
  repeat       
    ct.Start     
    waitcnt(clkfreq + cnt)     
    cogstop(1)      
    waitcnt(clkfreq + cnt)     

Child object:
VAR

   long stack[6]
   byte cog
        
PUB Start : success 

   ' Stop                                                    'remove the cogstop command to restart the cog
    success := cog := cognew(main, @stack + 1)
 

PUB Stop

    if cog
      cogstop(cog~ - 1)  


Pub main

   dira[11]~~
    repeat 200
      outa[11]~~
      waitcnt(clkfreq/10 + cnt)
      outa[11]~
      waitcnt(clkfreq/10 + cnt)

Comments

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-11-17 08:37
    Should probably be,
    [FONT=arial]cognew(main, @stack[COLOR=#FF0000][/COLOR][SIZE=1][COLOR=#FF0000])[/COLOR][/SIZE] + 1  ' <--- add one to cog#, not to @stack[/FONT]
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-17 08:38
    First of all, it's bad form (and problematic) to assume that you know what functional cog goes with which number. There's also a reason why the Start and Stop methods of most objects that start a cog are written the way they are. The biggest reason is that resources for the cog, like the stack area, are only provided once. If you somehow try to start another cog while an existing one is still using the resources, none of it will work. That's why the Start method first calls Stop ... to make sure no other cog is running the same code (and using the same variables).

    Normally, the bootloader uses cog 0, then starts the program using the same cog with all other cogs stopped. If, for some reason, you use some other mechanism to start up your program, like loading it from an SD card using some kind of other loader, all bets are off. Your program could start up in any cog. For most situations, they're all identical.
  • lardomlardom Posts: 1,659
    edited 2012-11-17 09:21
    @Tracy Allen, That did the trick. I copied your code and explanation so I can see the difference.
    @Mike Green, I replaced cogstop(1) with cogstop(ct.Start) and cogstop(ct.main) but that did not stop the cog. I see the danger of resource conflicts. (I did not try cogstop(ct.Stop) but I'm not through experimenting.)
  • lardomlardom Posts: 1,659
    edited 2012-11-17 11:04
    @Mike and Tracy, I think I have it figured out. I will copy the cogid to a variable in the parent object. I will test it later on today. Thanks. This problem kept me up late.
  • JonnyMacJonnyMac Posts: 9,110
    edited 2012-11-17 14:10
    Keep in mind that cognew will give you the cog id. The reason that most start methods add one is that this allows the return to be 0 (for no cog) or 1-to-8 (for a launched) cog; the former may be interpreted as false, the latter as true.
  • lardomlardom Posts: 1,659
    edited 2012-11-17 21:32
    @JonnyMac, thanks for the additional info. I can be dense at times when it comes to things mentally getting through. Boolean true or false is one of those things.

    In the modified code below, which works, I subtracted "1". I'm assuming the remainder will always equal cogid. I'm going to test it by restarting several cogs.
    PUB main  | A                      
      
      repeat       
        A := ct.Start     
        waitcnt(clkfreq + cnt)     
        cogstop(A - 1)
        waitcnt(clkfreq + cnt)
    
  • JonnyMacJonnyMac Posts: 9,110
    edited 2012-11-18 07:41
    If you're going through the trouble of creating an object with a .start method it would be good form to provide the complimentary .stop method -- don't force the user to know how to stop/unload your object.
  • lardomlardom Posts: 1,659
    edited 2012-11-18 08:52
    You are absolutely right. I don't recall a .stop method so I'll look at the .start method as a template. You are also right about other users: I'd like to share what I've learned if anyone can benefit. It's important to me that this forum's investment in my growth extend to others.
  • lardomlardom Posts: 1,659
    edited 2012-11-18 09:13
    @JonnyMac, Wow. I can't believe I didn't try that in the beginning! All I did was call the .Stop method. Thank you.:smile:

    (BTW, I did admit I was dense!)
    PUB main                 
      
        repeat       
          ct.Start     
          waitcnt(clkfreq + cnt)     
          ct.Stop
          waitcnt(clkfreq + cnt)
    
Sign In or Register to comment.