Headbanging: r/w values between Main and Object in another cog
Erlend
Posts: 612
I thought I hade sorted out using @pointers by now, but obviously not. Here's a sample code I wrote for you.
-and the Main to test it
Testing with debug to display the return-write gives garbage. What am I doing wrong?
{Receive DAT value from Main, and write data data to VAR value in Main} CON _clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz _xinfreq = 5_000_000 VAR long LocalInputDataPointer long LocalOutputDataPointer long cog, stack[32] PUB start(InputDataPointer, OutputDataPointer) cog:= cognew(r_w(LocalInputDataPointer, LocalOutputDataPointer), @stack[0]) LocalInputDataPointer := InputDataPointer LocalOutputDataPointer := OutputDataPointer PUB r_w(indataPointer, outdataPointer) IF long[LocalInputDataPointer] [4] == 6 long[LocalOutputDataPointer] [4] := 3 ELSE long[LocalOutputDataPointer] [4] := 1
-and the Main to test it
CON _clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz _xinfreq = 5_000_000 VAR long OutputData[9] OBJ rw : "TESTr_w" PUB Main | i rw.start(@InputData, @OutputData) DAT InputData long 3, 1, 4, 2, 9, 7, 5, 8, 0, 6
Testing with debug to display the return-write gives garbage. What am I doing wrong?
Comments
Top level object
Child object
This example works and it does demo passing values by reference. It's ok for learning but not a good design pattern.
You find the "bug" without harming by yourself? First you call r_w with parameters but then you don't use them ;o)
In general I don't understand what the LocalXXXXDataPointer variables are good for at all! ... well ... at least when the program keeps being as simple as that. Because having the pointers in the r_w-stack is local enough. If your program get's more complex and you need access from more than one function it might make sense to store the values in a VAR, but then you don't have to pass those LocalXXXX addresses to the functions, because all functions in an object have access to the VARs defined in the object.
One way
Another way
This is an example of encapsulation...
Top Level
Child Object