-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
194 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
[package] | ||
name = "handler" | ||
version = "0.1.0" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
edition.repository = true | ||
description = "Data handler code, calculations, resistance check" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
lib = { version = "0.1.0", path = "../brainbit" } | ||
lib = { path = "../brainbit" } | ||
tracing.workspace = true | ||
tracing-subscriber.workspace = true | ||
color-eyre.workspace = true | ||
chrono.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} | ||
pub mod main_handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use chrono::Utc; | ||
use lib::bbit::resist::ResistState; | ||
use lib::bbit::responses::{DeviceStatusData, Nss2Status}; | ||
use lib::bbit::EventHandler; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::sync::{Arc, Mutex}; | ||
use tracing::instrument; | ||
|
||
const SKIP_FIRST_RESIST_RECORDS_NUMBER: u8 = 20; | ||
const STORE_RESIST_RECORDS_NUMBER: u8 = 20; | ||
|
||
#[derive(Debug)] | ||
pub struct BBitHandler { | ||
counter: Arc<AtomicUsize>, | ||
device_status: Mutex<DeviceStatusData>, | ||
output: Mutex<File>, | ||
skipped_resist_records_number: AtomicUsize, // AtomicUsize = AtomicUsize::new(0); | ||
current_chanel_number_resist_measure: AtomicUsize, | ||
resist_measure_records: Vec<u8>, | ||
resist_results: Mutex<ResistState>, | ||
} | ||
|
||
#[lib::async_trait] | ||
impl EventHandler for BBitHandler { | ||
#[instrument(skip(self))] | ||
async fn device_status_update(&self, status_data: DeviceStatusData) { | ||
let time = Utc::now(); | ||
let formatted: String = time.to_rfc3339_opts(chrono::SecondsFormat::Secs, true); | ||
// formatted = formatted.replace("\'", ""); | ||
let msg = format!("{formatted:?} - {status_data}\n"); | ||
tracing::debug!(msg); | ||
{ | ||
// write eeg data to file | ||
let mut lock = self.output.lock().unwrap(); | ||
lock.write_all(msg.as_bytes()).unwrap(); | ||
} | ||
{ | ||
// read and update local Device Status | ||
let mut lock = self.device_status.lock().unwrap(); | ||
lock.status_nss2 = status_data.status_nss2; | ||
lock.battery_level = status_data.battery_level; | ||
lock.cmd_error = status_data.cmd_error; | ||
} | ||
self.counter.fetch_add(1, Ordering::SeqCst); | ||
} | ||
|
||
#[instrument(skip_all)] | ||
async fn eeg_update(self: &mut BBitHandler, eeg_data: Vec<u8>) { | ||
let time = Utc::now(); | ||
let mut formatted: String = time.to_rfc3339_opts(chrono::SecondsFormat::Secs, true); | ||
formatted = formatted.replace("\'", ""); | ||
// let msg = format!("{formatted:?} - EEG={:>3?}\n", eeg_data); | ||
let msg = format!("{:>3?}\n", eeg_data); | ||
{ | ||
let mut lock = self.output.lock().unwrap(); | ||
lock.write_all(msg.as_bytes()).expect("Can't write log..."); | ||
} | ||
let nss2status = self.device_status.lock().unwrap().status_nss2; | ||
match nss2status { | ||
Nss2Status::ResistTransmission => { | ||
tracing::debug!(msg); | ||
let skipped_number = self.skipped_resist_records_number.load(Ordering::Relaxed); | ||
if skipped_number > 0 { | ||
// skip 'SKIP_FIRST_RESIST_RECORDS_NUMBER' records | ||
tracing::debug!("Skipping = {:?}", skipped_number); | ||
self.decrease_skipped_resist_records_number(); | ||
return; | ||
} | ||
let gathered_records_number = self.get_resist_measure_records_len(); | ||
if gathered_records_number >= STORE_RESIST_RECORDS_NUMBER as usize { | ||
tracing::debug!( | ||
"Gathered = {:?} records for ch='{}'", | ||
gathered_records_number, | ||
self.current_chanel_number_resist_measure | ||
.load(Ordering::Relaxed) | ||
); | ||
// got to next channel | ||
} | ||
} | ||
Nss2Status::EegTransmission => { | ||
tracing::debug!(msg); | ||
} | ||
Nss2Status::Stopped => { | ||
tracing::debug!("Stopped device in main"); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
impl BBitHandler { | ||
pub async fn new(counter: Arc<AtomicUsize>) -> color_eyre::Result<Self> { | ||
Ok(Self { | ||
counter, | ||
device_status: Mutex::new(DeviceStatusData::default()), | ||
output: Mutex::new(File::create(format!("main_app_output.txt"))?), | ||
skipped_resist_records_number: AtomicUsize::new( | ||
SKIP_FIRST_RESIST_RECORDS_NUMBER as usize, | ||
), | ||
current_chanel_number_resist_measure: AtomicUsize::new(0), | ||
resist_measure_records: Vec::with_capacity(STORE_RESIST_RECORDS_NUMBER as usize), | ||
resist_results: Mutex::new(ResistState::default()), | ||
}) | ||
} | ||
|
||
fn decrease_skipped_resist_records_number(&mut self) { | ||
self.current_chanel_number_resist_measure | ||
.fetch_sub(1, Ordering::Relaxed); | ||
} | ||
|
||
fn push_resist_data(&mut self, new_data: u8) { | ||
self.resist_measure_records.push(new_data); | ||
} | ||
|
||
fn get_resist_measure_records_len(&self) -> usize { | ||
self.resist_measure_records.len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.