raslib
Raspberry PI library for Rust. GPIO controller, L298N motors, sockets and "i2clib" integrated
All tests are made on Raspberry PI 4B+ on Raspbian OS
Overview
- GPIO controller
- L298N motors controller (needs the GPIO controller)
NOTE there is no server socket generator because the Rust standard library already contains a really good way to create sockets
SEE here to create a server socket
Install
In your project Cargo.toml
file, add the "raslib" dependency :
[dependencies]
raslib = { git = "https://github.com/antoninhrlt/raslib" }
And then, build your project : cargo build
Examples
-
Blink a led
use raslib; use raslib::gpio::Gpio; fn blink_led() -> Result<(), std::io::Error> { let led = Gpio::new(16)?; loop { led.write(raslib::HIGH)?; raslib::sleep(1000); led.write(raslib::LOW)?; raslib::sleep(1000); } }
-
L298N motor
use raslib::l298n::L298n; fn forward_forever() -> Result<(), std::io::Error> { let mut motor_left = L298n::new(18, 15, 14); let mut motor_right = L298n::new(9, 7, 25); motor_left.forward()?; motor_right.forward()?; Ok(()) }
Server socket
From doc.rust-lang.org
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;
fn server() {
let server = TcpListener::bind("<raspi ip>:9000").unwrap();
for stream in server.incoming() {
let mut stream: TcpStream = stream.unwrap();
let mut signal = [0; 1];
loop {
stream.read(&mut signal);
if signal[0] == 0 {
break;
}
// does things
}
}
}