Shop OBEX P1 Docs P2 Docs Learn Events
REading a value from a Cog??? — Parallax Forums

REading a value from a Cog???

SailerManSailerMan Posts: 337
edited 2006-12-02 19:38 in Propeller 1
Basically I have code to Read a Sony REmote running in an endless loop in a cog ....Call it Cog#0.

I want another cog#1 to be·notifyed that data has been recieved in·Cog#0·and what that data is.

Is there a way to do this?

Thanks,
Eric

PS any people in Pennsylvania out there?

·

Comments

  • James LongJames Long Posts: 1,181
    edited 2006-11-29 03:16
    Eric,

    There are multiple ways to do this......depends on if you want to go through the hub or not.

    1. Direct method (real fast).......If you have a extra pin you are not using. Make it an output and set it high when the data is ready. You main loop can look at the pin and retreive the data when the pin goes high.

    2. If you can't go with that option. Make a byte VAR. Slice it up into 8 bit flags. byte[noparse][[/noparse]0] through byte[noparse][[/noparse]7] and you can set those flags high....and have the main loop see if the bit is high or low. When the bit is high retreive the data.

    The data passing is a little different.

    Main loop (cog#1)

    mypassedinformation *8 /2

    Data loop (cog #0)

    long[noparse][[/noparse]mypassedinformation] = what ever data to be passed····· (the size can be byte,word,long,)



    You can thank Beau, Paul, Mike and a few other to get this through my thick skull.

    Hope this helps,

    James L
  • SailerManSailerMan Posts: 337
    edited 2006-11-29 12:36
    I think I understand what you are saying.
    I will try when I get home and ask questions where needed.
    mypassedinformation *8 /2
    

    Is anything specific with the line above?



    Thanks for your help,

    Eric
  • James LongJames Long Posts: 1,181
    edited 2006-11-29 15:16
    Is anything specific with the line above? said...
    (replace this text with what was said)
    No....just that you use the information there.

    James L
  • SailerManSailerMan Posts: 337
    edited 2006-11-29 17:27
    I am planning on making the IR Code an Object...Will I declare (mypassedinformation) in the VAR Block of the IR Object or the Main Program.

    Like I said I can't test this out yet.. I'm just thinking.
  • SailerManSailerMan Posts: 337
    edited 2006-12-01 13:08
    I have tried everything I know, which isn't to much... [noparse]:)[/noparse] to get this to work.

    I created a SonyIRDecoder Spin Object.

    It basically runs in a loop waiting for IR data to come.

    When Data arrives it is decoded into Device ID and Button Code.

    In my Main program I use the object like normal



    IR : "SonyIRDecoder"
     
    Pub Main
       IR.Start(24)
       Repeat
        ------------I want to see the Device ID and Button Code Variables here...
    



    How Do I get access to these? I tried the info below but could not get it to work.

    Regards,

    Eric
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-12-01 17:20
    To communicate data between objects you must pass thier location be reference. So when you call IR.Start it will have two arguments, the pin assignment and the location of the variables. The code within your SonyIRDecoder uses the pointer to the base address of the variable block passed to it to assign the data a value which can then be read by the master object.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • acantostegaacantostega Posts: 105
    edited 2006-12-01 17:44
    Hi,
    I'm sure this has been addressed before in the forums, so you might want to struggle with the seach engine to find some of those threads smile.gif
    One approach I recall is to have your IR object communicate with other cogs through a shared variable. Say you want to share the value in the variable "Info". Then have your IR object something like:

    'IRthingie code
    Var
       long myInfoAddress
    
    PUB start(InfoAddress)
      myInfoAddress := InfoAddress ' copy address of info variable
      ' then start infinite loop in a separate cog as usual
    
    
    


    in which you pass the address of a variable, probably a global variable in your "main" code. So from your main code you do something like:
    ' "Main" code
    Var 
       byte Info 'or word, long
    Obj
       IRthingie : "IRThingie"
    PUB start
       IRthingie.start(@Info) ' pass address of info to IR object
    
    


    The IR object would update the Info variable in the code running as an endless loop in the separate cog, like so:
    ' IRThingie code
    newvalue := ... 'some routine to get new value
    byte[noparse][[/noparse]myInfoAddress]:= newvalue ' update Info variable, use byte[noparse][[/noparse]] to dereference myInfoAddress
    
    


    The "client", perhaps the "main" thread (which instantiated the IRthingie object) will periodically consult the Info variable as usual:
    ' Main code
    if (info == something)
       doSomething()
    ' etc
    
    


    To ensure the data in info is "fresh", you might set it to an "illegal" value (e.g. -1) from your "main" code after reading it, and then poll info until it takes a value different from -1.
    I haven't taken into account concurrency problems. If you just update a 32-bit or less variable, there'll be no problem because read/write operations on these are atomic. If it's not the case, you can use semaphores or a buffer approach.
    Warning: I haven't tested this code and it might have mistakes. There also might be a better way, I'm mainly translating from a C mental model. I'm sure Mike Green has explained this better somewhere smile.gif
  • SailerManSailerMan Posts: 337
    edited 2006-12-02 19:38
    It's all working now... thanks for the help... I'm learning new things every day. [noparse]:)[/noparse]

    Best Regards,
    Eric
Sign In or Register to comment.