diff --git a/src/main.rs b/src/main.rs index 8e8d2c9..56d9a48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +mod patterns; + +use patterns::{PixelColor, Strip}; use bluer::{AdapterEvent, Device}; use bluer::gatt::remote::Characteristic; use futures::{pin_mut, StreamExt}; @@ -57,19 +60,17 @@ async fn main() -> bluer::Result<()> { async fn send_seq(char: &Characteristic) -> bluer::Result<()> { println!(" Characteristic flags : {:?}, ", char.flags().await?); - 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]); - } + let mut strip_red : Strip<10>; + strip_red.strip.fill(PixelColor { red: 255, green: 0, blue: 0}); + + let mut strip_green: Strip<10>; + strip_green.strip.fill(PixelColor { red: 0, green: 255, blue: 0}); loop { - char.write(&*base_data).await?; + char.write(&*strip_green.to_array()).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; - } } @@ -112,7 +113,9 @@ async fn find_neopixel_service(device: &Device) -> bluer::Result { + pub strip: [PixelColor; N], +} + +impl Strip { + pub fn to_array(&self) -> Vec { + let mut data : Vec = 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(data: Strip, char: Characteristic) -> bluer::Result<()> { + let mut frame= BASE_STRIP_DATA.to_vec(); + frame.append(&mut data.to_array()); + char.write(&*frame).await?; + Ok(()) +}