Shop OBEX P1 Docs P2 Docs Learn Events
How best to transfer the data to the object (when there are many). — Parallax Forums

How best to transfer the data to the object (when there are many).

AlarusAlarus Posts: 48
edited 2013-04-26 09:54 in Propeller 1
How best to transfer the data to the object (when there are many)?

How best to transfer the data to the object (when there are many).

The first eight long words are transmitted to the optimization.
Can I transfer more than eight words long.

For example:
ModBus.Start (ADR_SLAVE, REG_OFFSET, REG_QTY, @ REG_VAL, @ REG_MIN, @ REG_MAX, PIN_RX, PIN_TX, PIN_RT, MODE, BAUDRATE)

Comments

  • localrogerlocalroger Posts: 3,451
    edited 2013-04-26 05:36
    Use an array, and pass a pointer to it. First, enumerate your parameters in the object itself, and use pointers to an array to refer to the values:
    CON
      #0
      ADR_SLAVE
      REG_OFFSET
      REG_QTY
      REG_VAL
      REG_MAX
      PIN_RX
      PIN_TX
      PIN_RT
      MODE
      BAUDRATE
      NUM_PARMS
    
    PUB Start(parms)
    
      whatever := long[parms + ADR_SLAVE] 'etc.
    
    

    Then, in your parent object, use object#name to refer to the pointers:
    VAR
      long PARMS[ModBus#NUM_PARMS]
    
    PUB START
    
      'setup
      PARMS[ModBus#ADR_SLAVE] := whatever, etc.
    
      ModBus.Start(@PARMS)
    
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-04-26 06:17
    I do this by passing a pointer to a DAT block. For example, in my servo driver, one can specify the start-up values for each servo. As this requires eight longs I pass an address (one long) instead of eight individual values.

    Here's the method that uses the process:
    pub startx2(count, base, posmin, posmax, pntr) | ch
    
    '' Start servo cog
    '' -- count is number of servo outputs (up to eight)
    '' -- base is first pin of contiguous group
    '' -- posmin and posmax expressed in microseconds
    '' -- pntr is address of table/array (of longs) with position start values (us)
    
      us001   := clkfreq / 1_000_000                                ' ticks per microsecond
    
      count   := 1 #> count <# CH_COUNT                             ' keep legal
      
      smin    := posmin #> SVO_MIN                                  ' set range
      smax    := posmax <# SVO_MAX
    
      stop                                                          ' stop if already running
      repeat ch from 0 to (count-1)                                 ' set starting positions
        set(ch, long[pntr][ch], 0)                                  '  for each channel            
      
      cog := cognew(servo8(count, base), @stack) + 1                ' launch servo cog
    
      return cog
    


    One might use it like this:
    servos.startx(8, SVO_BASE, SVO_MIN, SVO_MAX, @ServoDefaults)
    


    The default positions are contained in the master program like this:
    dat
    
    ' Starting positions for servos when driver is loaded
    
    ServoDefaults           long    1_500, 1_500, 1_500, 1_500
                            long    1_500, 1_500, 1_500, 1_500
    
  • localrogerlocalroger Posts: 3,451
    edited 2013-04-26 09:54
    Another way to do it, if you don't need for the parent to be able to modify the passed values later (which it can't when you pass in the call anyway) is to declare the same block of vars in the parent and object and copy them with longmove:
    var
      long  ADR_SLAVE
      long  REG_OFFSET
      long  REG_QTY
      long  REG_VAL
      long  REG_MAX
      long  PIN_RX
      long  PIN_TX
      long  PIN_RT
      long  MODE
      long  BAUDRATE
      long  NUM_PARMS
    
    PUB Main
    
      ModBus.Start(@ADR_SLAVE)
    

    ...and in the object...
    var
      long  ADR_SLAVE
      long  REG_OFFSET
      long  REG_QTY
      long  REG_VAL
      long  REG_MAX
      long  PIN_RX
      long  PIN_TX
      long  PIN_RT
      long  MODE
      long  BAUDRATE
      long  NUM_PARMS
    
    PUB Start(parms)
    
      longmove(@ADR_SLAVE, parms, 11)
    

    The vars must all be of the same type and grouped the same in both the parent and object, but this allows you to refer to the parameters by variable name in both environments instead of using long[address+offset].
Sign In or Register to comment.