
I have a bunch of global variables in C I would like to directly read and write from Haskell. Is this possible with FFI, or must I write a bunch of C wrapper functions for the interface, i.e. a 'get' and a 'set' for each variable? I'm building a simulator for one of our embedded systems. The C is embedded code for an ECU -- the global variables are sensor and control IO. I'm using Haskell to integrate the C with vehicle models and to provide the user interface. With the help of the SDL hackage lib, I'm able to feed analog and discrete inputs from a gamepad into the simulator, which then runs the embedded code and streams VCD data into GTKWave, providing waveform visualization in realtime. Pretty fun stuff. -Tom

On Tue, Apr 20, 2010 at 8:48 AM, Tom Hawkins
I have a bunch of global variables in C I would like to directly read and write from Haskell. Is this possible with FFI, or must I write a bunch of C wrapper functions for the interface, i.e. a 'get' and a 'set' for each variable?
I believe Maurício C. Antunes' bindings-DSL has support for automating reading from and writing to global variables. Look at the "Detailed usage guide" from the home page of: http://hackage.haskell.org/package/bindings-DSL regards, Bas

Tom Hawkins wrote:
I have a bunch of global variables in C I would like to directly read and write from Haskell. Is this possible with FFI,
Yes it is, as explained in section 4.1.1. in the FFI specification [1]. An import for a global variable int bar would look like this: foreign import ccall "&bar" bar :: Ptr CInt The difference to an import of a function int foo() is the extra "&". HTH, Bertram [1] http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi/ffise4.html#x7-170004.1.1

On Tue, Apr 20, 2010 at 01:48:16AM -0500, Tom Hawkins wrote:
I have a bunch of global variables in C I would like to directly read and write from Haskell. Is this possible with FFI, or must I write a bunch of C wrapper functions for the interface, i.e. a 'get' and a 'set' for each variable?
Yup. foo.c: int my_int; Foo.hs: foreign import "&my_int" my_int_ptr :: Ptr Int foo = do poke my_int_ptr 4 x <- peek my_int_Ptr John -- John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/
participants (4)
-
Bas van Dijk
-
Bertram Felgenhauer
-
John Meacham
-
Tom Hawkins