Going Crazy (Spin)
BenClark
Posts: 20
Please take a look at the code below.
I run this using ViewPort.
I have an array of longs that I want to use as a stack.
I have a long var, Buttons1StackPushPos, that I use as a position indicator into the stack.
I call the Push1 method and it checks the stack to see if the entry just after the position indicator (Buttons1StackPushPos) is empty. If it is empty, it bumps the Buttons1StackPushPos variable by one and places the past in value into the array at
Button1Stack[Buttons1StackPushPos]
It then returns.
The main method calls the push again.
When it gets into the push method, the Buttons1StackPushPos is ALWAYS set back
to zero.
What am I missing here?
Driving this newbie mad!
Thanks in advance for your help.
I run this using ViewPort.
I have an array of longs that I want to use as a stack.
I have a long var, Buttons1StackPushPos, that I use as a position indicator into the stack.
I call the Push1 method and it checks the stack to see if the entry just after the position indicator (Buttons1StackPushPos) is empty. If it is empty, it bumps the Buttons1StackPushPos variable by one and places the past in value into the array at
Button1Stack[Buttons1StackPushPos]
It then returns.
The main method calls the push again.
When it gets into the push method, the Buttons1StackPushPos is ALWAYS set back
to zero.
What am I missing here?
Driving this newbie mad!
Thanks in advance for your help.
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 'Pin definitions for my board. CHANGE TO MATCH YOUR BOARD! BTN1 = 16 BTN2 = 17 BTN3 = 18 BTN1Pushed = 129 ' 10000001 BTN1Released = 1 ' 00000001 BTNSTACKTOP = 9 OBJ vp : "Conduit" utils : "Utils" 'button: "Button" 'vid: "TV_Text" VAR long IO, bogus, bState, bogus2, BTN1State, BTN2State, BTN3State, StackOverFlow, tIndex, Buttons1StackPushPos long Counter long cntr long Buttons1Stack[BTNSTACKTOP + 1] PUB Main vp.config(string("start:code")) vp.config(string("var:bogus,bState,bogus2,BTN1State,BTN2State,BTN3State,StackOverFlow,tIndex,Buttons1StackPushPos")) vp.config(string("start:dll")) vp.share(@bogus,@Buttons1StackPushPos) DIRA[BTN1]~ ' Set PIN 16 to Output DIRA[BTN2]~ ' Set PIN 17 to Output DIRA[BTN3]~ ' Set PIN 18 to Output bogus2 := 0 StackOverFlow := 0 BTN1State := 0 ' Button Push Stack Position Buttons1StackPushPos := 0 ' Button Pull Stack Position Buttons1StackPullPos := 0 longfill(@Buttons1Stack, 0, BTNSTACKTOP + 1) repeat 1000000 Counter ++ IO := INA ' Copy the INA (Current State of ALL Pins) waitcnt(10_000 + cnt) Push1(BTN1Pushed) Push1(BTN1Pushed) Push1(BTN1Pushed) if bogus2 == 1 bogus2 := 0 else bogus2 := 1 PUB Push1(value) : success | curval, index ' This method should only be called by the cog that is watching the buttons (PUB WatchButtons) ' Are we at the top? In other words, have we reached the end of the stack if Buttons1StackPushPos >= BTNSTACKTOP ' Go get the entry in the first slot of the stack curval := Buttons1Stack[0] ' if the first slot is not empty, we ran out of stack space if curval <> 0 StackOverFlow := 1 return -1 else ' the first slot is empty so we can use it now Buttons1Stack[0] := value ' Move our position indicator to the first slot Buttons1StackPushPos := 0 return 0 else ' see if the next slot is available index := Buttons1StackPushPos tIndex := index curval := Buttons1Stack[Buttons1StackPushPos + 1] tIndex := ++index if curval <> 0 bogus := curval StackOverFlow := 1 return -1 else ' move to the next slot Buttons1StackPushPos := ++Buttons1StackPushPos bogus := Buttons1StackPushPos ' Put the value into the next slot Buttons1Stack[Buttons1StackPushPos] := value return 0 return 0
Comments
before the code block starts, type "code" in brackets. [ ] and at the end type "/code" in brackets.
Thanks
That is a lot of code to sort out at a glance. I would suggest that you take the specific issue you are having, make some test code that is very simple and work with little sections first before trying to get the whole thing to work out. It may in face be more than one problem, so when you isolate specific concepts until you have it down good, it will help both to solve the problem and to learn the SPIN concepts.
In fact, I have tracked it down to one specific spot.
The Push1 method.
The very first if statement. if Buttons1StackPushPos >= BTNSTACKTOP
If you put a stop on the if statement, the value of Buttons1StackPushPos on the
second call will be 1. Then if you step one step, which goes directly to the else, the
value of Buttons1StackPushPos changes during that one step back to zero.
Nothing in the IF section runs because the condition is false so it goes straight to the else.
Then, poof, the value changes with no other code running.
I have no clue what to do or where to go next.
Anyone???
I changed the if statement from
to
Now the it works. I doubled and triple check the Prop manual.
>= is 'Greater Than' Page 171
Anyone seen this before or have an explanation?
Buttons1StackPushPos >= BTNSTACKTOP
actually means
Buttons1StackPushPos := Buttons1StackPushPos > BTNSTACKTOP
In other words, Buttons1StackPushPos is compared to BTNSTACKTOP, then the result (TRUE or FALSE, depending which is greater) is stored in Button1StackPushPos. That's probably messing up your program.
If you just want to test "greater than", use plain ">".
If you want "greater or equal", use "=>" (which looks a little funny at first).
You wanted "=>"
Kye-
That's a great pneumonic for remembering which way round those boolean expression should be written. Thanks. I know I've been bitten by this particular one before.
After testing a new concept out and finding out first hand what it does, then you can start compiling your own set of notes for future use.
Except for ==.
What am I, chopped liver?!
/kidding
//just trying out the multi-quote feature