There is a functions in the 'C' wiringPi library with type,
extern void (*pinMode) (int pin, int mode) ;
I tried calling it from haskell using the FFI with FunPtr. So I did,
foreign import ccall unsafe "wiringPi.h &pinMode" c_pinMode
:: FunPtr (CInt -> CInt -> IO ())
foreign import ccall "dynamic" dc_pinMode
:: FunPtr (CInt -> CInt -> IO ()) -> (CInt -> CInt -> IO ())
but for some reason, even though it compiles, it doesn't seem to be calling the function that 'pinMode' points to.
So I tried using normal Foreign.Ptr, thinking I might be able to peek at the Ptr to get a reference to the underlying 'C' function pointed to by 'pinMode'. So I tried,
foreign import ccall "wiringPi.h &pinMode" c_pinMode
:: Ptr (Ptr (CInt -> CInt -> IO ()))
And then, in the implementation of the haskell function that calls 'pinMode' I used peek twice to get a reference to the underlying function. But I keep getting compilation errors where the compiler tell me that the function of type (CInt -> CInt -> IO ())
is not an instance of the 'Storable' typeclass.
So I checked out the storable typeclass, to make (CInt -> CInt -> IO ())
an instance of the storable typeclass.. The minimum implementation required is for peek, poke and a few other functions.. I realized, it really shouldn't be so difficult to call a function that is reference by a pointer..
I feel like I am missing something fundamental. Can someone please point me in the right direction?
Thanks and Regards
extern void (*pinMode) (int pin, int mode);
declarespinMode
as a pointer to a function taking twoint
s and returningvoid
. Tryforeign import ccall unsafe "wiringPi.h pinMode" c_pinMode
, taking the address of the pointer gives you one level of indirection too much (but I'm not too familiar with the FFI, so I'm not 100% sure I interpret it right). – Daniel FischerpinMode
a function or a function pointer? If it's a function pointer, I think the type offoreign import ccall unsafe "wiringPi.h &pinMode" c_pinMode
should bePtr (FunPtr (CInt -> CInt -> IO ()))
. – nymkPtr (FunPtr (CInt -> CInt -> IO ()))
, I can't doc_pinMode (3 :: CInt) (4 :: CInt)
. I do not think this works. However I will try what you said. – Jay