feedback on this method
Jack3
Posts: 55
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.
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?
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
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.
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.
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
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.
But, if you really want, you could have four copies of the function with each hard coded to toggle their specific pin.
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).