Shop OBEX P1 Docs P2 Docs Learn Events
Updating Parent Array Values from Child Object? — Parallax Forums

Updating Parent Array Values from Child Object?

NJPNJP Posts: 3
edited 2015-10-06 14:46 in Propeller 1
I've been working on a simple PWM reader in spin for my project and it works great as methods inside a single object file but when I use it as a separate object file I cannot figure out how to update my main code data array from the child object. I understand that I will have to pass the address but I do not know how I can use that address to actually change the value of the data stored in the array. I know that I will have to be careful with the read / write timing here as separate cogs will be using this same memory. Here is the framework for what I've got:

Main Code
OBJ
  pst    : "Parallax Serial Terminal Plus"
  PWMR   : "My PWM Reader 0"
CON
  _CLKMODE=XTAL1+ PLL16X
  _XINFREQ = 5_000_000
 
VAR
  long stack[100]
  long PWMdata[10]

PUB main
  cognew(PWMR.start(0,1,PWMdata),@stack)                'start a new cog with the reader on pin 0, 1 pin only
  repeat
                                   'print outputs to terminal

Child Object "My PWM Reader 0"
CON
  _CLKMODE=XTAL1+ PLL16X
  _XINFREQ = 5_000_000

VAR
  long startHi
  long stopHi
  byte pin0
  byte pinNum
  byte pin
  long PWMdat[10]
  long outputArrayAddress

PUB start (startPin, numberOfPins,dataAddress)
  pin0:=startPin
  pinNum:=numberOfPins
  outputArrayAddress:=dataAddress

  repeat pin from pin0 to (pin0+pinNum-1)
    DIRA[pin]~                                          'sets all pins to inputs                                        
  repeat
    repeat pin from pin0 to (pin0+pinNum-1)             'cycle through and read PWM on each pin          
      repeat while ina[pin]==1
      repeat while ina[pin]==0
      starthi:=cnt
      repeat while ina[pin]==1
      stophi:=cnt
      PWMdat[pin]:=stophi-starthi
    repeat pin from pin0 to (pin0+pinNum-1)             'cycle through and update PWM data array values
      outputArrayAddress[pin]:=PWMdat[pin]              'need help with this

I know it's a simple question but I just cannot find the answer...

Comments

  • T ChapT Chap Posts: 4,223
    edited 2015-10-05 23:45
    In a child, you can take the pointer that it was sent with the long in Main, and do this:

    long[childdata] := 1

    The main method can then read the data at any time.

    Or, as an example in the child obj do long[outputArrayAddress] := 1

    then in the main method read the data in PWMdata.
  • ElectrodudeElectrodude Posts: 1,658
    edited 2015-10-05 23:51
    You have your BBCode begin and end tags backwards. Also, try code tags instead of noparse tags - it will maintain indentation.

    You can't cognew a child object's method. Put the cognew in a new method in the child object.

    You need to do long[baseptr][offset] to index arrays by pointer.

    main.spin:
    OBJ
    
      child: "childobject"
    
    VAR
    
      long array[123]
    
    PUB main
    
      child.init(@array, 0, 122)
    
      ' do stuff
    

    childobject.spin:
    VAR
    
      long stack[64]
      word arrayptr
    
    PUB init(_arrayptr, a, b)
    
      arrayptr := _arrayptr
    
      cognew(loop(a, b), @stack)
    
    PRI loop(a, b, c) | i
    
      repeat
        repeat i from a to b
          long[arrayptr][i] := read_val(i)
    

    Also, _clkmode and _xinfreq in child objects are ignored.
  • Thanks, folks
    I got it figured out. It was mostly the cognew() placement causing something I had already tried to fail.
Sign In or Register to comment.