View Full Version : Save references in array

Michel L
11-14-2011, 08:37 PM
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:

long monitorThis

monitor : "Monitor"

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

Monitor program:

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?

Mike G
11-15-2011, 12:27 AM
Take a look at AN003: Implementing Abstract Data Structures with Spin Objects (http://www.parallaxsemiconductor.com/an003)

But, yes you can create an array of pointers.

11-15-2011, 03:52 AM
pri setValue(valIndex, value)
@monitors[valIndex] := value

This should read:

pri setValue(valIndex, value)
long[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)
byte[monitors[valIndex]] := value

pri setWordValue(valIndex, value)
word[monitors[valIndex]] := value

Michel L
11-15-2011, 12:42 PM
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.