Skip to content

Commit

Permalink
Implement Bluetooth socket type
Browse files Browse the repository at this point in the history
  • Loading branch information
sciguy16 committed Jan 7, 2024
1 parent 6676d31 commit 0450ca0
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 166 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,47 @@ description = "USB driver for communicating with the NXT brick"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[[example]]
name = "gui"
name = "bluetooth"
required-features = ["examples"]

[[example]]
name = "gamepad"
required-features = ["examples"]

[[example]]
name = "gui"
required-features = ["examples"]

[features]
default = ["usb", "bluetooth"]
examples = [
"strum",
"dep:eframe",
"dep:gilrs",
]
examples = ["strum", "dep:eframe", "dep:gilrs", "dep:tracing-subscriber"]
strum = ["dep:strum", "dep:strum_macros"]
usb = ["dep:rusb"]
bluetooth = ["dep:bluer", "tokio/rt"]

[dependencies]
async-trait = "0.1"
futures = "0.3"
num-derive = "0.4"
num-traits = "0.2"
thiserror = "1"
futures = "0.3"
tokio = { version = "1", features = ["sync"] }
async-trait = "0.1.77"
tracing = "0.1"

# USB support
rusb = { version = "0.9", optional = true }

# Bluetooth support
bluer = { version = "0.16", features = ["bluetoothd", "rfcomm"], optional = true }
bluer = { version = "0.16", features = [
"bluetoothd",
"rfcomm",
], optional = true }

strum = { version = "0.25", optional = true }
strum_macros = { version = "0.25", optional = true }
tracing-subscriber = { version = "0.3", optional = true }

# required for gui
eframe = { version = "0.24", optional = true }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const POWER: i8 = 80;
#[tokio::main]
async fn main() -> nxtusb::Result<()> {
let nxt = Nxt::first_usb().await?;
let _nxt2 = nxtusb::Bluetooth::wait_for_nxt().await?;
println!("Running motor A at {POWER}");
nxt.set_output_state(
Expand Down
11 changes: 7 additions & 4 deletions examples/bluetooth.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use nxtusb::Nxt;
#[tokio::main]
async fn main() -> nxtusb::Result<()> {
tracing_subscriber::fmt::init();

fn main() -> nxtusb::Result<()> {
// let device = nxtusb::Bluetooth::new();
// let _nxt = Nxt::init(device);
let nxt = nxtusb::Bluetooth::wait_for_nxt().await?;

let info = nxt.get_device_info().await?;
dbg!(info);

Ok(())
}
8 changes: 8 additions & 0 deletions examples/deviceinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ async fn main() -> nxtusb::Result<()> {

let info = nxt.get_device_info().await?;
println!("Device info:\n{info:?}");
println!(
"BT address: {}",
info.bt_addr
.iter()
.map(|byte| format!("{byte:02x}"))
.reduce(|acc, ele| format!("{acc}:{ele}"))
.unwrap_or_default()
);

let versions = nxt.get_firmware_version().await?;
println!("Versions:\n{versions:?}");
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub enum Error {
#[error("libusb error")]
Usb(#[from] rusb::Error),

#[cfg(feature="usb")]
#[error("bluetooth error")]
Bluetooth(#[from]bluer::Error),

#[error("device error")]
Device(#[from] crate::protocol::DeviceError),

Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use std::{
sync::Arc,
};

#[macro_use]
extern crate tracing;

#[cfg(feature = "strum")]
pub use strum::IntoEnumIterator;

Expand Down Expand Up @@ -107,14 +110,16 @@ impl Nxt {
}

/// Initialise an NXT struct from the given device
pub async fn init(
device: impl Socket + Send + Sync + 'static,
pub async fn init<D: Socket + Send + Sync + 'static>(
device: D,
) -> Result<Self> {
debug!("Initialise NXT from {} device", std::any::type_name::<D>());
let mut nxt = Self {
device: Arc::new(device),
name: String::new(),
};
let info = nxt.get_device_info().await?;
debug!("Connected device is named `{}`", info.name);
nxt.name = info.name;
Ok(nxt)
}
Expand Down
Loading

0 comments on commit 0450ca0

Please sign in to comment.