Shop OBEX P1 Docs P2 Docs Learn Events
How do I pass a byte array between objects? — Parallax Forums

How do I pass a byte array between objects?

ChemikalChemikal Posts: 32
edited 2009-11-05 15:09 in Propeller 1
Evening,

I have, what I think, is going to be a pretty cool XBee object eventually, but I am stuck at something I have never known how to do, and rather than sidestepping it like I usually do, there must be a simple way to do it...

In my XBee object, I am forming a byte array in a "Receive" PUB statement, and I would like to pass it back to the object that originally called the PUB... I was thinking I would send the pointer to the Main object, and then itterate through the remote array and store it to a local byte array before it gets overwritten...


This is what I have right now, trying to get it to work:


Main object: (simplified) - **brackets replaced with {}

VAR
  byte PacketPtr

PUB Main
  repeat

    PacketPtr := XB.receive

    SER.str(CONSOLE,string("PUB: "))
    SER.hex(CONSOLE,byte{@PacketPtr}{3},2)  'byte 3 of the packet is $90, which is just one thing I want to retrieve
    SER.str(CONSOLE,string(10,13))
    
    waitcnt(clkfreq + cnt)




XBee object:

VAR
  byte API_Packet{200}

PUB Receive
  ... fill the byte array, etc
  return @API_Packet




Doesn't work. So how do I pass the byte array to the Main object?

Thanks for the help... these XBees are going to be handy soon...

Comments

  • photomankcphotomankc Posts: 943
    edited 2009-11-05 06:34
    If I understand what you have there correct, lose the {3} notation with the pointer as well as the @symbol. To access byte 3 of the array you would just add 3 to the base address of the array so use:

    SER.hex(CONSOLE, byte{PacketPtr+3} ,2)

    You don't use the @ sign because the variable's value IS already the address.

    ETA: Fixed the goof with the braces.· I think that's right.

    Post Edited (photomankc) : 11/5/2009 6:42:59 AM GMT

  • Cluso99Cluso99 Posts: 18,069
    edited 2009-11-05 06:39
    The easiest way is to look at the FullDuplexSerial code as it does what I think you are after. It has a transmit and a receive fifo buffer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)
    · Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • ChemikalChemikal Posts: 32
    edited 2009-11-05 07:06
    Got it - it was because of PacketPtr being set to a "byte" and not a "long" for the address.

    SER.hex(CONSOLE,byte{PacketPtr}{3},2) works now


    One more question:
    Can I send more than one variable back to the Main Object?
    I mean, now that I have the address for the byte array, I would also like to send back the variable that is calculated while it is receiving: the byte array length, but the 'return' statement only takes one argument... I am thinking of just referring to another PUB statement, such as "GetLength" or something, but I think that'd be rather a messy method of doing this...

    Any way to send that back as well, or any suggestions?


    EDIT: forgot the braces
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-11-05 08:34
    It's not messy writing functions to access object-data ... that's very usual in object oriented programming and it's the clean way.

    It would be messy if you pack that information into the return value as well (because currently you only need 16 bit of the 32 bits available). So, you should not use
    return @API_Packet + API_Len<<16
    ;o)

    And returning a pointer to an array of return values would be ugly as well.

    But we talk about microcontroller programming, so ugly or messy tricks are allowed when you run out of memory. Of course we don't want to see such tricks in official ObEx objects ;o)·
  • photomankcphotomankc Posts: 943
    edited 2009-11-05 14:33
    One way to get back multiple values is not to use return value but use pointers in the function parameters to variables the called function will populate:

    PUB main | v1, v2, v3
    ...
      FillVars(12, @v1, @v2, @v3)
    ...
    
    
    PUB FillVars(value, v1, v2, v3)
      long[noparse][[/noparse]v1] := value
      long[noparse][[/noparse]v2] := value
      long[noparse][[/noparse]v3] := value
    
    





    After FillVars executes the values of v1 through v3 should now be 12. It can get confusing when you do that later on down the road if the name of the function doesn't suggest what is going to happen but it's an effective way to get multiple values back from a function.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-11-05 15:09
    photomankc,

    You don't need to specify the address fro each variable, this can get very messy with large numbers of variables.

    Using your example....

    [b]PUB[/b] main | v1, v2, v3
    ...
      FillVars(12, @v1)  '<- Just pass the address of the first variable
    ...
    
    
    [b]PUB[/b] FillVars(value, v1)
      [b]long[/b][noparse][[/noparse]*v1][noparse][[/noparse]*0] := value
      [b]long[/b][noparse][[/noparse]*v1][noparse][[/noparse]*1] := value
      [b]long[/b][noparse][[/noparse]*v1][noparse][[/noparse]*2] := value
    
    
    





    This same topic came up between me and one of my co-workers recently...

    Attached are two examples of passing variables between COGs without using 'copies' of variables. The first example Var Pass Test is a very simple illustration that uses the DEMO board LED’s, showing how to pass values from one cog to another. The second file Var Pass Test v2 is basically another way of doing what the first file does, but it ensures that the value of the variable can be changed at the source after the DisplayVariables cog has been launched. In other words more of a sanity check to make sure that variables we are working with are ‘live’ and not a copy. There are probably other ways to pass variables from one cog to the next, but this seems to make the most sense to me.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 11/5/2009 3:15:55 PM GMT
Sign In or Register to comment.