Variables across objects, again
Michael Petry
Posts: 11
Hello.· I have spent 3 days reading threads related to "global variables", "hub ram", and "passing data between objects".· I have about decided that it's impossible to do what I'm attempting, but I want to try one last time.
I have a collaborative project with my son:·· I want to build an object, which my son will use in his projects.· My object will run in it's own cog, and will constantly send data back to my son's cog/object.· His object will monitor the data stream for changes, and respond accordingly.··· In vain, I have built a test program.·
This almost works.·· The first cog·does see a change at memory address $7F00, but only only the last change made (:=23).· It does not see anything the 2nd cog does, until the 2nd cog stops.· Any suggestions?
I have a collaborative project with my son:·· I want to build an object, which my son will use in his projects.· My object will run in it's own cog, and will constantly send data back to my son's cog/object.· His object will monitor the data stream for changes, and respond accordingly.··· In vain, I have built a test program.·
{{VarTest.spin}} {{This is a test to determine if another cog, running an external object, can pass a continuous data stream back to the main cog/object. The shared memory location is $7f00}} CON _CLKMODE = XTAL1 + PLL4X 'Set to ext low-speed crystal, 4x PLL _XINFREQ = 5_000_000 'Frequency on XIN pin is 5 MHz VAR word pin long SqStack[noparse][[/noparse]100] OBJ MyCog : "ChangePin" PUB Main 'This is my son's object. it looks for a change at $7F00 cognew(MyCog.ChangePin, @SqStack) 'Start my cog, running ChangePin (my object) dira[noparse][[/noparse]16]~~ dira[noparse][[/noparse]17]~~ dira[noparse][[/noparse]18]~~ dira[noparse][[/noparse]19]~~ dira[noparse][[/noparse]20]~~ dira[noparse][[/noparse]21]~~ dira[noparse][[/noparse]22]~~ dira[noparse][[/noparse]23]~~ repeat pin := word[noparse][[/noparse]$7f00] 'Look for changes in memory address $7f00 outa[noparse][[/noparse]pin]~~ 'and turn on the corresponding pin
{{ChangePin.spin}} PUB ChangePin 'Modify the data in memory address $7F00, which my son's cog will watch word[noparse][[/noparse]$7f00] := 16 waitcnt(20_000_000 + cnt) word[noparse][[/noparse]$7f00] := 20 waitcnt(20_000_000 + cnt) word[noparse][[/noparse]$7f00] := 23
This almost works.·· The first cog·does see a change at memory address $7F00, but only only the last change made (:=23).· It does not see anything the 2nd cog does, until the 2nd cog stops.· Any suggestions?
Comments
Cog 0 : Demo code
Cog 1 : LED driver
Cog 2 : fader method that uses address of the LED object's brightness variable
Perhaps hard-wiring an hub address is not a good idea; let the program locat the variable and then reveal that location with @.
There is your problem. You can't start a cog from a method in another object.
Do something like this.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"VOOM"?!? Mate, this bird wouldn't "voom" if you put four million volts through it! 'E's bleedin' demised!
Post Edited (Michael Petry) : 5/22/2009 3:39:44 AM GMT
below is a demo how a variable is changed by different cogs
if you want to exchange data between different objects which means exchanging data between code that is saved in different *.SPIN-files
simply define "get" and "set"-methods
let's assume the code below is in a *.SPIN-file named MyObject.SPIN
inside your main.spin you have some code
you only wrote that the second objects monitors a datastream and respond on changes
As soon as you describe what you want to do as the whole thing in your project I'm sure the forum-members will
find several solutions
the other way is to pass the RAM-adress of the variable to the other object
content of file ObjectOfMicheals_Son.SPIN
content of file ObjectOfMichael.spin
see also attached archive
Post Edited (Michael Petry) : 5/22/2009 3:34:03 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'm either going mad or both...
word[noparse][[/noparse] OtherCog#MY_ADR ]
But I think your first idea was better - or what stefan38 suggests in the last example - passing the address to use to your object. Because this way the main-program has control over all memory used by direct access. Say you have different objects that work this way or your main want's to use such an object twice. Then you have potentially conflicts in case both objects define the same address for such a communication variable.
Post Edited (MagIO2) : 4/18/2010 7:51:32 PM GMT