Shop Learn
Passing bytes from parent to child object question — Parallax Forums

Passing bytes from parent to child object question

In my parent object I have a 4 byte array.

Var
byte Buff1[4]

And in my child object I also have the same.

VAR
byte Buff2[4]

Lets say in the parent object I want to pass all the contents of Buff1 to the child objects Buff2.
In the parent object method I do this...

PUB Some method

ChildObject.Transfer(Buff[0], Buff[1], Buff[2], Buff[3])

In the child object I have a method called Transfer and receive the 4 bytes to Buff2 in the child object.

PUB Transfer(data1, data2, data3, data4)

Buff2[0] := data1
Buff2[1] := data2
Buff2[2] := data3
Buff2[3] := data4

This works for me but was wondering if there is a simpler way to send the Buff1 as a whole to the child objects Buff2?

Comments

  • Cluso99Cluso99 Posts: 17,677

    Do you always want to use the "same" buffer for both objects?

    If yes, just put them once in a DAT and pass the address as a pointer to the child object.

  • JonnyMacJonnyMac Posts: 7,548
    edited 2021-03-12 17:14

    Easy-peasy. This method is in the child object, but called from the parent.

    pub transfer(p_src)
    
      bytemove(@buf2, p_src, 4)
    

    Call it like this:

      child.transfer(@buf1)
    

    Another thought is you could just pass the address of buf1 to the child during setup. This would allow the child object use buf1 without the explicit transfer requirement. Instead of this:

      do_something(buf2[idx])
    

    ... you would do this:

      do_something(byte[p_buf][idx])
    

    In the second case the variable p_buf is a pointer to buf1. This was passed to the child during setup.

Sign In or Register to comment.