Shop OBEX P1 Docs P2 Docs Learn Events
feedback on this method — Parallax Forums

feedback on this method

Jack3Jack3 Posts: 55
edited 2014-02-17 11:00 in Propeller 1
Smart ones....
Is this snippet an acceptable method to define cogs

from the current cog, get the cog id, lock it, then check that the current cog +1 is not running, if so stop it, start a new cog, lock it, etc.
static volatile int cog; 

 unsigned int stack1[65];
 unsigned int stack2[65];
 unsigned int stack3[65];
 unsigned int stack4[65];
 

 void Blink1(void *B1);
 void Blink2(void *B2);
 void Solid1(void *S1);
 void CheckCog(void);
 

 

 int main(void)
 { 
 

    int cog = cogid();  //get the current cog
    int lockid=locknew();  //get a lock ID
    lockset(lockid);  //use lock id to lock current cog and the rest should follow
                     //due to hard code below
    
     CheckCog();  //function to make sure not already a cog and stop if so 
                 // as all cogs will be locked after reaching their loop
 

     cog = cogstart(&Blink1, NULL, stack1, sizeof(stack1));
     lockset(lockid+1); 
     
     CheckCog();
     cog = cogstart(&Blink2, NULL, stack2, sizeof(stack2));
     lockset(lockid+1);  //by following the new cog which is also a +1 it stays in sync
   
   return 0;
 }

 void CheckCog()  // make sure cog not already running
 {
   cog = cog+1;  //increment cog for next check
     if (cog)      // if it is, stop it so a new one can start
       {
         cogstop(cog);
       }
   return;


Just want to make sure I am not getting into trouble..........

Another question.....if I start pins to high in a function, do I need to continue a loop or will they stay high without further intervention?

Comments

  • SRLMSRLM Posts: 5,045
    edited 2014-02-16 18:48
    What are you trying to accomplish? Typically, we just cogstart a new cog and everything works out. If there are no more cogs available then cogstart will return -1 (no cog allocated).

    One problem that I do see is that you're using lockid+1. While this will technically work it won't play well with others: locks should be checked out (locknew() ) first, and your lockid+1 is not.
  • Jack3Jack3 Posts: 55
    edited 2014-02-16 19:39
    I wondered if I should just use another locknew().

    I am trying to run several led functions continuously and independently. I know I waste resources by using a whole cog for this, but I am a beginner and just getting new cogs going without issue is the first goal. I am a neonate programmer. I am learning. So this is as much to learn as to finish a project. This particular project is a duplicate of a Spin program. Trying to see which I prefer to write in, trying to learn a lot more about writing code, especially in C and C++.

    So play, learn, and have a project.....waiting on parts for it, so writing code.
  • SRLMSRLM Posts: 5,045
    edited 2014-02-16 19:47
    But why not just do something like:
    cogA = cogstart(Blink, (void*)pinA, stackA, sizeof(stackA));
    cogB = cogstart(Blink, (void*)pinB, stackB, sizeof(stackB));
    cogC = cogstart(Blink, (void*)pinC, stackC, sizeof(stackC));
    cogD = cogstart(Blink, (void*)pinD, stackD, sizeof(stackD));
    

    BTW, take a look at the cogstart function example in propeller.h documentation: https://sites.google.com/site/propellergcc/documentation/libraries/propeller-h-library#TOC-cogstart
  • Jack3Jack3 Posts: 55
    edited 2014-02-16 20:24
    Well first of all I thought one was supposed to check and make sure the cog you are starting isn't clobbering another one. Sure your way sounds nice and simple but doesn't take into account anything getting clobbered....but I know little here, so educate me. By locking a cog I just opened, it can't get clobbered, correct? This will be a stand alone thing that boots at power up and just runs continuously.

    My pins are global and static so I don't need to pass them around I don't think, maybe I do, I haven't wired this up yet. I will need to pass in one value \back and forth with a function in the future in a cog, but not that far yet. Just about tho

    I had been looking at that function you linked me to, Been all over that page today.

    I appreciate your help.
  • SRLMSRLM Posts: 5,045
    edited 2014-02-16 20:37
    What do you mean by clobbered? cogstart will only start free cogs, and doesn't affect other running cogs. The purpose of the pinA/B/C/D in my example is to pass in to a common function the specific pin to blink (such as in the cogstart example).

    But, if you really want, you could have four copies of the function with each hard coded to toggle their specific pin.
  • ersmithersmith Posts: 6,054
    edited 2014-02-17 08:57
    Jack3:

    I think you're confused about locks and cogs. Locks have nothing to do with cogstart; the two concepts are completely independent. "Setting" lock #1, for example, will have no effect on any attempt to start cog #1. Locks are used to coordinate access to shared resources, so they only need to be used when two cogs might want to access the some specific memory (or pin) at the same time.

    As SRLM mentioned, cogstart will always allocate a new cog. If all cogs are already in use, it will fail (return -1). It *is* possible, using the coginit() macro, to start a specific cog rather than "the next free cog", but that's not what cogstart does (and it's not something most users will normally want to do).
  • Jack3Jack3 Posts: 55
    edited 2014-02-17 11:00
    Okay, I understand now. I thought that was something to worry about. Simple change.
Sign In or Register to comment.