Shop OBEX P1 Docs P2 Docs Learn Events
can you do this with simple pointers somehow? — Parallax Forums

can you do this with simple pointers somehow?

s2jesses2jesse Posts: 62
edited 2009-08-24 01:56 in Propeller 1


repeat   buttonCheck(right, rightState, rightPressed, rightReleased)  
    if rightPressed
      OutA[noparse][[/noparse]led] := ledOn      
    if rightReleased
     OutA[noparse][[/noparse]led] := ledOff            
 
 
 
 
PUB buttonCheck(button, buttonState, buttonPressed, buttonReleased) 
  if ina[noparse][[/noparse]button] <> buttonState 'if button has changed 
   'CHANGED               
    if ina[noparse][[/noparse]button]
      'PRESSED          
      buttonPressed:=1         
    else
      'RELEASED
      buttonReleased:=1 
  else
    'NO CHANGE    
    buttonPressed:=0  
    buttonReleased:=0                     
  buttonState := ina[noparse][[/noparse]button]

i want this little button meathod to work with multiple buttons and pass in which button i want to change the variable for... can i do that by doing something similar to what i have here with pointers to the variables? i already have the state,pressed, and released variables declared ahead of time.

Jesse

Comments

  • TimmooreTimmoore Posts: 1,031
    edited 2009-08-24 00:29
    Heres 1 way to do it
    repeat 
       buttonCheck(right, @rightState, @rightPressed, @rightReleased)  
        if rightPressed
          OutA[noparse][[/noparse]led] := ledOn      
        if rightReleased
         OutA[noparse][[/noparse]led] := ledOff            
     
     
     
     
    PUB buttonCheck(button, buttonStatePtr, buttonPressedPtr, buttonReleasedPtr) 
      if ina[noparse][[/noparse]button] <> long[noparse][[/noparse]buttonStatePtr] 'if button has changed 
       'CHANGED               
        if ina[noparse][[/noparse]button]
          'PRESSED          
          long[noparse][[/noparse]buttonPressedPtr] :=1         
        else
          'RELEASED
          long[noparse][[/noparse]buttonReleasedPtr] :=1 
      else
        'NO CHANGE    
        long[noparse][[/noparse]buttonPressedPtr] :=0  
        long[noparse][[/noparse]buttonReleasedPtr] :=0                     
      long[noparse][[/noparse]buttonStatePtr] := ina[noparse][[/noparse]button]
    
    
  • s2jesses2jesse Posts: 62
    edited 2009-08-24 00:50
    sweet worked thank you! now i just gotta figure out a debounce to toss in
  • TimmooreTimmoore Posts: 1,031
    edited 2009-08-24 01:25
    When ina[noparse][[/noparse]button] changes, Save cnt, next time if 10ms has gone by, look to see if ina[noparse][[/noparse]button] is the same, if so change state
  • s2jesses2jesse Posts: 62
    edited 2009-08-24 01:43
    this must not be what you mean [noparse]:)[/noparse]· not quite workin... I totally get the concept.. but the implemention isnt gelling in my brain yet [noparse]:)[/noparse]


    PUB buttonCheck(button, buttonStatePtr, buttonPressedPtr, buttonReleasedPtr) | lastDebounceTime 
      debounceDelay := 800_000
      
      if ina[noparse][[/noparse]button] <> byte[noparse][[/noparse]buttonStatePtr] 'if button has changed 
       'CHANGED
        lastDebounceTime := CNT                                                                     
        if ina[noparse][[/noparse]button]
          'PRESSED          
          byte[noparse][[/noparse]buttonPressedPtr]:=1         
        else
          'RELEASED
          byte[noparse][[/noparse]buttonReleasedPtr]:=1 
      else
        'NO CHANGE    
        byte[noparse][[/noparse]buttonPressedPtr]:=0  
        byte[noparse][[/noparse]buttonReleasedPtr]:=0                     
      if CNT - lastDebounceTime > debounceDelay 
        byte[noparse][[/noparse]buttonStatePtr] := ina[noparse][[/noparse]button] 
    


    ·
  • TimmooreTimmoore Posts: 1,031
    edited 2009-08-24 01:56
    I would make buttonstate have 4 values - pressed, not-pressed, maybepressed, maybenotpressed
    make lastdebouncetime external - same as other paras

    if state is not-pressed and ina[noparse][[/noparse]button] <> not-pressed
    state := maybepressed
    debouncetime = cnt
    elseif state is maybepressed and cnt - debouncetime > delay
    if ina[noparse][[/noparse]button] <> not-pressed
    state = pressed

    similar for other states
Sign In or Register to comment.