Shop OBEX P1 Docs P2 Docs Learn Events
Stuck on syntax to read value in memory — Parallax Forums

Stuck on syntax to read value in memory

John KauffmanJohn Kauffman Posts: 653
edited 2010-11-16 19:29 in Propeller 1
I am trying to use the syntax #3 at the bottom of page 53 of the prop manual v1.1.
Program #1 creates and fills three variables
Program #2 reads them - but it fails
I compile and run Program #2
The line marked ### the code below is where the error occurs.
The standard lines of clock freq, etc. are there and when I comment out the "read variable" lines the code compiles, runs and performs as expected.
I know this is a trivial question, but I don't see how my code is different than the Prop Manual.

Thanks.

File = TestNumGen01.spin
{Creates and fills three variables with numbers}
VAR
byte Number1
byte Number2
byte Number3
PUB Start
{ Fills three variables}
Number1 := 11
Number2 := 12
Number3 := 13


File = TestMatchDisplay01.spin
OBJ
TestNumGen : "TestNumGen01"
VgaText : "VGA_Text"

PUB MyPub
{displays value in variable "Number1" put in the main memory by object "TestNumGen"}
vgatext.start(16)
TestNumGen.start
vgatext.str(string("Here are the numbers: ")) ' this line works fine
' ### problem is here:
‘ all three following lines result in compile error of "expect expression term"
‘ when next three lines are commented out the program runs fine.
vgatext.dec(Number1[0])
vgatext.dec(byte[Number1][0])
vgatext.dec(byte[@Number1][0])

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2010-11-15 16:29
    Your three byte variables only exist in the TestNumGen01 object, TestMatchDisplay01 doesn't know about them and therefore can't access them. Quick fix, return the address of Number1 from TestNumGen.start and go from there.

    Could you please use [noparse]
    
    [/noparse] for code fragments?                        
  • John KauffmanJohn Kauffman Posts: 653
    edited 2010-11-15 16:42
    kuroneko wrote: »
    Your three byte variables only exist in the TestNumGen01 object, TestMatchDisplay01 doesn't know about them and therefore can't access them.
    > Is there a syntax to store them so they are visible to other objects?
    kuroneko wrote: »
    Quick fix, return the address of Number1 from TestNumGen.start and go from there.
    > I'm not sure how to do that. Do you have an example? Is it like this: ?
    [noparse]
    ' (file that is reading)
    MyFunction(#Number1)
    
    [/noparse]
    kuroneko wrote: »
    Could you please use [noparse]
    
    [/noparse] for code fragments?
    I did not know about that convention, will do. Thanks.
  • kuronekokuroneko Posts: 3,623
    edited 2010-11-15 16:50
    > Is there a syntax to store them so they are visible to other objects?
    No. Everything under VAR is visible to all methods in the same file, local variables are confined to their methods. If you need cross-object access then you have to communicate addresses (e.g. here is a buffer address, please fill it for me) or return values by method. It really depends what you want to achieve.
    > I'm not sure how to do that. Do you have an example? Is it like this: ?
    [noparse]
    ' (file that is reading)
    MyFunction(#Number1)
    
    [/noparse]

    That's it ;) Which will then look like this (noparse tags omitted):
    ' (file that is reading)
    MyFunction(#Number1)
    
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-11-15 18:07
    You'd probably have better luck getting help if you simply attached your files so that others could run them.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2010-11-15 19:14
    In the end, good point, Jon. My thinking was that the answer would be so easy that it would be fastest for the experts to just see the few salient lines of code. But you are right, running the SPIN allows an expert to use the Prop Tool debugging tools.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-11-16 10:32
    ... and provide additional feedback that may be useful. I've had my customers send me programs complaining about a problem in section A that was in fact caused by section B. As I say to them, it's better to provide too much information when requesting help than too little.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2010-11-16 12:26
    OK, with good observations from The Mac, attached are the two files.

    Goal:
    OuterFile.spin, the top object, will use the value in a variable that has been filled by the .start method of the InnerFile.spin.

    Current problem:
    The OuterFile does not compile.

    Thanks.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-11-16 17:10
    You have several "broken" (okay, perhaps banged up) lines in each file.

    What is your goal. It appears like you want to use the .start() method of "inner" to set a variable in that object, but you'd also like to read that variable back. A parent object cannot see the variables in one of it's children. You can deal with this two ways

    1) Provide a method to return the variable from the child object -- for example (this would be in "inner"):
    pub shared
    
      return NumberinShareMemory
    


    2) Provide an address to shared values. This is useful when you have several variables. For example, what if you had an array of five bytes in the "inner" object. By returning the address of that array with a method you could do this:
    pub address
    
      return @NumberinsShareMemory
    

    Now your "outer" object can loop through that array like this:
    repeat idx from 0 to 4
      term.dec(byte[inner.address][idx])
      term.tx(CR)
    

    Perhaps it would be useful to state your end goal.


    Note that this uses the address. You have this syntax error in your "inner" object:
    PUB Start(Number1)
      { Fills a variable}
      byte[NumberinShareMemory] := 11
    

    This is the most straightforward:
    PUB Start(Number1)
      { Fills a variable}
      NumberinShareMemory := 11
    

    ...but you could also do this:
    PUB Start(Number1)
      { Fills a variable}
      byte[@NumberinShareMemory] := 11
    

    Note the inclusion of the @ (address of) sign.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2010-11-16 18:05
    Success with the first option.

    Inner:
    VAR
      byte NumberinShareMemory
    pub shared
      NumberinShareMemory := 11
      return NumberinShareMemory
    

    Outer:
    OBJ
        Inner : "TestMemoryInnerFile"
        VgaText :  "VGA_Text"   
    PUB MyPub
      vgatext.start(16)
      vgatext.dec(inner.shared)
    

    VGA shows "11"

    Jon: thanks for sticking with me to see this one through.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-11-16 19:29
    No problem. Just keep in mind that the .shared method you show above will always overwrite the value of your variable with 11. One assumes this is not what you want to do as you've just created a constant the hard way. My point is to be careful not to allow bad code -- that could become habit -- sneak into your tests.
Sign In or Register to comment.