Shop OBEX P1 Docs P2 Docs Learn Events
Passing method pointer as parameter — Parallax Forums

Passing method pointer as parameter

kolyurkolyur Posts: 43
edited 2023-12-11 15:23 in PASM2/Spin2 (P2)

I'm wondering if there's an easy fix for what I'm trying to do. My Spin 2 program sends commands to a TFT display to draw objects. I have a method that draws a numeric entry keypad, waits for user entry, then returns to the previous screen. When entry is complete, I'd like the option to run a method specified by the caller. I can do this with a method pointer:

PUB DoNumericEntry(xPos, yPos, {lots more parameters}, MethodToRunWhenComplete)
  { numeric entry code }
  MethodToRunWhenComplete()
  {code to return to previous screen }

And it would be called like this:

DoNumericEntry(200, 50, {other parameter values}, @SomeMethod)

Generally this works fine. But the problem is that SomeMethod can't have any parameters of its own in order to be passed as part of DoNumericEntry. It would be nice if SomeMethod could have any number of parameters, but I don't know how to accomplish this. Clearly this doesn't work:

DoNumericEntry(200, 50, {other parameter values}, @SomeMethod(a, b, c))

My current workaround is to create an intermediate method that calls SomeMethod with global variables:

PUB SomeMethodWrapper()
  SomeMethod(global1, global2, global3)

And use that as the method pointer:

DoNumericEntry(200, 50, {other parameter values}, @SomeMethodWrapper)

Is there a better way to accomplish this? I should mention that everything is in the same cog.

Comments

  • I think you can pass a method pointer -- which is just a long -- but you'll have to use globals as you're doing. I've never really worked with method pointers so I did this simple test.

    con
    
      #0, LED_ON, LED_OFF
    
    
    var
    
      long blinkpin
      long ontime
      long offtime
    
    
    pub main() | do_it
    
      do_it := @blink
    
      waitms(1000)
    
      repeat 3
        do_it(56, 1000, 1000)
    
      blinkpin, ontime, offtime := 57, 500, 500
    
      repeat 3
        test_method_pointer(@blink)
    
      repeat
    
    
    pub test_method_pointer(p_method)
    
      p_method(blinkpin, ontime, offtime)
    
    
    pub blink(pin, onms, offms)
    
      pinwrite(pin, LED_ON)
      waitms(onms)
      pinwrite(pin, LED_OFF)
      waitms(offms)
    
  • This might be a better way to go if you're method exists in another object.

    var
    
      long blinkpin
      long ontime
      long offtime
    
    
    con
    
      #0, LED_ON, LED_OFF
    
    
    pub main() | do_it
    
      do_it := @blink
    
      waitms(1000)
    
      repeat 3
        do_it(56, 1000, 1000)
    
      blinkpin, ontime, offtime := 57, 500, 500
    
      repeat 3
        test_method_pointer(@blink, @blinkpin)
    
      repeat
    
    
    pub test_method_pointer(p_method, p_params)
    
      p_method(long[p_params][0], long[p_params][1], long[p_params][2])
    
    
    pub blink(pin, onms, offms)
    
      pinwrite(pin, LED_ON)
      waitms(onms)
      pinwrite(pin, LED_OFF)
      waitms(offms)
    
Sign In or Register to comment.