Shop OBEX P1 Docs P2 Docs Learn Events
Question about shared variables — Parallax Forums

Question about shared variables

Laurent RLaurent R Posts: 27
edited 2009-04-10 08:52 in Propeller 1
Hi all

Here is my goal :
- main cog receive command like launch a new cog to do a task in backgound

- this second cog need to know some variables from the main code so I give him as parameters when he starts

First question : is it the right way or not ?

Second : If the second cog changes a variable, will the first cog see this modification?



VAR

  Byte  AnalogDefaultChan
  Byte  DefaultRange
  Byte  DefaultBip
  
  long  ValAlarmHi              
  Byte  High_Pos                 
  long  ValAlarmLo              
  Byte  Low_Pos                 
  Byte  Latch_Alarm            
  byte  Warning_1270_flag



CogAlarm := cognew(alarm.Start(Warning_1270_flag,ValAlarmHi,High_Pos,ValAlarmHLo,Low_Pos,Latch_Alarm),@15)                                       




Thanks for help

Laurent

Comments

  • mparkmpark Posts: 1,305
    edited 2009-04-09 15:53
    First, yes, that's correct.
    Second, no, the second cog is operating on its own private copies of the first cog's variables. If you want to transmit information from the second cog to the first, you could pass the address of the variables, like so:
    cognew(alarm.Start(@Warning_1270_flag,@ValAlarmHi, etc etc)
    but you'd have to modify the Start method accordingly.
  • SRLMSRLM Posts: 5,045
    edited 2009-04-09 15:55
    You may want to poke around this thread. I think there's an answer in there somewhere.

    http://forums.parallax.com/showthread.php?p=765086
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-09 16:09
    You can't use a method call to another object in a COGNEW or COGINIT ("alarm.Start"). It's not well documented and the compiler won't catch it, but it won't work.
  • mparkmpark Posts: 1,305
    edited 2009-04-09 16:49
    D'oh! Missed that. (Homespun can flag those errors, btw, even if I can't.)
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-09 16:54
    I think there are some examples in the Propeller Education Kit tutorials that you can download that show how to pass global variables from one object to another.

    The compiler sorts global variables in an object according to their base size. Longs are first, then words, then bytes. If you're going to pass groups of variables of the same base size, you can just pass the address of the first variable and reference the others as if you were passing an array (using BYTE / WORD / LONG like LONG[noparse][[/noparse] variable address ] [noparse][[/noparse] subscript ]). You can also pass the addresses of each of the variables if there are only a few.
  • Laurent RLaurent R Posts: 27
    edited 2009-04-10 08:52
    Thanks at all

    I'll see that



    edit :

    @ Mike : And if cognew is in the other object?

    so I call "alarm.start" and the start method open a new cog it's the right way , is'nt it?

    Best regards

    Laurent

    Post Edited (Laurent R) : 4/10/2009 9:01:13 AM GMT
Sign In or Register to comment.