/
EmotivInsight.h
146 lines (108 loc) · 3.63 KB
/
EmotivInsight.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* EmotivInsight.h
*
* Implements data collection interface for Emotiv Insight device.
*
* Created on: 4 Nov 2016
* Author: Tomas Ukkonen
*
*/
#ifndef EMOTIVINSIGHT_H_
#define EMOTIVINSIGHT_H_
#include <vector>
#include <pthread.h>
#include <time.h>
#include "DataSource.h"
#include "Iedk.h" // add -Iemotiv_insight/include and -Lemotiv_insight
namespace whiteice {
namespace resonanz {
class EmotivInsight: public DataSource {
public:
EmotivInsight();
virtual ~EmotivInsight();
/*
* Returns unique DataSource name
*/
std::string getDataSourceName() const;
/**
* Returns true if connection and data collection to device is currently working.
*/
bool connectionOk() const;
/**
* returns data between t0..(t1-1) samples (total of t1-t0 samples)
* returns false if there is no data or if t0 > current time.
*
* negative values t0 means taking data from the end of the buffer [more recent values]:
* x[size(x)-abs(t0) .. size(x)-abs(t1)]
*
* if t1 is negative data is retrieved all the way to the end of the buffer
*
* data is in format x[t][signal number];
*/
bool data(std::vector< std::vector<float> >& x, int t0 = 0, int t1 = 0) const;
/**
* returns most recent data vector
*/
bool data(std::vector<float>& x) const;
/**
* returns estimated variance of measured data.
*/
bool variance(std::vector< std::vector<float> >& v, int t0 = 0, int t1 = 0) const;
/**
* returns variance vector for most recent measurements
*/
bool variance(std::vector<float>& v) const;
/**
* return number of 1d-signals in data
*/
unsigned int getNumberOfSignals() const;
/**
* return signal name for the given signal number
*/
std::string getSignalName(int index) const;
bool getSignalNames(std::vector<std::string>& names) const;
bool extradata(std::vector<float>& x) const { x.resize(0); return true; }
unsigned int getExtraNumberOfSignals() const { return 0; }
/**
* Returns amount of data collected [collection frequency is 1 Hz]
*/
int getDataLength() const;
/**
* Removes already collected data.
*/
bool clearData();
protected:
double current_time() const;
// collection frequency [1 Hz]
static constexpr double time_frequency = 1.0;
private:
void update_tick(double t0, double t1); // update time progression and adds fresh data to be used
std::vector<float> latest_value;
std::vector<float> latest_variance;
std::vector< std::vector<float> > values;
std::vector< std::vector<float> > variances;
mutable pthread_mutex_t data_lock;
static const unsigned int NUMBER_OF_SIGNALS = 5;
///////////////////////////////////////////////////////////////////////////////////////
void new_data(float t, std::vector<float>& data); // records new data since last update_tick() call
std::vector<float> times;
float buffer_min_time, buffer_max_time;
std::vector< std::vector<float> > samples;
static const unsigned int MAX_BUFFER_SIZE = 1000; // maximum number of samples to be stored after which we ignore the rest
double latest_data_received_t;
////////////////////////////////////////////////////////////////////////////////////////
void poll_events();
int connection;
EmoEngineEventHandle eEvent;
EmoStateHandle eState;
bool keep_polling;
pthread_mutex_t emotiv_lock;
pthread_t poll_thread;
friend void* __emotiv_insight_start_poll_thread(void*);
// helper function..
float calculateScaledScore(const double& rawScore, const double& maxScale,
const double& minScale);
};
}
}
#endif /* EMOTIVINSIGHT_H_ */