Passing Variables by reference more than 1 level deep?
DavidM
Posts: 630
Hi,
I have a LONG variable defined in my VAR Block.
I have an OBJECT defined in my OBJ Block.
I can pass a reference to that variable from my main method to another method that is in another OBJECT ( another .spin file)
by using the @ symbol before the variable name.
When that method is called I can work with the referenced variable by using..
LONG [noparse][[/noparse] MyReferencedVariable ] : = 1 ( to write the variable)
MyVariable := LONG [noparse][[/noparse] MyReferencedVariable ] ( to read the variable)
so far this works..
But, then I start a new cog in the method which was called and it runs correctly for a short period, but when it finishes I want it to update my referenced variable, of which I can't seem to get it to work!
What I mean is, re-referencing the variable again ( more than 1 level) does not seem to work for me.
Q1) How do I get this COG to modify the referenced variable?
I hope I explained it??
Thanks
Dave M
I have a LONG variable defined in my VAR Block.
I have an OBJECT defined in my OBJ Block.
I can pass a reference to that variable from my main method to another method that is in another OBJECT ( another .spin file)
by using the @ symbol before the variable name.
When that method is called I can work with the referenced variable by using..
LONG [noparse][[/noparse] MyReferencedVariable ] : = 1 ( to write the variable)
MyVariable := LONG [noparse][[/noparse] MyReferencedVariable ] ( to read the variable)
so far this works..
But, then I start a new cog in the method which was called and it runs correctly for a short period, but when it finishes I want it to update my referenced variable, of which I can't seem to get it to work!
What I mean is, re-referencing the variable again ( more than 1 level) does not seem to work for me.
Q1) How do I get this COG to modify the referenced variable?
I hope I explained it??
Thanks
Dave M
Comments
-Phil
Here is my code..
there are two spin files
-Phil
Do you mean like this..
-Phil
I just tried your example..
The variable vBuzzerState variable is not recognized
So do you mean this..
regards
Dave M
Thanks,
-Phil
I am trying to store the store the cog number in vBuzzerState, This variable is kept in the MAIN METHOD.
I want external methods to manage it, in order to keep my MAIN METHOD clean.
This code runs the first time, but the cog won't update the variable ( i.e set it to zero 0 , meaning no cog is running for this routine.
The trick is to get any method including COGS and external objects to work with GLOBAL Variables
Dave M
I got it WORKING! As Phil tried to say, My variables were wrong, you cant use the same name for the variable, So I added Ptr ( for pointer) at the end of the same variable name
[noparse][[/noparse]code]
''
START OF MAIN CODE
CON
_CLKMODE =XTAL1 +PLL16X
_XINFREQ =5_000_000
VAR
BuzzerPin = 27
OBJ
Buzzer : "Buzzer.Spin"
VAR
LONG BuzzerState
PUB MAIN
Buzzer.Beep ( BuzzerPin, 1000, 1000, 4 , @BuzzerState)
WAITCNT(100_000_000+CNT) <- This counter was to short to test!!! My Bad!
Buzzer.Beep ( BuzzerPin, 1000, 1000, 3 , @BuzzerState)
END OF MAIN CODE.SPIN
START OF BUZZER.SPIN
'' Controls a Buzzer via a transistor.
'' Uses a COG so as not to halt system
VAR
LONG BuzzerStack[noparse][[/noparse]20]
OBJ
Timing : "Timing.spin"
PUB Beep ( BuzzerPin, OnDuration, OffDuration, NoOfBeeps ,BuzzerStatePtr) <---- I added the letters Ptr at the end of the variable
IF LONG[noparse][[/noparse]BuzzerStatePtr] == 0
LONG[noparse][[/noparse]BuzzerStatePtr] := COGNEW( BeepRun(BuzzerPin, OnDuration, OffDuration, NoOfBeeps , BuzzerStatePtr ) , @BuzzerStack )
PUB BeepRun ( BuzzerPin, OnDuration, OffDuration, NoOfBeeps ,BuzzerStatePtr)
DIRA[noparse][[/noparse]BuzzerPin] := 1
REPEAT NoOfBeeps
OUTA[noparse][[/noparse]BuzzerPin] := 1
Timing.pause1ms(OnDuration)
OUTA[noparse][[/noparse]vBuzzerPin] := 0
Timing.pause1ms(OffDuration)
LONG[noparse][[/noparse]BuzzerStatePtr] :=0 <-- This resets the "COG NUMBER variable back in the MAIN METHOD/MEMORY to 0
regards
Dave M