4 ms·
I have a pretty cool use case where I successfully interfaced between C++ and Rust code. This was for a project consisting of a sculpture with a bunch of LED s
by codetrotter 5y ago
I have a pretty cool use case where I successfully interfaced between C++ and Rust code.
This was for a project consisting of a sculpture with a bunch of LED strips on it, and my part was to do the programming, electronics and wiring for it.
To control the LED strips I use Teensy 3.2 microcontroller boards, with the OctoWS2811 adaptor board [0], along with an industrial singleboard computer.
The Teensy 3.2 boards I programmed using the Arduino libraries and the OctoWS2811 library [1]. The code I wrote for this part was C++. (But I don’t use the Arduino IDE, I use JetBrains CLion.)
Compiling and flashing the firmware for the Teensy, and disconnecting and reconnecting cables etc was making the development a bit annoying.
So in Rust I wrote a LED simulator program that renders the “pixels” of the LED strips on my screen, and I compiled my C++ code that I’ve written for the microcontrollers on the host, with some shims that I wrote to provide some functions that my firmware uses, and I link this with my Rust program.
The end result is that I have a fully functioning LED simulator that I use while developing, that runs on my computer. It runs on both macOS and Linux. And then the same C++ code of mine gets included and built without modification when I build my firmware for the microcontrollers.
It is work that I am very satisfied about :)
Should also note that I use the bindgen crate, not cxx. https://crates.io/crates/bindgen https://crates.io/crates/bindgen
[0]: https://www.pjrc.com/store/octo28_adaptor.html https://www.pjrc.com/store/octo28_adaptor.html
[1]: https://www.pjrc.com/teensy/td_libs_OctoWS2811.html https://www.pjrc.com/teensy/td_libs_OctoWS2811.html
- chris_overseas 5y agoThis sounds great, I'd been thinking about creating something similar (I also have a project with Teensy 3.2/4.0, OctoWS2811 and CLion). Is there any chance you'd be willing to open source it? In particular I'd be interested in what you did to get the Teensy code compiling and running on the host with the shims you mention.
- girvo 5y agoIf your board supports it, the Zephyr RTOS and build tools support building your firmware for a Linux binary target too :) even has a neat RPC protocol to test the firmware on your computer rather than on the board. Probably too much for a Teensy tho, but it is supported: https://docs.zephyrproject.org/latest/boards/arm/teensy4/doc/index.html https://docs.zephyrproject.org/latest/boards/arm/teensy4/doc...