Shop OBEX P1 Docs P2 Docs Learn Events
can we call a function using a variable — Parallax Forums

can we call a function using a variable

senariosenario Posts: 1
edited 2013-05-24 09:21 in Propeller 1
hello guys

i am a newbie in propeller and don't know much about spin
i wanted to know that can we call a function using a variable

this is not the full code
[FONT=comic sans ms][SIZE=4]
[/SIZE][/FONT]    cognew(function(23,clkfreq), @stack[10])    

PUB one (pin,rate)
     
     dira[pin]~~


  repeat 2
     outa[pin] := 1
     waitcnt(rate + cnt)
     outa[pin] := 0
     waitcnt(rate + cnt)
     
     
PUB two (pin,rate)


     dira[pin]~~
     
   repeat 5  
     outa[pin] := 1
     waitcnt(rate + cnt)
     outa[pin] := 0
     waitcnt(rate + cnt)


instead of function can we assign a variable to one or two on that place
i had tried a lot and i need some help now
any kind of help will be appreciated
thanks

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-05-24 05:45
    Sadly not.

    There is a thread going on here about method or object pointers possibly being implemented in the next version of Spin.
    Why don't you just add another parameter to the pin toggling function that takes the toggle rate 2 or 5.
    Something like:
    cognew(pulse(23,clkfreq, 2), @stack[10])
    
    PUB pulse (pin,rate, count)
         
         dira[pin]~~
    
    
      repeat count
         outa[pin] := 1
         waitcnt(rate + cnt)
         outa[pin] := 0
         waitcnt(rate + cnt)
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-05-24 09:21
    You can use your function to generate a number and then call the desired method with a case statement.
    result := Function ' Function would be a method that returns 1 or 2.
    
      case result
        1:
          cognew(One(23,clkfreq), @stack[10])  
        2:
          cognew(Two(23,clkfreq), @stack[10])  
    
    
Sign In or Register to comment.