Shop OBEX P1 Docs P2 Docs Learn Events
Quick question on accessing an object's (public) variables (how?) — Parallax Forums

Quick question on accessing an object's (public) variables (how?)

bulkheadbulkhead Posts: 405
edited 2007-08-20 02:40 in Propeller 1
I have several long variables in an object that I want to access from the main program. How is this done? When I do "object.variable" it gives me "Expected subroutine name." I browsed through the PEK lab on objects and didn't see this.

Also, in my object I am trying to write a "start" method, but it gives me "expected a unique subroutine name." When I call the method something else (like startUp) it works fine. How can I get my method to be named "start" like all of the other objects in the library?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'm new to the propeller!

Comments

  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-19 03:48
    to access an object variable:

    object#variable

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-19 06:03
    Ken,
    That only works for constants. There is no way to directly access variables in another object.

    You have two ways ...

    1) You can create GET and PUT methods in the object with the variable and call those from elsewhere. Obviously, GET returns the value of the variable and PUT sets the variable from its parameter.

    2) You can create an "address of" function that returns the address of the variable, then refer to the variable using that address. If Z is set to the address, LONG[noparse][[/noparse] Z ] can be used to access the variable.
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-19 06:56
    3) WRT your start-issue. There is already a name START in your object; this can also be a variable deeply burried in e.g. the DAT section, or an assembly label!

    The rules are relatively simple: NO OVERLOADING! NO SHADOWING!

    As always, Mike's explanation wrt 1) and 2) is perfect. I should like to add, that GETters and SETters spend considerable time... In contrast to modern OOP design, where you find a tendency to offer them "for free" (i.e. compilers generate inline code). GETting an address is fine and in the spirit of SPIN as being a "structured assembly language" smile.gif

    This pointer needs not necessarily address an "array", but can point to any place in VAR or DAT space.
    Note (And this IS important! When you do not understand it, ask again!):
    - VAR variables are resorted by the compiler: LONGS first, follow WORDs, follow BYTES; unawareness of this can lead to deep frustration smile.gif
    - In contrast DAT variables are padded when appropriate!
    - VAR is object space; only DAT is "global"!

    Edit: Damned typos!

    Post Edited (deSilva) : 8/19/2007 7:13:12 AM GMT
  • RinksCustomsRinksCustoms Posts: 531
    edited 2007-08-19 18:31
    constants are set @ compile and cannot be changed? T/F
    inferring from the previous two posts by Mike and Desilva, one would need to create a method within the object of the variable you wish to change and then pass it the value(s) you want it to change. Correct?

    Object A (top object)
    OBJ
    ·pwm : "PWM_a_pin"
    VAR
    ·long num_a,num_x

    PUB transfer_to_another_obj

    ·num_a := <variable manipulation code>
    ·num_x := <variable manipulation code>
    ·pwm.set(num_a,num_x)

    separate object

    {{PWM_a_pin·'(referenced "child" object)}}

    VAR

    ·long·num_y,num_x,stack[noparse][[/noparse]4]

    PUB start

    ·<cognew code>

    PUB stop

    ·<cogstop code>

    PUB set(a,b)
    ·num_x := a
    ·num_y := b

    PRI maintain_pulse· | c

    ·repeat
    ·· num_x := <PWM manipulation code> * num_x
    ···if num_y > 0
    ····· <manipulation code>
    ·· repeat·c from 0 to 20
    ····· <toggle pin "num_y" with "num_x" pulse duration
    Would this program work? More specifically, after the "maintain_pulse" method has started in a new cog, would it or could it reference the original "PWM" object's variables·"num_x" & "num_y" for the recalculation that occurs within the outer repeat loop? Somehow i don't think it works like this, what am I missing?


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    E3 = Thought

    http://folding.stanford.edu/·- Donating some CPU/GPU downtime just might lead to a cure for cancer! My team stats.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-19 19:27
    Constants are constant as far as your program is concerned. They're computed by the compiler when it compiles your program.

    The variables in the first object are different from those in the second object (and inaccessible directly).

    You need a GET_X and GET_Y routine in the 2nd object to return num_x and num_y respectively to the caller.

    All of this is completely separate from the issue of having two different computers (cogs) running at the same time, one running the main program and whatever it calls, the other running maintain_pulse (I assume). Since both computers can be accessing num_x and num_y to get or set the values, you have to prevent one computer from getting at the values while the other computer is changing them. You can do this by using the LOCKxxx primitives (see the Propeller Manual for examples).

    Have a look at the examples, think about what I've said, then come back with further questions.
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-20 02:40
    Forgive me...I should have double checked before claiming to be an authority. All variables are private and can only be accessed through getters and setters. Encapsulation is a good thing. I guess I hadn't noticed in the documentation the use of LONG as a cast operator either. Must read in more detail next time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
Sign In or Register to comment.