Shop OBEX P1 Docs P2 Docs Learn Events
Save references in array — Parallax Forums

Save references in array

Michel LMichel L Posts: 141
edited 2011-11-15 05:42 in Propeller 1
I'm writing a spin-object that will work together with a piece of software written in Processing to monitor some values as well as set some variables.

To set variables I would like to save the address of certain variables that can be managed into an array so that the object can change them later.

Something like this:


Main program:
var 
  long monitorThis

obj
  monitor : "Monitor"

pub start
  monitor.start
  monitor.registerVar(0, @monitorThis)
  ...
  ' use monitorThis

Monitor program:
var
  long monitors[10]

pub start
  ...

pub registerVar(valIndex, reference)
  monitors[valIndex] := reference

pri setValue(valIndex, value)
  @monitors[valIndex] := value

...


Any ideas if this is possible and/or could work? Any suggestions for improvements?

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-11-14 17:27
    Take a look at AN003: Implementing Abstract Data Structures with Spin Objects

    But, yes you can create an array of pointers.
  • kuronekokuroneko Posts: 3,623
    edited 2011-11-14 20:52
    pri setValue(valIndex, value)
      [COLOR="#FF0000"]@monitors[valIndex] := value[/COLOR]
    
    This should read:
    pri setValue(valIndex, value)
      [COLOR="#FFA500"]long[/COLOR][monitors[valIndex]] := value
    
    assuming the addresses you stored all refer to long quantities. If you need mixed sizes you should either store the type/size as well (and use them as a filter) or call special functions for addresses where you know that it's only e.g. a byte.
    pri setByteValue(valIndex, value)
      [COLOR="#FFA500"]byte[/COLOR][monitors[valIndex]] := value
    
    pri setWordValue(valIndex, value)
      [COLOR="#FFA500"]word[/COLOR][monitors[valIndex]] := value
    
  • Michel LMichel L Posts: 141
    edited 2011-11-15 05:42
    Thanks Mike and Kuroneko. I remembered seeing something like these documents but I forgot where. I also remember seeing something as passing pointers to an object in a cog vs. using getters. But I could find that one back either.

    And thanks for the correction. I didn't know I had to use the long[] command for this.

    I will make sure to post and share my code/application on the forum and obex. As a thanks for your support. I hope a lot of other people will be able to use my application. It is nowhere as extensive as ViewPort but I hope some monitoring and graphing of variables will be useful.

    Michel
Sign In or Register to comment.