How do i define a callback function in CFFI?

Discussion of Common Lisp

How do i define a callback function in CFFI?

Postby joeish80829 » Sat Oct 26, 2013 6:11 pm

the below function create-trackbar-example works except for the last parameter of create-trackbar, "test" ..the test parameter is supposed to be a function that gets called when the trackbar moves. cvCreateTrackbar, the function I'm wrapping, creates a gui slider on the Opencv output window it can either change the variable of a running program or call a function when the slider is moved

more info on the last parameter of cvCreateTrackbar, called onChange, here ... 0on_change)

In the documentation for cvCreateTrackbar it says: for onChange:

"onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated."

im trying to convert the second code on this page to lisp
...Im pretty sure i have all functions wrapped correctly but in theory create-trackbar could be wrong the last parameter in my wrapper is a defctype of :pointer type called cv-trackbar-callback

here is my attempt but i have no idea how to define a function as a pointer and ive tried alot of variations but is too long to post. The defun change contrast compiles btw...i tried using make-pointer to make the change-contrast function a pointer but make-pointer wants it to be a cant use foreign-alloc i dont think because the function needs to be converted to a pointer first to use that...any guidance is appreciated

Code: Select all
(defun change-contrast (&optional contrast img dest)
(if (< contrast 10) (scale img dest (/ 1 (coerce (- 11 contrast) 'double-float)))
    (if (>= contrast 10) (scale img dest (- contrast 9))))
     (show-image "MyImage" dest))

(defun display (filename)
  "Open the image FILENAME and show it in a window."
  (let* ((img (load-image filename 1))
        (img-size (get-size img))
       (dest (create-image img-size +ipl-depth-8u+ 3))
       (contrast (cffi:foreign-alloc :int :initial-contents '(10))

      (test "not sure what to do here")))
    (named-window "MyWindow" 1)
   (create-trackbar "conrast" "MyWindow" contrast 21 test)
 (princ (mem-ref contrast :int))
  (change-contrast (mem-ref contrast :int) img dest)
    (loop while (not (= (wait-key 0) 27)))
    (release-image img)
    (release-image dest)
    (destroy-window "MyWindow")))
Posts: 96
Joined: Tue Sep 03, 2013 5:32 am

Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests