Shop OBEX P1 Docs P2 Docs Learn Events
returning data from a cog — Parallax Forums

returning data from a cog

BitsBits Posts: 414
edited 2012-03-12 01:44 in Propeller 1
I am trying to return data from a cog but want to get it in one operation so to speak.

Here is what I normally end up doing.
Pub Get_spectrometer
     Return spectrometer

Pub Get_polarization
     Return polarization 
 
Pub Get_phase velocity   
     Return  phase velocity

Now what I would love to do is return all 3 data in one call, something like this below.
Pub Get_machine
     Return spectrometer & polarization & phase velocity

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2012-03-09 12:20
    The one and only thing you can return is a long. So, if your variables are bytes you can pack them together in one long.

    return spectrometer << 16 | polarization << 8 | phase_velocity

    If your variables are bigger the only way is to pass an address of a value buffer and let the get-function directly write it to these variables:
    VAR
      long spec, pol, phase
    PUB
      get_all( @spec )
    ....
    PUB get_all( addr )
      long[ addr ][ 0 ] := spectrometer
      long[ addr ][ 1 ] := polarization
      long[ addr ][ 2 ] := phase_velocity
    

    If the variables are in the same *.SPIN-file there is no need at all to return the values, because all parts of this SPIN-file have access to those variables.
  • Mark_TMark_T Posts: 1,981
    edited 2012-03-09 16:07
    You can pass the function an address (or addresses) to place the results in... For instance a 3-vector could be passed back by passing the address of an array of size 3... The callee then writes the results using long[][] into that array for the caller to use.
  • Heater.Heater. Posts: 21,230
    edited 2012-03-12 01:44
    To be clear, what you are talking about is returning data from a method of a Spin object. That method may of may not be running in another COG.
    As noted the only way to return multiple values or complex structures is to pass the address of said structure to the method, have it fill in the results based on that pointer and then return.

    Now this all applies equally well when the Spin method is running in another COG. But in that case, lets assume the COG runs forever, you also have to include something in that data structure that indicates when the result is ready and available. Perhaps one LONG is used as a command to "doSomething" with the data. That command may then be set to "doneSomething" when the COG has written the results back.
Sign In or Register to comment.