The Rust bindings allow you to control Bricks and Bricklets from your Rust programs. The ZIP file for the bindings contains:
src/
the source code of the bindingsexamples/
the examples for every Brick and BrickletThe Rust bindings are also hosted on crates.io.
The Rust bindings can either be installed from the zip file, or by downloading them from crates.io.
If you add tinkerforge = "2.*"
to your Cargo.toml
's [dependencies]
section, cargo
will download the bindings and all dependencies on the next run.
If you want to install them from the zip file, download and extract it and register the dependency by adding
tinkerforge = {path=[your/path/here]}
to the [dependencies]
section of your
Cargo.toml
, specifing the correct path (without src
at the end) instead of the placeholder. The next run of cargo will download the binding's dependencies.
To test a Rust example Brick Daemon and Brick Viewer have to be installed first. Brick Daemon acts as a proxy between the USB interface of the Bricks and the API bindings. Brick Viewer connects to Brick Daemon and helps to figure out basic information about the connected Bricks and Bricklets.
As an example let's compile the configuration example for the Stepper Brick
from the command line. For this we create a new folder example_project
,
run cargo init
in this folder, and then copy the example_configuration.rs
file from the examples/StepperBrick/
folder into the src
folder which was
created by cargo. Rename the copied file to main.rs
(overwriting the one
created by cargo):
example_project/
-> Cargo.toml
-> src/main.rs (was example_configuration.rs)
Then add either tinkerforge = "2.*"
or tinkerforge = {path=[your/path/here]}
into the [dependencies]
section of your project's Cargo.toml
.
In the example HOST
and PORT
specify at which network address the
Stepper Brick can be found. If it is connected locally to USB then localhost
and 4223 is correct. The UID
value has to be changed to the UID of the
connected Stepper Brick, which you can figure out using Brick Viewer:
const HOST: &str = "localhost";
const PORT: u16 = 4223;
const UID: &str = "XXYYZZ"; // Change XXYYZZ to the UID of your Stepper Brick.
Now we can build and run the project with cargo run
.
Links to the API reference for the IP Connection, Bricks and Bricklets as well as the examples from the ZIP file of the bindings are listed in the following table. Further project descriptions can be found in the Kits section.