Help with Var sharing between objects
lmclaren
Posts: 104
Hi,
I am struggling a bit trying to get my head around sharing variables between objects, this is what I have:
Roaster_Main.spin
│
├──PhaseDimmer.spin (runs in own cog)
│
├──Simple_Numbers.spin
│
├──SPI_Devices.spin
│ │
│ └──SPI_Spin.spin (runs in own cog)
│
├──VGA64_TMPEngine.spin
│
├──Artisan.spin (runs in own cog)
│ │
│ └──pcFullDuplexSerial4FC.spin (2nd port, not started, using the one started in Roaster_Main)
│
└──pcFullDuplexSerial4FC.spin (1st port, runs in own cog, started from Roaster_Main
How I would like it to work is:
Data collected in SPI_Devices,
Data used by, Roaster_Main, Artisan
I have setup an OBJ in Artisan for SPI_Devices and called this method:
VAR
long MAX1_C
PUB Get_MAX1
return MAX1_C
If I call the method from Roaster_Main it works and returns the value, If I call it from Artisan, it does not.
I have read the doc a few times and I am missing something.
Is there a way to refer from Artisan to a var in Roaster_Main?
I will have a number of objects in seperate cogs collecting infromation and this information will be sent out from other objects in other cogs, eg some will go out as serial, some will go out to the video display.
btw, Am I using the pcFullDuplexSerial4FC incorrectly by starting it in Roaster_Main and then using the second port in Artisan?
many thanks
Lee
I am struggling a bit trying to get my head around sharing variables between objects, this is what I have:
Roaster_Main.spin
│
├──PhaseDimmer.spin (runs in own cog)
│
├──Simple_Numbers.spin
│
├──SPI_Devices.spin
│ │
│ └──SPI_Spin.spin (runs in own cog)
│
├──VGA64_TMPEngine.spin
│
├──Artisan.spin (runs in own cog)
│ │
│ └──pcFullDuplexSerial4FC.spin (2nd port, not started, using the one started in Roaster_Main)
│
└──pcFullDuplexSerial4FC.spin (1st port, runs in own cog, started from Roaster_Main
How I would like it to work is:
Data collected in SPI_Devices,
Data used by, Roaster_Main, Artisan
I have setup an OBJ in Artisan for SPI_Devices and called this method:
VAR
long MAX1_C
PUB Get_MAX1
return MAX1_C
If I call the method from Roaster_Main it works and returns the value, If I call it from Artisan, it does not.
I have read the doc a few times and I am missing something.
Is there a way to refer from Artisan to a var in Roaster_Main?
I will have a number of objects in seperate cogs collecting infromation and this information will be sent out from other objects in other cogs, eg some will go out as serial, some will go out to the video display.
btw, Am I using the pcFullDuplexSerial4FC incorrectly by starting it in Roaster_Main and then using the second port in Artisan?
many thanks
Lee
Comments
If I want to share child to child, is that where I have to use a DAT?
Sometimes it's easier to keep a shared storage area at the parent level and just tell the child objects where it is. Otherwise you'd need to ask child 1 for its address, tell child 2 and vice versa.
That's what I've concluded having looked at the same situation earlier today for a different reason. Being able to share the FullDuplexSerial object "as-is" would be great. I remember someone creating a "wrapper" for FullDuplexSerial and claiming that fixed the problem. Well, it doesn't because even though the object was included only once, the including object is included more than once so the VAR addresses were different. Bummer.
So if i want to get away with not using a DAT section for variables, the only way to share the object is by "tell the child objects where it is" as you said. I get to add another method to FullDuplexSerial for that to work ... :sick:
TopObject
Child1 8 long vars
Child2 8 long vars
Child3 8 long vars
To start Child1, I do the following:
from TopObject,
Child1.Start(@Child1Vars_in_TopObject, @Child2Var_in_TopObject, @Child3Vars_in_TopObject)
Child2.Start(@Child1Vars_in_TopObject, @Child2Var_in_TopObject, @Child3Vars_in_TopObject)
Child3.Start(@Child1Vars_in_TopObject, @Child2Var_in_TopObject, @Child3Vars_in_TopObject)
With all the VARs in the TopObject only.
many thanks
I've spent a lot of time hacking pcFullDuplexSerial4FC, but I'm not sure it the above technique would work or not.
Can I have a shared object that has been told the address of all the vars and then reuse the object in each child.
as an example,
OBJ
VARSHARE "VARSHAREOBJ"
From Top Object
VarShare.Init(@location_of_Vars_in_TopObject)
from ChildObject
OBJ
VARSHARE "VARSHAREOBJ"
CON
FRED,CAT,DOG
VarShare.GetFred
Return @location + Fred
SHARE.spin
CONS
Var1
Var2
Var3
Var4
Var5
PUB SET(VarOffset,Value)
PUB GET(VarOffset) Value
DAT
long[5]
In each child,
OBJ
S : "Share"
S.GET(S#VAR1)
or
S.SET(S#VAR1)
Am I on the right track?
many thanks
Note that variables get re-ordered according to type, i.e. longs come first followed by word and byte variables. The order within each group stays the same.
I have a "header" object I use to keep all my constants in. I add the Header object to the top and all child objects. I can then use Header#CONSTANT_NAME to access the constants. You could keep your "FRED, CAT, DOG" constants in a header so all your objects use the same offset from @location.
You do need one PUB method. Just add:
This isn't my idea. I learned from someone here on the forum.
Duane
Edit: I was typing while you added another post. Yes you're on the right track.
best regards
Lee
You can pass a pointer to the structure to methods that are in the same object or in different objects. The offsets can be defined in separate object like kuroneko suggests. If all data structure elements are the same size you can just define byte, word or long offsets and access them as an array, such as WORD[cat].
best regards
Lee