DataPacket

Struct DataPacket 

Source
pub struct DataPacket {
    pub host_timestamp: f64,
    pub emotibit_timestamp: f64,
    pub packet_id: u32,
    pub data_points: u8,
    pub version: u8,
    pub reliability: u8,
    pub data_type: DataType,
}
Expand description

Emotibit Data Packet

Fields§

§host_timestamp: f64

Local timestamp on a host PC

§emotibit_timestamp: f64

Milliseconds since start of EmotiBit

§packet_id: u32

Packet count since start of EmotiBit

§data_points: u8

Number of data points in the payload

§version: u8

Version of packet protocol

§reliability: u8

Data reliability score out of 100, currently always 100

§data_type: DataType

Type of data being sent and its payload

Implementations§

Source§

impl DataPacket

Source

pub fn inject_host_timestamp(self, map: &TimeSyncMap) -> Self

Performs linear interpolation based on TimeSyncMap and returns a new DataPacket with a host timestamp.

Examples found in repository?
examples/to_csv.rs (line 94)
13fn read_write(path_buf: Option<PathBuf>) -> Result<()> {
14    let filename: &str = path_buf
15        .as_ref()
16        .and_then(|name| name.file_stem())
17        .and_then(|name| name.to_str())
18        .unwrap_or("default");
19
20    let (datapackets, errors): (Vec<_>, Vec<_>) = parser::get_packets(
21        &path_buf
22            .as_ref()
23            .unwrap()
24            .clone()
25            .into_os_string()
26            .into_string()
27            .unwrap(),
28    )?
29    .into_iter()
30    .partition(Result::is_ok);
31
32    let mut output_file = path_buf.clone().unwrap();
33    output_file.set_file_name(format!("{}_ERROR.csv", filename));
34
35    // Write Errors
36    let mut output = File::create(output_file.clone())?;
37    for err in errors.into_iter().filter_map(|result| result.err()) {
38        writeln!(output, "{}", err)?;
39    }
40
41    // Write TimeSyncs
42    output_file.set_file_name(format!("{}_timesyncs.csv", filename));
43    let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
44    match parser::find_syncs(&datapackets) {
45        Ok(syncs) => {
46            let header =
47                StringRecord::from(vec!["RD", "TS_received", "TS_sent", "AK", "RoundTrip"]);
48            writer.write(&header)?;
49            for packet in syncs {
50                writer.write(&packet)?;
51            }
52        }
53        Err(e) => {
54            writer.write(&StringRecord::from(vec![format!("{:?}", e)]))?;
55        }
56    }
57
58    // Extract TypeTags
59    let set: HashSet<&str> = HashSet::from_iter(
60        datapackets
61            .iter()
62            .map(|result| result.as_ref().unwrap().data_type.as_str()),
63    );
64
65    // Write TimeSyncsMap
66    output_file.set_file_name(format!("{}_timeSyncMap.csv", filename));
67    let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
68    let syncmap = parser::generate_sync_map(&datapackets);
69    match &syncmap {
70        Ok(map) => {
71            let header = StringRecord::from(vec![
72                "TE0",
73                "TE1",
74                "TL0",
75                "TL1",
76                "TimeSyncsReceived",
77                "EmotiBitStartTime",
78                "EmotiBitEndTime",
79                "DataParserVersion",
80            ]);
81            writer.write(&header)?;
82            writer.write(map)?;
83        }
84        Err(e) => {
85            writer.write(&StringRecord::from(vec![format!("{:?}", e)]))?;
86        }
87    }
88
89    // Write Packets
90    let packets: Vec<DataPacket> = match syncmap {
91        Ok(map) => datapackets
92            .into_iter()
93            .filter_map(|result| result.ok())
94            .map(|p| p.inject_host_timestamp(&map))
95            .collect(),
96        Err(_) => datapackets
97            .into_iter()
98            .filter_map(|result| result.ok())
99            .collect(),
100    };
101
102    for t in set.iter() {
103        output_file.set_file_name(format!("{}_{}.csv", filename, t));
104        let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
105        let header = StringRecord::from(vec![
106            "LocalTimestamp",
107            "EmotiBitTimestamp",
108            "PacketNumber",
109            "DataLength",
110            "TypeTag",
111            "ProtocolVersion",
112            "DataReliability",
113            t,
114        ]);
115        writer.write(&header)?;
116        for packet in packets.iter().filter(|x| x.data_type.as_str() == *t) {
117            writer.write(packet)?;
118        }
119    }
120
121    Ok(())
122}

Trait Implementations§

Source§

impl Clone for DataPacket

Source§

fn clone(&self) -> DataPacket

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Csv for DataPacket

Source§

impl Debug for DataPacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl TryFrom<&StringRecord> for DataPacket

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(r: &StringRecord) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&str> for DataPacket

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(str: &str) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.