Shop OBEX P1 Docs P2 Docs Learn Events
Passing an array to a PUB? — Parallax Forums

Passing an array to a PUB?

HughHugh Posts: 362
edited 2014-02-02 09:15 in Propeller 1
Can I pass arrays to a PUB?

e.g., if I have two arrays, arrayFirst[16] and arraySecond[16] can I call a PUB by, for example, dostuff[arrayFirst] and use either of these arrays, handling either by the same code?

I'd prefer to it this way, but one big array and multiple offsets might be another.

I've been going round in circles with various attempts at this.

Cheers
Hugh

Comments

  • RaymanRayman Posts: 14,659
    edited 2014-01-31 12:33
    Yes, you can use the @ operator to pass the address on an array.
    For your example, you'd use dostuff(@arrayFirst).

    Then, you'd have something like:

    Pub dostuff|pArray,y

    and one way to access the array (if it is a long array) is
    x:=long[pArray][y] where y is the index

    PS: Also see below for "Similar threads"
  • HughHugh Posts: 362
    edited 2014-01-31 12:46
    Thank you for the true enlightenment!
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-01-31 12:50
    As Ray points out, you can -- by passing the address of the array. Keep in mind that the code in the receiving method must know the type off array you're passing, a Spin can deal with bytes, words, and longs. For example, let's say you have a method called print_numbers() that will an array (of longs) for you. You could code it like this:
    pub print_numbers(p_list, n) | idx
    
      idx := 0
    
      repeat n
        serial.dec(long[p_list][idx++]
        serial.tx(13)
    


    If you want to make it more flexible, you could pass the array type (bytes per element) like this:
    pub print_numbers(p_list, type, n) | idx
    
      idx := 0
    
      repeat n
        case type
          4:
            serial.dec(long[p_list][idx++]
          2:
            serial.dec(word[p_list][idx++]
          1:
            serial.dec(byte[p_list][idx++]
          other:
            return
        serial.tx(13)
    
  • HughHugh Posts: 362
    edited 2014-02-02 09:15
    Thanks you - all sorted and working well.
Sign In or Register to comment.