Currently this project is not compatible with the BrickPi platform.
```rust extern crate ev3devlangrust;
use ev3devlangrust::Ev3Result; use ev3devlangrust::motors::{LargeMotor, MotorPort}; use ev3devlangrust::sensors::ColorSensor;
fn main() -> Ev3Result<()> {
// Get large motor on port outA.
let large_motor = LargeMotor::get(MotorPort::OutA)?;
// Set command "run-direct".
large_motor.run_direct()?;
// Run motor.
large_motor.set_duty_cycle_sp(50)?;
// Find color sensor. Always returns the first recognised one.
let color_sensor = ColorSensor::find()?;
// Switch to rgb mode.
color_sensor.set_mode_rgb_raw()?;
// Get current rgb color tuple.
println!("Current rgb color: {:?}", color_sensor.get_rgb()?);
Ok(())
} ```
There is a template repository that contains all the required configurations for cross-compilation and perfomance/binary-size optimizations for this "Hello World" example.
LargeMotor [lego-ev3-l-motor, lego-nxt-motor]MediumMotor [lego-ev3-m-motor]TachoMotor: Useful wrapper around LargeMotor and MediumMotor to make common functions easier to useColorSensor [lego-ev3-color]CompassSensor [ht-nxt-compass]GyroSensor [lego-ev3-gyro]InfraredSensor [lego-ev3-ir]IrSeekerSensor [ht-nxt-ir-seek-v2]LightSensor [lego-nxt-light]TouchSensor [lego-ev3-touch, lego-nxt-touch]UltrasonicSensor [lego-ev3-us, lego-nxt-us]Ev3Button: Provides access to the integrated buttons on the ev3 brickLed: Provides access to the integrated leds on the ev3 brickPowerSupply: Provides access to the power supply informationScreen: Provides access to the integrated display of the ev3 bricksound: Provides access to the integrated speakers of the ev3 brickInstall cross and the armv5te toolchain
bash
cargo install cross
rustup target add armv5te-unknown-linux-gnueabi
Build binary with docker
bash
cross build --release --target armv5te-unknown-linux-gnueabi
The --release flag is optional. However, it can speed up the execution time by a factor of 30.
The target binary is now in target/armv5te-unknown-linux-gnueabi/release/{application_name}
If the above compilation with cross failed you can try this manual approach.
Create target configuration in .cargo/config
toml
[target.armv5te-unknown-linux-gnueabi]
linker = "/usr/bin/arm-linux-gnueabi-gcc"
Get the docker image. You can either download the prebuild image or build it yourself with the provided Dockerfile (docker/Dockerfile).
```bash docker pull pixix4/ev3dev-rust
docker build . -t pixix4/ev3dev-rust --no-cache ```
Build binary
```bash
docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust cargo build --release --target armv5te-unknown-linux-gnueabi
docker run --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust \ cargo build --release --target armv5te-unknown-linux-gnueabi ```
If you use the direct method you will notice that each build gets stuck at Updating crates.io index for a long time. To speed up this step you can use the vendoring machanic of cargo.
bash
cargo vendor
Execute the above command and add this addtional config to .cargo/config.
```toml [source.crates-io] replace-with = "vendored-sources"
[source.vendored-sources] directory = "vendor" ```
To reduce the resulting binary size you can try the following steps:
Cargo.toml:toml
[profile.release]
lto = true
```bash
docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust /usr/bin/arm-linux-gnueabi-strip /build/target/armv5te-unknown-linux-gnueabi/release/{application_name}
docker run --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust \ /usr/bin/arm-linux-gnueabi-strip /build/target/armv5te-unknown-linux-gnueabi/release/{application_name} ```
With this you can reduce the binary size of the "Hello World" example by more than 90%.
If you have problems with code completion or inline documentation with rust analyzer it may help to enable to following settings:
json
{
"rust-analyzer.cargo.loadOutDirsFromCheck": true,
"rust-analyzer.procMacro.enable": true
}
(Example from VSCode settings.json)