With Rust module support getting usable slowly I’d like to add an actually sensible Rust module (also to have something testable there that’s more than a reimplementation of the ps shell command) – nothing completely made up, but also nothing that people later would like to use on an unsupported platform but can’t.
I’m listing a few ideas as a poll (without any promise to implement the winner); if you’re missing something please add it below:
SAUL device that reports to stdout (like native’s LED0_ON/OFF)
One example of glue code for a sensor could be the LSM303AGR – it’s on the microbit-v2, our LSM303DLHC driver acted up terribly when I tried extending it to the 303AGR (because even when hardware documentation is OKish, I’ve yet to see any vendor that provides docs in such a fashion that it’s easy to abstract over different versions of a chip), and there is a crate out there that appears to do all the right things (including using the embedded-hal interface that’s available for RIOT peripherals).
I promised myself I would learn Rust this year. I’ve started working on the Tutorials on the website, but once I finish those I might give porting a driver in RIOT to Rust a go. (Or does that not make much sense? Do drivers need to be ported, or should we just write some kind of interface to the existing C driver?)
Drivers don’t need porting; just their high-level interfaces need being wrapped.
For example, all of the environment sensors implement SAUL, and SAUL is wrapped, so any board’s temperature can be read by looking up temperature sensors and reading their value from Rust.
What I’m thinking of here is using a Rust implementation someone else wrote for a sensor we don’t support yet and using it to implement a SAUL driver. Rust drivers are easy to use: The example on https://crates.io/crates/lsm303agr (written for Linux, without consideration for no_std) can be copy-pasted into the hello-world example replacing main, and needs just the two lines about taking a Linux device by file name replaced with the RIOT version, and the crate added – then, it produces accelerometer output:
--- old 2022-01-13 05:24:58.876984409 +0100
+++ new 2022-01-13 05:25:37.013237620 +0100
@@ -1,7 +1,7 @@
-use linux_embedded_hal::I2cdev;
+use riot_wrappers::i2c::I2CDevice;
use lsm303agr::{AccelOutputDataRate, Lsm303agr};
fn main() {
- let dev = I2cdev::new("/dev/i2c-1").unwrap();
+ let dev = I2CDevice::new(0);
let mut sensor = Lsm303agr::new_with_i2c(dev);
sensor.init().unwrap();