Shop OBEX P1 Docs P2 Docs Learn Events
Spin Question - Passing ByRef — Parallax Forums

Spin Question - Passing ByRef

BenClarkBenClark Posts: 20
edited 2010-09-08 10:20 in Propeller 1
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-08 09:56
    The problem is in your push method and the notation you're using to refer to the stack. Spin passes all parameters by value, so, when you want to pass something by reference, you have to pass the value of a reference as you're doing with @stack. On the other side of the call, you have to de-reference the address using BYTE, WORD or LONG depending on the size of the variable.
    push(@stack,valueToPush,@stackIndex)
    
    pri push( stackAddress, theValue, theIndex)
    long[stackAddress][long[theIndex]++] := theValue
    

    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.
  • BenClarkBenClark Posts: 20
    edited 2010-09-08 10:02
    Thanks for the quick reply.
    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
  • T ChapT Chap Posts: 4,223
    edited 2010-09-08 10:11
    Hi Ben, one thing also along these lines, is that you can declare a line of variables like this:

    LONG var1,var2,var3,var4

    and access them in the Push by one reference being sent only as a pointer like this:
    PUB Watch
           push(@var1)
    
    PUB push(apointer)
    
      somevariable1 := long[apointer]    'var1
      somevariable2 := long[apointer+4]  'var2
      somevariable3 := long[apointer+8]  'var3
      somevariable4 := long[apointer+12] 'var4
    
    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-08 10:20
    If the parameter is a byte or byte array, use BYTE. If it's a word or word array, use WORD.

    ++ 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.
Sign In or Register to comment.