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

View File

@@ -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<u8> = vec![0x00, 0x00, 0x01];
let mut off_data: Vec<u8> = 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);
}
}