Shop OBEX P1 Docs P2 Docs Learn Events
Sharing variables between cogs ? — Parallax Forums

Sharing variables between cogs ?

FORDFORD Posts: 221
edited 2012-10-16 02:56 in Propeller 1
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

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-10-16 02:03
    There's a big difference between cogs and objects. You're limited in how you can access variables from different objects (included Spin files). As long as your program is written in Spin, your program can read from and write to any of the variables with in an object.

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-10-16 02:33
    Here's an example where one cog counts up while the other cog takes care of input from and output to the terminal window.
    CON
      _CLKMODE = XTAL1 + PLL16X
      _CLKFREQ = 80_000_000
      DEBUG_BAUD = 115_200
      
    VAR
      long stack[30]
      long count
      byte inputFlag
      
    OBJ
      Debug : "FullDuplexSerial"
      
    PUB Setup 
      waitcnt(clkfreq * 3 + cnt)    ' time to open terminal window.
      
      Debug.start(31, 30, 0, DEBUG_BAUD)
        
      cognew(DebugCog, @stack) 
      MainLoop
    PUB MainLoop
    '' This method increments the value of "count".
    '' If the "inputFlag" variable is greater than zero, the value
    '' of "count" is reset to zero.
      repeat
        if inputFlag > 0
          inputFlag--
          count := 0
        waitcnt(clkfreq / 2 + cnt)
        count++  
        
    PUB DebugCog | character, previousCount
    '' This method takes care of all input and output to terminal window.
    '' If input occurs, one is added to "inputFlag".
    '' This method displays the value of "count" whenever it changes.
      previousCount := -1           ' make sure previousCount doesn't equal "count"
      Debug.Str(string(13, "Press any key to reset count."))
      repeat
        character := Debug.Rxcheck
        if character > 0            ' watch for key press
          inputFlag++
        if count <> previousCount
          previousCount := count
          Debug.Str(string(13, "count = "))
          Debug.Dec(count)
      
    


    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.
  • FORDFORD Posts: 221
    edited 2012-10-16 02:38
    Thanks Duane,
    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 ]
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-10-16 02:56
    It looks like you've got this figured out. Great.

    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.

    attachment.php?attachmentid=78421&d=1297987572
Sign In or Register to comment.