View Full Version : How do I access another object's constants

william chan
07-04-2009, 06:56 PM
Usually I declare all the pins in the Main object.
Lets say

redledpin = 12

After that if another object needs to know the pin number of the red led, what's the best way to do it?

www.fd.com.my (http://www.fd.com.my)
www.mercedes.com.my (http://www.mercedes.com.my)

07-04-2009, 07:47 PM
I think it's


Toys are microcontroled.
Robots are microcontroled.
I am microcontroled.

If it's not Parallax then don't even bother. :-)

Mini-Din/PS2 connectors are for sale! 5 for $1! PM me if you wish to make an order.
Cheapˇshipping unless specified!ˇˇˇˇˇˇˇˇˇˇ150 left!!ˇˇ

Mike Green
07-04-2009, 09:39 PM
You have to pass it to the lower level object using a subroutine call. In order to reference a constant definition like microcontrolled suggested, the object with the constant definition has to be lower down and has to be defined as an object where you're going to reference it. For example:

Main object:
OBJ myObj : " ... "

myObj#redLedPin := 1

Lower object (myObj):

CON redLedPin = 5

If you want to go the other way:

Main object:

CON redLedPin = 5
OBJ myObj : " ... "


Lower object:

VAR byte saveLedPin

PUB start( pinNumber )
saveLedPin := pinNumber

07-05-2009, 03:31 AM
How do you avoid a circular reference error if you have the main and lower level objects pointing to each other?

Mike Green
07-05-2009, 03:41 AM
You can't have a circular reference because the lower level object cannot have a reference to a higher level object, so the lower level object can't point to the upper level object.

You need to define your constants in a lower level object, then they can be referred to in some higher level object by using the obj#constant notation. There are ways to define an object without any instance variables, so you can declare multiple instances where needed and no unnecessary code or data is allocated. One example is DongleBasic from the Object Exchange. There are all sorts of important constants declared in BB_definitions.spin that are referenced in other objects and in the main object.

william chan
07-05-2009, 06:46 AM
Isn't it a bad idea to have all the pin definitions scattered all over many lower object files?
How would you ensure to that don't inadvertently reuse a already used pin?
Isn't it better to have all pins defined in the top object file?

www.fd.com.my (http://www.fd.com.my)
www.mercedes.com.my (http://www.mercedes.com.my)

Mike Green
07-05-2009, 12:47 PM
In the case of DongleBasic (and other FemtoBasic derivatives), the central definition file is BB_definitions.spin. This has the various pin definitions, work area definitions, other global definitions. It's included in all sorts of "lower" object files. Have a look at how things are structured.

You could have the I/O pins all defined in the top object file as long as they're passed as parameters to the various start methods of the "lower" object files. I've done that in other programs. Both schemes are useful.

07-05-2009, 01:07 PM
For things that do not change ... certain pin definitions, etc..., make your life simple and just use a common global object with constants and an empty pub method in it. You can "include" that anywhere and refer to it in the simplest possible way ... "object#constant" as microcontrolled mentioned. PASM has limits of course, but the syntax can still be applied in a long.


Propalyzer: Propeller PC Logic Analyzer (http://www.brouhaha.com/~sdenson/Propalyzer)
http://forums.parallax.com/showthread.php?p=788230 (http://forums.parallax.com/showthread.php?p=788230)