blink strip state

This commit is contained in:
Tobias Ollive 2022-01-24 16:17:54 +01:00
parent 33da38381a
commit 908a512c31
2 changed files with 45 additions and 9 deletions

View File

@ -1,3 +1,6 @@
mod patterns;
use patterns::{PixelColor, Strip};
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};
@ -57,19 +60,17 @@ async fn main() -> bluer::Result<()> {
async fn send_seq(char: &Characteristic) -> bluer::Result<()> { async fn send_seq(char: &Characteristic) -> bluer::Result<()> {
println!(" Characteristic flags : {:?}, ", char.flags().await?); println!(" Characteristic flags : {:?}, ", char.flags().await?);
let mut base_data: Vec<u8> = vec![0x00, 0x00, 0x01]; let mut strip_red : Strip<10>;
let mut off_data: Vec<u8> = vec![0x00, 0x00, 0x01]; strip_red.strip.fill(PixelColor { red: 255, green: 0, blue: 0});
for _ in 0..10 {
base_data.append(&mut vec![0xff, 0x00, 0x00]); let mut strip_green: Strip<10>;
off_data.append(&mut vec![0x00, 0x00, 0x00]); strip_green.strip.fill(PixelColor { red: 0, green: 255, blue: 0});
}
loop { loop {
char.write(&*base_data).await?; char.write(&*strip_green.to_array()).await?;
sleep(Duration::from_secs(1)).await; sleep(Duration::from_secs(1)).await;
char.write(&*off_data).await?; char.write(&*strip_red.to_array()).await?;
sleep(Duration::from_secs(1)).await; sleep(Duration::from_secs(1)).await;
} }
} }
@ -112,7 +113,9 @@ async fn find_neopixel_service(device: &Device) -> bluer::Result<Option<Characte
for service in device.services().await? { for service in device.services().await? {
if SERVICE_UUID == service.uuid().await? { if SERVICE_UUID == service.uuid().await? {
for char in service.characteristics().await? { for char in service.characteristics().await? {
println!("uuid : {}", &char.uuid().await?);
if PIXEL_DATA_UUID == char.uuid().await? { if PIXEL_DATA_UUID == char.uuid().await? {
return Ok(Some(char)); return Ok(Some(char));
} }

33
src/patterns.rs Normal file
View File

@ -0,0 +1,33 @@
use bluer::gatt::remote::Characteristic;
const BASE_STRIP_DATA : [u8; 5] = [0x0, 0x0, 0x0, 0x0, 0x1];
#[derive(Clone, Copy)]
pub(crate) struct PixelColor {
pub(crate) red: u8,
pub(crate) green: u8,
pub(crate) blue: u8,
}
pub(crate) struct Strip<const N: usize> {
pub strip: [PixelColor; N],
}
impl<const N: usize> Strip<N> {
pub fn to_array(&self) -> Vec<u8> {
let mut data : Vec<u8> = vec![];
for i in 0..N {
data.append(&mut vec![self.strip[i].red, self.strip[i].green, self.strip[i].blue]);
}
data
}
}
async fn write_strip<const N: usize>(data: Strip<N>, char: Characteristic) -> bluer::Result<()> {
let mut frame= BASE_STRIP_DATA.to_vec();
frame.append(&mut data.to_array());
char.write(&*frame).await?;
Ok(())
}