From 33da38381a688eb039c42c39fc5db3933e651f26 Mon Sep 17 00:00:00 2001 From: Tobias Ollive Date: Sat, 22 Jan 2022 16:17:37 +0100 Subject: [PATCH] add small routine to send data --- Cargo.lock | 9 +-------- Cargo.toml | 3 +-- src/main.rs | 30 +++++++++++++++++++++++------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c566c0..27ef690 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,7 +39,7 @@ dependencies = [ "strum", "tokio", "tokio-stream", - "uuid 0.8.2", + "uuid", ] [[package]] @@ -417,7 +417,6 @@ dependencies = [ "bluer", "futures", "tokio", - "uuid 1.0.0-alpha.1", ] [[package]] @@ -565,12 +564,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "uuid" -version = "1.0.0-alpha.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb3ab47baa004111b323696c6eaa2752e7356f7f77cf6b6dc7a2087368ce1ca4" - [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 228afc6..aab3aa0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,4 @@ edition = "2018" [dependencies] bluer = "0.13.1" tokio = "1.15.0" -futures = "0.3.19" -uuid = "1.0.0-alpha.1" \ No newline at end of file +futures = "0.3.19" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 26dc67a..8e8d2c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use bluer::{AdapterEvent, Device}; use bluer::gatt::remote::Characteristic; use futures::{pin_mut, StreamExt}; -use std::error::Error; +use std::time::Duration; +use tokio::time::sleep; const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8); const PIXEL_DATA_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0903c33242a893bd25e905756cb8); @@ -25,8 +26,12 @@ async fn main() -> bluer::Result<()> { let device = adapter.device(addr)?; match find_neopixel_service(&device).await { Ok(Some(char)) => { - send_seq(char) println!("found characteristic"); + match send_seq(&char).await { + // this loop should never stop + _ => () + } + } Ok(None) => { println!("characteristic not found") @@ -49,13 +54,24 @@ async fn main() -> bluer::Result<()> { Ok(()) } -async fn send_seq(char: &Characteristic) -> { +async fn send_seq(char: &Characteristic) -> bluer::Result<()> { println!(" Characteristic flags : {:?}, ", char.flags().await?); - let base_data = vec![0x00, 0x00, 0x01]; - for i in 0..10 { -# base_data. + let mut base_data: Vec = vec![0x00, 0x00, 0x01]; + let mut off_data: Vec = vec![0x00, 0x00, 0x01]; + for _ in 0..10 { + base_data.append(&mut vec![0xff, 0x00, 0x00]); + off_data.append(&mut vec![0x00, 0x00, 0x00]); } + + loop { + char.write(&*base_data).await?; + sleep(Duration::from_secs(1)).await; + char.write(&*off_data).await?; + sleep(Duration::from_secs(1)).await; + + } + } async fn connect(device: &Device, retries: u8) -> bluer::Result<()> { @@ -65,7 +81,7 @@ async fn connect(device: &Device, retries: u8) -> bluer::Result<()> { for i in 0..retries { match device.connect().await { Ok(()) => return Ok(()), - Err(err) => { + Err(_) => { println!("connection error ({}), retry…", i); } }