rust fmt
This commit is contained in:
parent
17866b6670
commit
1098b7ba6b
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -9,3 +9,4 @@ target/
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
|
*.iml
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
|
use bluer::gatt::remote::Characteristic;
|
||||||
|
use bluer::{Adapter, AdapterEvent, Address, Device, Result, Uuid};
|
||||||
|
use futures::{pin_mut, StreamExt};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use bluer::{Adapter, AdapterEvent, Address, Device, Result, Uuid};
|
|
||||||
use bluer::gatt::remote::Characteristic;
|
|
||||||
use futures::{pin_mut, StreamExt};
|
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
// const CONNECT_RETRIES: u8 = 3;
|
// const CONNECT_RETRIES: u8 = 3;
|
||||||
// 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);
|
||||||
|
|
||||||
pub(crate) async fn create_session() -> Result<Adapter>
|
pub(crate) async fn create_session() -> Result<Adapter> {
|
||||||
{
|
|
||||||
let session = bluer::Session::new().await?;
|
let session = bluer::Session::new().await?;
|
||||||
let adapter_names = session.adapter_names().await?;
|
let adapter_names = session.adapter_names().await?;
|
||||||
let adapter_name = adapter_names.first().expect("no Bluetooth adapter found");
|
let adapter_name = adapter_names.first().expect("no Bluetooth adapter found");
|
||||||
|
@ -21,10 +20,9 @@ pub(crate) async fn create_session() -> Result<Adapter>
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn has_service(device: &Device, uuid: &Uuid) -> Result<bool> {
|
async fn has_service(device: &Device, uuid: &Uuid) -> Result<bool> {
|
||||||
|
|
||||||
let uuids = device.uuids().await?.unwrap_or_default();
|
let uuids = device.uuids().await?.unwrap_or_default();
|
||||||
if uuids.contains(uuid) {
|
if uuids.contains(uuid) {
|
||||||
return Ok(true)
|
return Ok(true);
|
||||||
}
|
}
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
@ -50,7 +48,7 @@ async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid)
|
||||||
println!("found service in device {}", addr);
|
println!("found service in device {}", addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => { continue }
|
Err(_) => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -61,7 +59,6 @@ async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid)
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec<Device> {
|
pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec<Device> {
|
||||||
println!("start scanning devices");
|
println!("start scanning devices");
|
||||||
let (tx, mut rx) = mpsc::channel(4);
|
let (tx, mut rx) = mpsc::channel(4);
|
||||||
|
@ -70,7 +67,7 @@ pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec<Device> {
|
||||||
let scan = tokio::spawn(timeout);
|
let scan = tokio::spawn(timeout);
|
||||||
println!("good");
|
println!("good");
|
||||||
let mut devices: Vec<Device> = Vec::new();
|
let mut devices: Vec<Device> = Vec::new();
|
||||||
while let Some(device)= rx.recv().await {
|
while let Some(device) = rx.recv().await {
|
||||||
println!("new device received {}", device.address());
|
println!("new device received {}", device.address());
|
||||||
devices.push(device);
|
devices.push(device);
|
||||||
println!("len {}", devices.len());
|
println!("len {}", devices.len());
|
||||||
|
@ -86,7 +83,7 @@ pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec<Device> {
|
||||||
|
|
||||||
pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> {
|
pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> {
|
||||||
if device.is_connected().await? {
|
if device.is_connected().await? {
|
||||||
return Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
for i in 0..retries {
|
for i in 0..retries {
|
||||||
match device.connect().await {
|
match device.connect().await {
|
||||||
|
@ -95,7 +92,6 @@ pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> {
|
||||||
println!("connection error ({}), retry…", i);
|
println!("connection error ({}), retry…", i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(bluer::Error {
|
Err(bluer::Error {
|
||||||
|
@ -104,18 +100,20 @@ pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_char(device: &Device, service_uuid: Uuid, char_uuid: Uuid) -> Result<Option<Characteristic>> {
|
pub async fn get_char(
|
||||||
|
device: &Device,
|
||||||
|
service_uuid: Uuid,
|
||||||
|
char_uuid: Uuid,
|
||||||
|
) -> Result<Option<Characteristic>> {
|
||||||
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?);
|
println!("uuid : {}", &char.uuid().await?);
|
||||||
if char_uuid == char.uuid().await? {
|
if char_uuid == char.uuid().await? {
|
||||||
|
return Ok(Some(char));
|
||||||
return Ok(Some(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
24
src/main.rs
24
src/main.rs
|
@ -1,19 +1,17 @@
|
||||||
mod patterns;
|
|
||||||
mod bluetooth;
|
mod bluetooth;
|
||||||
|
mod patterns;
|
||||||
|
|
||||||
use patterns::Strip;
|
|
||||||
use bluer::gatt::remote::Characteristic;
|
use bluer::gatt::remote::Characteristic;
|
||||||
|
use patterns::Strip;
|
||||||
use tokio::sync::watch;
|
use tokio::sync::watch;
|
||||||
|
|
||||||
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);
|
||||||
const STRIP_SIZE: usize = 25;
|
const STRIP_SIZE: usize = 25;
|
||||||
const BASE_STRIP_DATA : [u8; 3] = [0x00, 0x00, 0x01];
|
const BASE_STRIP_DATA: [u8; 3] = [0x00, 0x00, 0x01];
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() -> bluer::Result<()> {
|
async fn main() -> bluer::Result<()> {
|
||||||
|
|
||||||
let adapter = bluetooth::create_session().await?;
|
let adapter = bluetooth::create_session().await?;
|
||||||
|
|
||||||
let devices = bluetooth::scan_devices(adapter, SERVICE_UUID).await;
|
let devices = bluetooth::scan_devices(adapter, SERVICE_UUID).await;
|
||||||
|
@ -30,19 +28,16 @@ async fn main() -> bluer::Result<()> {
|
||||||
let strip = *rx.borrow();
|
let strip = *rx.borrow();
|
||||||
write_strip(&strip, &char).await;
|
write_strip(&strip, &char).await;
|
||||||
}
|
}
|
||||||
}).await;
|
})
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{:?}", device);
|
println!("{:?}", device);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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?);
|
||||||
//
|
//
|
||||||
|
@ -63,15 +58,16 @@ async fn main() -> bluer::Result<()> {
|
||||||
// Ok(())
|
// Ok(())
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub async fn write_strip<const N: usize>(data: &Strip<N>, char: &Characteristic) -> bluer::Result<()> {
|
pub async fn write_strip<const N: usize>(
|
||||||
|
data: &Strip<N>,
|
||||||
|
char: &Characteristic,
|
||||||
|
) -> bluer::Result<()> {
|
||||||
let frame = [BASE_STRIP_DATA.to_vec(), data.to_array()].concat();
|
let frame = [BASE_STRIP_DATA.to_vec(), data.to_array()].concat();
|
||||||
print!("{:?}",frame);
|
print!("{:?}", frame);
|
||||||
char.write(&*frame).await?;
|
char.write(&*frame).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// async fn find_neopixel_service(device: &Device) -> bluer::Result<Option<Characteristic>> {
|
// async fn find_neopixel_service(device: &Device) -> bluer::Result<Option<Characteristic>> {
|
||||||
// let addr = device.address();
|
// let addr = device.address();
|
||||||
// let uuids = device.uuids().await?.unwrap_or_default();
|
// let uuids = device.uuids().await?.unwrap_or_default();
|
||||||
|
|
|
@ -21,11 +21,14 @@ impl fmt::Display for PixelColor {
|
||||||
|
|
||||||
impl Default for PixelColor {
|
impl Default for PixelColor {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
PixelColor { red: 0, green: 0, blue: 0 }
|
PixelColor {
|
||||||
|
red: 0,
|
||||||
|
green: 0,
|
||||||
|
blue: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct Strip<const N: usize> {
|
pub struct Strip<const N: usize> {
|
||||||
pub strip: [PixelColor; N],
|
pub strip: [PixelColor; N],
|
||||||
|
@ -33,10 +36,14 @@ pub struct Strip<const N: usize> {
|
||||||
|
|
||||||
impl<const N: usize> Strip<N> {
|
impl<const N: usize> Strip<N> {
|
||||||
pub fn to_array(&self) -> Vec<u8> {
|
pub fn to_array(&self) -> Vec<u8> {
|
||||||
let mut data : Vec<u8> = vec![];
|
let mut data: Vec<u8> = vec![];
|
||||||
|
|
||||||
for i in 0..N {
|
for i in 0..N {
|
||||||
data.append(&mut vec![self.strip[i].red, self.strip[i].green, self.strip[i].blue]);
|
data.append(&mut vec![
|
||||||
|
self.strip[i].red,
|
||||||
|
self.strip[i].green,
|
||||||
|
self.strip[i].blue,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
|
@ -44,33 +51,33 @@ impl<const N: usize> Strip<N> {
|
||||||
|
|
||||||
impl<const N: usize> Default for Strip<N> {
|
impl<const N: usize> Default for Strip<N> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Strip { strip: [PixelColor::new(); N] }
|
Strip {
|
||||||
|
strip: [PixelColor::new(); N],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct RainbowPattern<const N: usize> {
|
pub struct RainbowPattern<const N: usize> {
|
||||||
pub(crate) current_iteration: usize,
|
pub(crate) current_iteration: usize,
|
||||||
pub(crate) max_iteration: Option<usize>
|
pub(crate) max_iteration: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<const N: usize> Iterator for RainbowPattern<N> {
|
impl<const N: usize> Iterator for RainbowPattern<N> {
|
||||||
type Item = Strip<N>;
|
type Item = Strip<N>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if let Some(nbr_iteration) = self.max_iteration {
|
if let Some(nbr_iteration) = self.max_iteration {
|
||||||
if nbr_iteration == self.current_iteration {
|
if nbr_iteration == self.current_iteration {
|
||||||
return None
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut strip = Strip::default();
|
let mut strip = Strip::default();
|
||||||
let step = 255 / N;
|
let step = 255 / N;
|
||||||
for i in 0..N {
|
for i in 0..N {
|
||||||
let pos = (i*step + self.current_iteration) as u8;
|
let pos = (i * step + self.current_iteration) as u8;
|
||||||
strip.strip[i] = wheel(pos)
|
strip.strip[i] = wheel(pos)
|
||||||
}
|
}
|
||||||
self.current_iteration = self.current_iteration +1 ;
|
self.current_iteration = self.current_iteration + 1;
|
||||||
Some(strip)
|
Some(strip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,20 +85,26 @@ impl<const N: usize> Iterator for RainbowPattern<N> {
|
||||||
fn wheel(index: u8) -> PixelColor {
|
fn wheel(index: u8) -> PixelColor {
|
||||||
let pos = 255 - index;
|
let pos = 255 - index;
|
||||||
match pos {
|
match pos {
|
||||||
0..=85 => PixelColor{
|
0..=85 => PixelColor {
|
||||||
red: 255 - (pos * 3),
|
red: 255 - (pos * 3),
|
||||||
green :0,
|
green: 0,
|
||||||
blue : pos * 3},
|
blue: pos * 3,
|
||||||
|
},
|
||||||
86..=170 => {
|
86..=170 => {
|
||||||
let pos = pos - 85;
|
let pos = pos - 85;
|
||||||
PixelColor{
|
PixelColor {
|
||||||
red: 0,
|
red: 0,
|
||||||
green :pos*3,
|
green: pos * 3,
|
||||||
blue : 255 - (pos * 3)}
|
blue: 255 - (pos * 3),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let pos = pos - 170;
|
let pos = pos - 170;
|
||||||
return PixelColor{red : pos*3, green : 255 - (pos*3), blue : 0};
|
return PixelColor {
|
||||||
|
red: pos * 3,
|
||||||
|
green: 255 - (pos * 3),
|
||||||
|
blue: 0,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user