Shop OBEX P1 Docs P2 Docs Learn Events
Going Crazy (Spin) — Parallax Forums

Going Crazy (Spin)

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

  • T ChapT Chap Posts: 4,223
    edited 2010-09-08 11:51
    HI Ben, it makes it easier to follow if you use some formatting to keep the code in tact:

    before the code block starts, type "code" in brackets. [ ] and at the end type "/code" in brackets.
    PUB yourcode
         test
         test 
    
    
  • BenClarkBenClark Posts: 20
    edited 2010-09-08 11:56
    I didn't realize you could do that.

    Thanks
  • T ChapT Chap Posts: 4,223
    edited 2010-09-08 12:46
    Ben

    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.
  • BenClarkBenClark Posts: 20
    edited 2010-09-08 13:02
    Thats about as small as I can get and still show the problem.
    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???
  • BenClarkBenClark Posts: 20
    edited 2010-09-08 13:07
    OK, now I'm really confused.
    I changed the if statement from
    if Buttons1StackPushPos >= BTNSTACKTOP
    

    to
    if Buttons1StackPushPos == BTNSTACKTOP
    

    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?
  • mparkmpark Posts: 1,305
    edited 2010-09-08 13:21
    The ">=" is actually the assignment form of the "greater than" operator. That is,
    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).
  • KyeKye Posts: 2,200
    edited 2010-09-08 13:21
    Equal sign on the right assigns every time.

    You wanted "=>"
  • pgbpsupgbpsu Posts: 460
    edited 2010-09-08 13:29
    Looks like Kye found the issue.

    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.
  • T ChapT Chap Posts: 4,223
    edited 2010-09-08 14:40
    Ben, what I was referring to as 'testing out smaller concepts" means that, when you are questioning a SPIN concept, in this case you have never used >= or => before I am assuming, but have concluded from the manual that your usage was correct, it would be easy to take that individual concept and test it out with a minimal test routine:
    If 10 >=  9
        'display something to show what happened
    
    If 10 =>  9
        'display something to show what happened
    

    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.
  • mparkmpark Posts: 1,305
    edited 2010-09-08 18:36
    Kye wrote: »
    Equal sign on the right assigns every time.

    Except for ==.
    pgbpsu wrote: »
    Looks like Kye found the issue.

    What am I, chopped liver?!

    /kidding
    //just trying out the multi-quote feature
Sign In or Register to comment.