While exploring how Go uses USB HID, I wondered if I can do the same in Dart. Node.js can do this via node-hid and it’s using its NAPI interface for this. Tested and worked: I could connect to the PS3 controller and turn on LEDs and motors at will. But what about Dart?
Dart has a new FFI interface since 2.12 and I never used it. Time to learn how this works in detail!
Unfortunately the second example I looked at (after hello_world()) was not using ffigen, but from what I read, it’s easier to use unless you prefer to create the Dart-C-bindings manually.
So I took the non-ffigen-using-example and ffigen’ified it: https://github.com/haraldkubota/dart-ffi
Notes: The varargs example does not work. The original sample here at line 36 even cheats: it defines MultiSum with 4 parameters. While this works, it won’t work with 3 or 5 parameters. So I skip this because I don’t think Dart can do varargs currently.