add small routine to send data

This commit is contained in:
Tobias Ollive 2022-01-22 16:17:37 +01:00
parent 534b300931
commit 33da38381a
3 changed files with 25 additions and 17 deletions

9
Cargo.lock generated
View File

@ -39,7 +39,7 @@ dependencies = [
"strum", "strum",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"uuid 0.8.2", "uuid",
] ]
[[package]] [[package]]
@ -417,7 +417,6 @@ dependencies = [
"bluer", "bluer",
"futures", "futures",
"tokio", "tokio",
"uuid 1.0.0-alpha.1",
] ]
[[package]] [[package]]
@ -565,12 +564,6 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "uuid"
version = "1.0.0-alpha.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb3ab47baa004111b323696c6eaa2752e7356f7f77cf6b6dc7a2087368ce1ca4"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.10.2+wasi-snapshot-preview1" version = "0.10.2+wasi-snapshot-preview1"

View File

@ -9,4 +9,3 @@ edition = "2018"
bluer = "0.13.1" bluer = "0.13.1"
tokio = "1.15.0" tokio = "1.15.0"
futures = "0.3.19" futures = "0.3.19"
uuid = "1.0.0-alpha.1"

View File

@ -1,7 +1,8 @@
use bluer::{AdapterEvent, Device}; use bluer::{AdapterEvent, Device};
use bluer::gatt::remote::Characteristic; use bluer::gatt::remote::Characteristic;
use futures::{pin_mut, StreamExt}; 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 SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8);
const PIXEL_DATA_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0903c33242a893bd25e905756cb8); 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)?; let device = adapter.device(addr)?;
match find_neopixel_service(&device).await { match find_neopixel_service(&device).await {
Ok(Some(char)) => { Ok(Some(char)) => {
send_seq(char)
println!("found characteristic"); println!("found characteristic");
match send_seq(&char).await {
// this loop should never stop
_ => ()
}
} }
Ok(None) => { Ok(None) => {
println!("characteristic not found") println!("characteristic not found")
@ -49,13 +54,24 @@ async fn main() -> bluer::Result<()> {
Ok(()) Ok(())
} }
async fn send_seq(char: &Characteristic) -> { async fn send_seq(char: &Characteristic) -> bluer::Result<()> {
println!(" Characteristic flags : {:?}, ", char.flags().await?); println!(" Characteristic flags : {:?}, ", char.flags().await?);
let base_data = vec![0x00, 0x00, 0x01]; let mut base_data: Vec<u8> = vec![0x00, 0x00, 0x01];
for i in 0..10 { let mut off_data: Vec<u8> = vec![0x00, 0x00, 0x01];
# base_data. 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<()> { 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 { for i in 0..retries {
match device.connect().await { match device.connect().await {
Ok(()) => return Ok(()), Ok(()) => return Ok(()),
Err(err) => { Err(_) => {
println!("connection error ({}), retry…", i); println!("connection error ({}), retry…", i);
} }
} }