Shop OBEX P1 Docs P2 Docs Learn Events
Working with variables across cogs- what am I missing? — Parallax Forums

Working with variables across cogs- what am I missing?

RforbesRforbes Posts: 281
edited 2012-11-16 13:11 in Propeller 1
Hey all,

I'm trying to figure out a better/more efficient way of working with variables.

My main object looks like this:
PUB Main

  Comms.Start(@Level_Status,@Sigstr,@IO_0_Status[2],@IO_1_Status[2],@IO_2_Status[2],@IO_3_Status[2],@IO_4_Status[2],@IO_5_Status[2],@IO_6_Status[2],@IO_7_Status[2],@IO_8_Status[2],@IO_9_Status[2],@IO_10_Status[2],@IO_11_Status[2])
  

My Comms object looks like this:
OBJ

  xfds: "FullDuplexSerial" 

VAR
long Cog
long Stack[60]

PUB Start(Level,Signal,IO0,IO1,IO2,IO3,IO4,IO5,IO6,IO7,IO8,IO9,IO10,IO11)    

  Stop

  Cog:=cognew(Main_comms(Level,Signal,IO0,IO1,IO2,IO3,IO4,IO5,IO6,IO7,IO8,IO9,IO10,IO11),@Stack)+1   

PUB Stop                                                 

  if Cog                                                
    cogstop(Cog~-1)                                     

PUB Main_comms(Level,Signal,IO0,IO1,IO2,IO3,IO4,IO5,IO6,IO7,IO8,IO9,IO10,IO11)|Poke


  xfds.Start(26,27,0,9600) '(RXpin, TXpin, Mode, Baud)  ' This gets FullDuplexSerial going

  repeat
    Poke:=xfds.rxTime(10)
    If Poke=="!"
      xfds.tx(long[Level])
      xfds.tx(long[Signal])      
      ''etc etc

I actually need to get about 60 longs into my "Main_comms" method for transmission via the serial.

What can I do to get my Main_comms method to have access to all 60 longs without passing them via parameters? Is there a simple way to achieve this? (Main_comms needs to be ran in a separate cog so that the loop continually monitors for an incoming start signal.)

Thanks in advance
Robert

Comments

  • RforbesRforbes Posts: 281
    edited 2012-11-16 12:36
    Woa, scratch that. I think I just figured it out. Haha!!
    (Thanks for looking! Hopefully I got this figured out.)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-16 12:36
    Just keep all your variables together as a block in the parent object and pass the address of the first variable.

    Then instead of:
    xfds.tx(long[Level])
          xfds.tx(long[Signal])      
        . . .
    

    Use:
    index := Level
      repeat NUMBER_OF_VARIABLES
        xfds.tx(long[indexl])
        index += 4 
    
  • RforbesRforbes Posts: 281
    edited 2012-11-16 12:42
    @Duane- Ok, that makes sense, I think. Thanks again man. Much appreciated :smile:
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-16 13:11
    One think to note though. Most serial object only transmit a single byte with the "tx" method, so only the least significant byte would be transmitted from these variables.

    If you increase the index by one instead of four, you'll send all the data.

    If you want to use ASCII characters for your data, the infor is usually kept in arrays of bytes.

    If you used "dec" instead of "tx" you'd send ASCII characters (up to ten) of the decimal value.
Sign In or Register to comment.