PDA

View Full Version : Stuck on syntax to read value in memory



John Kauffman
11-15-2010, 11:14 PM
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])

kuroneko
11-15-2010, 11:29 PM
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
for code fragments?

John Kauffman
11-15-2010, 11:42 PM
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?


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: ?


' (file that is reading)
MyFunction(#Number1)



Could you please use
for code fragments?
I did not know about that convention, will do. Thanks.

kuroneko
11-15-2010, 11:50 PM
> 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: ?


' (file that is reading)
MyFunction(#Number1)


That's it ;) Which will then look like this (noparse tags omitted):



' (file that is reading)
MyFunction(#Number1)

JonnyMac
11-16-2010, 01:07 AM
You'd probably have better luck getting help if you simply attached your files so that others could run them.

John Kauffman
11-16-2010, 02:14 AM
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.

JonnyMac
11-16-2010, 05:32 PM
... 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 Kauffman
11-16-2010, 07:26 PM
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.

JonnyMac
11-17-2010, 12:10 AM
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 Kauffman
11-17-2010, 01:05 AM
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.

JonnyMac
11-17-2010, 02:29 AM
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.