Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

USB

USB is the default transport (feature usb). It’s implemented in pure Rust with nusb, so no libusb is required. See Installation for the one-time udev (Linux) or WinUSB (Windows) setup.

use nxt_rs::prelude::*;

fn main() -> nxt_rs::Result<()> {
let brick = Brick::usb_first().block()?;        // the first brick found
let bricks = Brick::usb_all().block()?;          // every connected brick
Ok(()) }

Choosing between several bricks

Usb::list() returns the bricks on the bus without opening them. The USB serial number is the brick’s Bluetooth address in hex, which is a stable way to tell bricks apart:

use nxt_rs::{Brick, blocking::Block, transport::Usb};

fn main() -> nxt_rs::Result<()> {
for device in Usb::list().block()? {
    println!("{:?} on bus {}", device.serial_number(), device.bus_id());
}

let wanted = Usb::list()
    .block()?
    .into_iter()
    .find(|d| d.serial_number() == Some("0016530A0B0C"))
    .expect("brick not connected");
let brick = Brick::new(wanted.open().block()?);
Ok(()) }

Details

  • The brick shows up as vendor 0x0694, product 0x0002, with bulk endpoints 0x01 (out) and 0x82 (in) on interface 0.
  • Only one program can open the brick at a time.
  • The default request timeout is 1 second.
  • USB-only commands, such as rebooting into firmware-update mode or resetting Bluetooth, are only available on a Brick<Usb>. See System commands.