Shop OBEX P1 Docs P2 Docs Learn Events
How do I access another object's constants — Parallax Forums

How do I access another object's constants

william chanwilliam chan Posts: 1,326
edited 2009-07-05 06:07 in Propeller 1
Usually I declare all the pins in the Main object.
Lets say

CON
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
www.mercedes.com.my

Comments

  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-07-04 12:47
    I think it's

    #redledpin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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 GreenMike Green Posts: 23,101
    edited 2009-07-04 14:39
    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 : " ... "
    
    myObj.start(redLedPin)
    
    Lower object:
    
    VAR byte saveLedPin
    
    PUB start( pinNumber )
       saveLedPin := pinNumber
    
    
  • KokovecKokovec Posts: 14
    edited 2009-07-04 20:31
    How do you avoid a circular reference error if you have the main and lower level objects pointing to each other?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-07-04 20:41
    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 chanwilliam chan Posts: 1,326
    edited 2009-07-04 23:46
    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
    www.mercedes.com.my
  • Mike GreenMike Green Posts: 23,101
    edited 2009-07-05 05:47
    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.
  • jazzedjazzed Posts: 11,803
    edited 2009-07-05 06:07
    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
Sign In or Register to comment.