Compare commits
3 Commits
d7f7dede49
...
142fdb44d7
Author | SHA1 | Date | |
---|---|---|---|
|
142fdb44d7 | ||
|
1098b7ba6b | ||
|
17866b6670 |
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -9,3 +9,4 @@ target/
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
|
*.iml
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
use std::collections::HashSet;
|
use bluer::gatt::remote::Characteristic;
|
||||||
use std::sync::mpsc::{channel, Sender};
|
|
||||||
use std::time::Duration;
|
|
||||||
use bluer::{Adapter, AdapterEvent, Address, Device, Result, Uuid};
|
use bluer::{Adapter, AdapterEvent, Address, Device, Result, Uuid};
|
||||||
use futures::{pin_mut, StreamExt};
|
use futures::{pin_mut, StreamExt};
|
||||||
use tokio::task::spawn_blocking;
|
use std::collections::HashSet;
|
||||||
|
use std::time::Duration;
|
||||||
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");
|
||||||
|
@ -22,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)
|
||||||
}
|
}
|
||||||
|
@ -38,7 +35,6 @@ async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid)
|
||||||
while let Some(evt) = discover.next().await {
|
while let Some(evt) = discover.next().await {
|
||||||
match evt {
|
match evt {
|
||||||
AdapterEvent::DeviceAdded(addr) => {
|
AdapterEvent::DeviceAdded(addr) => {
|
||||||
println!("device adress {}", addr);
|
|
||||||
if already_scanned.contains(&addr) {
|
if already_scanned.contains(&addr) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -47,36 +43,28 @@ async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid)
|
||||||
match has_service(&device, &uuid).await {
|
match has_service(&device, &uuid).await {
|
||||||
Ok(service_found) => {
|
Ok(service_found) => {
|
||||||
if service_found {
|
if service_found {
|
||||||
tx.send(device).await;
|
tx.send(device).await.unwrap();
|
||||||
println!("found service in device {}", addr);
|
println!("found service in device {}", addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => { continue }
|
Err(_) => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("ended");
|
|
||||||
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");
|
|
||||||
let (tx, mut rx) = mpsc::channel(4);
|
let (tx, mut rx) = mpsc::channel(4);
|
||||||
;
|
|
||||||
let timeout = tokio::time::timeout(Duration::from_secs(30), bluetooth_scan(tx, adapter, uuid)).await;
|
let timeout = tokio::time::timeout(Duration::from_secs(30), bluetooth_scan(tx, adapter, uuid));
|
||||||
let scan = tokio::spawn(timeout);
|
let scan = tokio::spawn(timeout);
|
||||||
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());
|
|
||||||
devices.push(device);
|
devices.push(device);
|
||||||
println!("len {}", devices.len());
|
|
||||||
if devices.len() >= 2 {
|
if devices.len() >= 2 {
|
||||||
println!("drop");
|
|
||||||
drop(scan);
|
drop(scan);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -84,3 +72,39 @@ pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec<Device> {
|
||||||
|
|
||||||
devices
|
devices
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> {
|
||||||
|
if device.is_connected().await? {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
for i in 0..retries {
|
||||||
|
match device.connect().await {
|
||||||
|
Ok(()) => return Ok(()),
|
||||||
|
Err(_) => {
|
||||||
|
println!("connection error ({}), retry…", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(bluer::Error {
|
||||||
|
kind: bluer::ErrorKind::ConnectionAttemptFailed,
|
||||||
|
message: "all attempts fail".parse().unwrap(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_char(
|
||||||
|
device: &Device,
|
||||||
|
service_uuid: Uuid,
|
||||||
|
char_uuid: Uuid,
|
||||||
|
) -> Result<Option<Characteristic>> {
|
||||||
|
for service in device.services().await? {
|
||||||
|
if service_uuid == service.uuid().await? {
|
||||||
|
for char in service.characteristics().await? {
|
||||||
|
if char_uuid == char.uuid().await? {
|
||||||
|
return Ok(Some(char));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
91
src/main.rs
91
src/main.rs
|
@ -1,35 +1,67 @@
|
||||||
mod patterns;
|
|
||||||
mod bluetooth;
|
mod bluetooth;
|
||||||
|
mod patterns;
|
||||||
|
|
||||||
use patterns::{PixelColor, Strip};
|
use std::convert::TryFrom;
|
||||||
use bluer::Device;
|
use std::thread::sleep;
|
||||||
use bluer::gatt::remote::Characteristic;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::time::sleep;
|
use bluer::gatt::remote::Characteristic;
|
||||||
use crate::patterns::RainbowPattern;
|
use patterns::Strip;
|
||||||
|
use tokio::sync::watch;
|
||||||
|
use crate::patterns::PixelColor;
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
for device in devices {
|
let mut pixels = Vec::<PixelColor>::new();
|
||||||
println!("{:?}", device);
|
|
||||||
|
for _ in 0..STRIP_SIZE{
|
||||||
|
pixels.push(PixelColor{
|
||||||
|
red: 255, green: 0, blue:0
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let strip = Strip::<STRIP_SIZE> {
|
||||||
|
strip : <[PixelColor; 25]>::try_from(pixels).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
Ok(())
|
let (tx, rx) = watch::channel(Strip::<STRIP_SIZE>::default());
|
||||||
|
|
||||||
|
|
||||||
|
for device in devices {
|
||||||
|
bluetooth::connect(&device, 3).await?;
|
||||||
|
let char = bluetooth::get_char(&device, SERVICE_UUID, PIXEL_DATA_UUID).await?;
|
||||||
|
if let Some(char) = char {
|
||||||
|
let mut rx_device = rx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
println!("device connected : {}", &device.address());
|
||||||
|
while rx_device.changed().await.is_ok() {
|
||||||
|
println!("ok");
|
||||||
|
let strip = rx_device.borrow().clone();
|
||||||
|
write_strip(&strip, &char).await.unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
|
||||||
|
tx.send(strip);
|
||||||
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
|
tx.send(Strip::<STRIP_SIZE>::default());
|
||||||
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fn create_pattern_list() -> Vec<patterns>
|
||||||
// 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?);
|
||||||
//
|
//
|
||||||
|
@ -50,32 +82,15 @@ 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>(
|
||||||
// let frame = [BASE_STRIP_DATA.to_vec(), data.to_array()].concat();
|
data: &Strip<N>,
|
||||||
// print!("{:?}",frame);
|
char: &Characteristic,
|
||||||
// char.write(&*frame).await?;
|
) -> bluer::Result<()> {
|
||||||
// Ok(())
|
let frame = [BASE_STRIP_DATA.to_vec(), data.to_array()].concat();
|
||||||
// }
|
print!("{:?}", frame);
|
||||||
|
char.write(&*frame).await?;
|
||||||
// async fn connect(device: &Device, retries: u8) -> bluer::Result<()> {
|
Ok(())
|
||||||
// if device.is_connected().await? {
|
}
|
||||||
// return Ok(())
|
|
||||||
// }
|
|
||||||
// for i in 0..retries {
|
|
||||||
// match device.connect().await {
|
|
||||||
// Ok(()) => return Ok(()),
|
|
||||||
// Err(_) => {
|
|
||||||
// println!("connection error ({}), retry…", i);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Err(bluer::Error {
|
|
||||||
// kind: bluer::ErrorKind::ConnectionAttemptFailed,
|
|
||||||
// message: "all attempts fail".parse().unwrap(),
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
106
src/patterns.rs
106
src/patterns.rs
|
@ -1,33 +1,113 @@
|
||||||
use bluer::gatt::remote::Characteristic;
|
use std::fmt;
|
||||||
|
|
||||||
const BASE_STRIP_DATA : [u8; 5] = [0x0, 0x0, 0x0, 0x0, 0x1];
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct PixelColor {
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub(crate) struct PixelColor {
|
|
||||||
pub(crate) red: u8,
|
pub(crate) red: u8,
|
||||||
pub(crate) green: u8,
|
pub(crate) green: u8,
|
||||||
pub(crate) blue: u8,
|
pub(crate) blue: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PixelColor {
|
||||||
|
fn new() -> Self {
|
||||||
|
Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct Strip<const N: usize> {
|
impl fmt::Display for PixelColor {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "[{},{},{}]", self.red, self.green, self.blue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Default for PixelColor {
|
||||||
|
fn default() -> Self {
|
||||||
|
PixelColor {
|
||||||
|
red: 0,
|
||||||
|
green: 0,
|
||||||
|
blue: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct Strip<const N: usize> {
|
||||||
pub strip: [PixelColor; N],
|
pub strip: [PixelColor; N],
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn write_strip<const N: usize>(data: Strip<N>, char: Characteristic) -> bluer::Result<()> {
|
impl<const N: usize> Default for Strip<N> {
|
||||||
let mut frame= BASE_STRIP_DATA.to_vec();
|
fn default() -> Self {
|
||||||
frame.append(&mut data.to_array());
|
Strip {
|
||||||
char.write(&*frame).await?;
|
strip: [PixelColor::new(); N],
|
||||||
Ok(())
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RainbowPattern<const N: usize> {
|
||||||
|
pub(crate) current_iteration: usize,
|
||||||
|
pub(crate) max_iteration: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Iterator for RainbowPattern<N> {
|
||||||
|
type Item = Strip<N>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if let Some(nbr_iteration) = self.max_iteration {
|
||||||
|
if nbr_iteration == self.current_iteration {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut strip = Strip::default();
|
||||||
|
let step = 255 / N;
|
||||||
|
for i in 0..N {
|
||||||
|
let pos = (i * step + self.current_iteration) as u8;
|
||||||
|
strip.strip[i] = wheel(pos)
|
||||||
|
}
|
||||||
|
self.current_iteration = self.current_iteration + 1;
|
||||||
|
Some(strip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wheel(index: u8) -> PixelColor {
|
||||||
|
let pos = 255 - index;
|
||||||
|
match pos {
|
||||||
|
0..=85 => PixelColor {
|
||||||
|
red: 255 - (pos * 3),
|
||||||
|
green: 0,
|
||||||
|
blue: pos * 3,
|
||||||
|
},
|
||||||
|
86..=170 => {
|
||||||
|
let pos = pos - 85;
|
||||||
|
PixelColor {
|
||||||
|
red: 0,
|
||||||
|
green: pos * 3,
|
||||||
|
blue: 255 - (pos * 3),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let pos = pos - 170;
|
||||||
|
return PixelColor {
|
||||||
|
red: pos * 3,
|
||||||
|
green: 255 - (pos * 3),
|
||||||
|
blue: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user