Skip to content

Commit

Permalink
feat: UI for calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed May 29, 2024
1 parent eb2582b commit bfa5ab1
Show file tree
Hide file tree
Showing 20 changed files with 896 additions and 193 deletions.
146 changes: 98 additions & 48 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ tracing-subscriber = "0.3.17"
ratatui = "0.26.3"
uuid = "1.3.2"
regex = "1.10.4"
btleplug = { package = "btleplug-goose-fixed", version = "0.11.5" }
btleplug = { version = "0.11.5" }
strum = "0.26.2"

[target.'cfg(target_os = "linux")'.dependencies]
dbus = { version = "0.9.7", features = ["vendored"] }
Expand Down
42 changes: 42 additions & 0 deletions cli/src/ble_actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::{error::Error, str::FromStr};

use crate::bluetooth::Connection;
use crate::config::*;
use btleplug::api::Peripheral;
use tokio::sync::mpsc;
use uuid::Uuid;

pub enum BleAction {
CalibrateCo2,
CalibrateTemperature(i32),
Stop,
}

pub async fn run_ble_mpsc<TPeripheral: Peripheral>(
connection: &Connection<TPeripheral>,
mut ble_action_receiver: mpsc::Receiver<BleAction>,
) -> Result<(), Box<dyn Error>> {
while let Some(action) = ble_action_receiver.recv().await {
match action {
BleAction::CalibrateCo2 => {
tracing::info!("Calibrating CO2 sensor");
connection
.write_to_sennsor(
"what the fuck".as_bytes(),
Uuid::from_str(&BLE_MAIN_SENSOR_CO2_CALIBRATION_CHAR).unwrap(),
)
.await?
}
BleAction::CalibrateTemperature(_temperature) => {
tracing::info!("Calibrating temperature sensor");
}
BleAction::Stop => {
connection.disconnect().await?;
tracing::info!("Stopping BLE actions");
break;
}
}
}

Ok(())
}
22 changes: 19 additions & 3 deletions cli/src/bluetooth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use btleplug::api::{Central, CharPropFlags, Manager as _, Peripheral, ScanFilter};
use btleplug::api::{Central, CharPropFlags, Manager as _, Peripheral, ScanFilter, WriteType};
use btleplug::platform::Manager;
use futures::StreamExt;
use std::error::Error;
Expand Down Expand Up @@ -77,15 +77,15 @@ impl<TPer: Peripheral> Connection<TPer> {
}

pub async fn subscribe<TData: FromBleData, TFun: FnMut(TData)>(
&mut self,
&self,
char_uuid: Uuid,
mut fun: TFun,
) -> Result<(), Box<dyn Error>> {
tracing::debug!("Subscribing to sensor");

let characteristic = self.try_find_characteristic(char_uuid, CharPropFlags::NOTIFY)?;
self.peripheral.subscribe(&characteristic).await?;
self.subscribed_characteristic = Some(characteristic);
//self.subscribed_characteristic = Some(characteristic);

let mut notification_stream = self.peripheral.notifications().await?;

Expand Down Expand Up @@ -117,6 +117,21 @@ impl<TPer: Peripheral> Connection<TPer> {
let characteristic = self.try_find_characteristic(char_uuid, CharPropFlags::READ)?;
TData::from_bytes(self.peripheral.read(&characteristic).await?)
}

pub async fn write_to_sennsor(
&self,
data: &[u8],
char_uuid: Uuid,
) -> Result<(), Box<dyn Error>> {
tracing::debug!("Writing to sensor");

let characteristic = self.try_find_characteristic(char_uuid, CharPropFlags::WRITE)?;
self.peripheral
.write(&characteristic, data, WriteType::WithResponse)
.await?;

Ok(())
}
}

pub async fn connect_to(
Expand Down Expand Up @@ -184,6 +199,7 @@ pub async fn connect_to(
.iter()
.any(|service| service.uuid == service_uuid)
{
adapter.stop_scan().await?;
return Ok(Connection {
peripheral,
subscribed_characteristic: None,
Expand Down
7 changes: 5 additions & 2 deletions cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ lazy_static! {
pub static ref BLE_MAIN_SENSOR_STREAM_CHAR: &'static str =
safe_c_str_to_string(raw_bindings::BLE_MAIN_SENSOR_STREAM_CHAR)
.expect("Invalid UTF-8 for BLE_MAIN_SENSOR_STREAM_CHAR");
pub static ref BLE_MAIN_SENSOR_ACTION_CHAR: &'static str =
safe_c_str_to_string(raw_bindings::BLE_MAIN_SENSOR_ACTION_CHAR)
pub static ref BLE_MAIN_SENSOR_CO2_CALIBRATION_CHAR: &'static str =
safe_c_str_to_string(raw_bindings::BLE_MAIN_SENSOR_CO2_CALIBRATION_CHAR)
.expect("Invalid UTF-8 for BLE_MAIN_SENSOR_STREAM_CHAR");
pub static ref BLE_MAIN_SENSOR_TEMP_CALIBRATION_CHAR: &'static str =
safe_c_str_to_string(raw_bindings::BLE_MAIN_SENSOR_TEMP_CALIBRATION_CHAR)
.expect("Invalid UTF-8 for BLE_MAIN_SENSOR_ACTION_CHAR");
pub static ref BLE_MAIN_SENSOR_CALIBRATE_CO2: &'static str =
safe_c_str_to_string(raw_bindings::BLE_MAIN_SENSOR_CALIBRATE_CO2)
Expand Down
7 changes: 6 additions & 1 deletion cli/src/config/raw_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
pub const BLE_MAIN_SERVICE_LOCAL_NAME: &[u8; 9] = b"co2nsole\0";
pub const BLE_MAIN_SENSOR_SERVICE: &[u8; 37] = b"0000FFE0-0000-1000-8000-00805F9B34FB\0";
pub const BLE_MAIN_SENSOR_STREAM_CHAR: &[u8; 37] = b"0000FFE0-0000-1000-8000-00805F9B34FB\0";
pub const BLE_MAIN_SENSOR_ACTION_CHAR: &[u8; 37] = b"beb5483e-36e1-4688-b7f5-ea07361b26a8\0";
pub const BLE_MAIN_SENSOR_CO2_CALIBRATION_CHAR: &[u8; 37] =
b"beb5483e-36e1-4688-b7f5-ea07361b26a8\0";
pub const BLE_MAIN_SENSOR_TEMP_CALIBRATION_CHAR: &[u8; 37] =
b"d753f24d-3aa0-4678-b039-a52d3b2e3946\0";
pub const BLE_MAIN_SENSOR_CALIBRATE_CO2: &[u8; 15] = b"CalibrateMhZ19\0";
pub const BLE_MAIN_SENSOR_CALIBRATE_TEMPERATURE: &[u8; 21] = b"CalibrateTemperature\0";
pub const BLE_MAIN_SENSOR_CALIBRATE_HUMIDITY: &[u8; 18] = b"CalibrateHumidity\0";
pub const BLE_WINDOW_SERVICE_LOCAL_NAME: &[u8; 16] = b"co2nsole window\0";
pub const BLE_WINDOW_SENSOR_SERVICE: &[u8; 37] = b"19B10000-E8F2-537E-4F6C-D104768A1214\0";
pub const BLE_WINDOW_SENSOR_READ_CHAR: &[u8; 37] = b"a3b27688-3b9e-4214-970c-3db5f14c5d2b\0";
pub const CALIBRATION_CO2_DEFAULT: u32 = 400;
pub const CALIBRATION_TEMPERATURE_ADJUST: i32 = -8;
Loading

0 comments on commit bfa5ab1

Please sign in to comment.