Sharing variables between cogs ?
FORD
Posts: 221
Hi to All,
I'm a prop newbie, so please be patient.
I am having trouble getting my head around how to share a variable between cogs.
What I want to do is...
In cogA - count the number of button presses, and have that variable always available to other cogs.
In cogB - display the number from within another cog, ie to an lcd.
I can do the lcd etc, its just the memory sharing I just cant seem to get my head around.
If anyone can point me to a nice example it would be much appreciated.
Cheers,
Chris
I'm a prop newbie, so please be patient.
I am having trouble getting my head around how to share a variable between cogs.
What I want to do is...
In cogA - count the number of button presses, and have that variable always available to other cogs.
In cogB - display the number from within another cog, ie to an lcd.
I can do the lcd etc, its just the memory sharing I just cant seem to get my head around.
If anyone can point me to a nice example it would be much appreciated.
Cheers,
Chris
Comments
What kind of Propeller board do you have and what kind of input and output do you want an example to contain?
It would be easiest for me to write a quick example (or use one I already have) if you could use either a QuickStart board's LED for output or a terminal window like Parallax Serial Terminal.exe. It would also be convenient to used the terminal window for input.
There's an example at Parallax Semiconductor but unfortunately, the last time I checked it still had a serious error in the code.
Cog #0 increments the value of "count" about every half a second. Cog #0 checks the value of "inputFlag" each loop and resets "count" to zero if "inputFlag" is greater than one.
Cog #1 checks for input from the terminal and increments the value of "inputFlag" if any input is detected. The "FullDuplexSerial" method "CheckRx" returns -1 if no key is pressed. Cog #1 displays the value of "count" when ever it changes. Cog #1 uses the local variable "previousCount" in order to check for a changed value of "count".
The variables "count" and "inputFlag" are both global variables and can be accessed from any method within this object (Spin file). It doesn't matter to the program, which cog the method is running in. All the cogs with in the object can access the global variables.
There are tricks to accessing variables when you start using PASM. PASM variables that are part of cog RAM may not be directly accessed by other cogs.
Variable declared in the "VAR" section are stored in hub RAM where all the cogs may access them.
I actually just sorted it out, I had more than just variable assignments to deal with.
So consider this one solved.
I declared a VAR called PRESSES.
I then started 3 cogs...
First to count PRESSES ++ each time an input pin is taken low (pressed).
Second other cog simply flashes 0.5 sec pulses of that number of presses in a REPEAT PRESSES loop, pausing for 3 seconds after each run.
Third to monitor another input pin which sets PRESSES to 0, stopping the LED flashing.
It all works fine.
Code shown below, not sure ifs its useful to anyone though...
Thanks again for your reply and offer to help mate.
Cheers,
Chris
[ code ]
VAR
long stack[30] , presses
'
PUB LaunchBlinkCogs
cognew(Blink(4, 0), @stack[0])
cognew(PressCount(21), @stack[10])
cognew(PressReset(22), @stack[20])
PUB Blink(Pin, reps)
dira[pin]~~
outa[pin]~
presses := reps
repeat
repeat Presses '1 indent
waitcnt(rate/2 + cnt) '2 indents
outa[pin] := 1 '2 indents
waitcnt(rate/2 + cnt) '2 indents
outa[pin] := 0 '2 indents
waitcnt(clkfreq * 3 + cnt) '1 indent
PUB Presscount(Pin)
dira[pin] := 0
dira[3] := 1
repeat
repeat while ina[Pin] == 0 '1 indent
presses ++ '2 indents
outa[3] := 1 '2 indents
waitcnt(clkfreq/4 + cnt) '2 indents
outa[3] := 0 '2 indents
repeat while ina[Pin] == 1 '2 indents
waitcnt(clkfreq/10 + cnt) '3 indents
PUB PressReset(Pin)
dira[pin] := 0
repeat
if ina[pin] == 0 '1 indents
presses := 0 '2 indents
[/ code ]
It's a lot easier to read code on the forum when code blocks are used.
Phil wrote a tutorial (and icon) on how to use code blocks.
Here's the link.