Spin Question - Passing ByRef
BenClark
Posts: 20
I need to pass some variables to a Spin method and let that method alter the variables.
I think I should be calling the method as shown below. (Push
It doesn't seem to work. Depending on the button, I want to pass in an array
of byte vars (Buttons1Stack[10]
When the main method starts, I fill the byte array with zeros
When the code gets into the Push method, it appears that the byte array has values in it. Instead of all zeros
Help! code is attached
VAR
long IO, bogus, bState, Smile, BTN1State, BTN2State, BTN3State, StackOverFlow
long Buttons1StackPushPos, Buttons2StackPushPos, Buttons3StackPushPos
long Buttons1StackPullPos, Buttons2StackPullPos, Buttons3StackPullPos
byte Buttons1Stack[BTNSTACKTOP + 1]
byte Buttons2Stack[BTNSTACKTOP + 1]
byte Buttons3Stack[BTNSTACKTOP + 1]
PUB WatchButtons | value
repeat
IO := INA ' Copy the INA (Current State of ALL Pins)
bState := ina[BTN1]
if bState == 0
if BTN1State == 0 ' If it changed from 0 to 1
Push(@Buttons1Stack[0], @Buttons1StackPushPos, BTN1Pushed)
BTN1State := 1
else
if BTN1State == 1 ' If it changed from 1 to 0
Push(@Buttons1Stack[0], @Buttons1StackPushPos, BTN1Released)
BTN1State := 0
I think I should be calling the method as shown below. (Push
It doesn't seem to work. Depending on the button, I want to pass in an array
of byte vars (Buttons1Stack[10]
When the main method starts, I fill the byte array with zeros
When the code gets into the Push method, it appears that the byte array has values in it. Instead of all zeros
Help! code is attached
VAR
long IO, bogus, bState, Smile, BTN1State, BTN2State, BTN3State, StackOverFlow
long Buttons1StackPushPos, Buttons2StackPushPos, Buttons3StackPushPos
long Buttons1StackPullPos, Buttons2StackPullPos, Buttons3StackPullPos
byte Buttons1Stack[BTNSTACKTOP + 1]
byte Buttons2Stack[BTNSTACKTOP + 1]
byte Buttons3Stack[BTNSTACKTOP + 1]
PUB WatchButtons | value
repeat
IO := INA ' Copy the INA (Current State of ALL Pins)
bState := ina[BTN1]
if bState == 0
if BTN1State == 0 ' If it changed from 0 to 1
Push(@Buttons1Stack[0], @Buttons1StackPushPos, BTN1Pushed)
BTN1State := 1
else
if BTN1State == 1 ' If it changed from 1 to 0
Push(@Buttons1Stack[0], @Buttons1StackPushPos, BTN1Released)
BTN1State := 0
Comments
The first item after the LONG is the address of the memory location. The second (optional) item is an array index which assumes the address refers to an array.
You gave this line as an example.
long[stackAddress][long[theIndex]++] := theValue
What if the parm is a byte array?
byte[stackAddress][byte[theIndex]++] := theValue
Also, what is the ++ for?
Thanks
LONG var1,var2,var3,var4
and access them in the Push by one reference being sent only as a pointer like this: For a byte, you don't need to add the multiples of 4 after the pointer. If you insert or delete anything in the original var declare line, then obviously the results will not be accurate any longer when trying to access via this method.
It might be a good idea to create some test code to try out the concepts of passing pointers, verify they were rec'd etc, before trying to run the actual more complex program.
++ is an increment operator similar to the same operator in C or other programming languages. Look it up in the Propeller Manual or the Propeller Tool's help files.