CEBL  2.1
DataSource.cpp
Go to the documentation of this file.
1 #include "DataSource.hpp"
2 #include "../CEBLModel.hpp"
3 #include "EEGDataStream.hpp"
4 #include "RandomDataStream.hpp"
5 #include "FileDataStream.hpp"
6 #include "MindsetStream.hpp"
7 
8 //----------------------------------------------------------------------
9 // CONSTRUCTORS / DESTRUCTORS
10 
12 {
13  this->model = model;
14 
15  source_names.push_back("Mindset");
16  source_names.push_back("Random Numbers");
17  source_names.push_back("Random EEG");
18  source_names.push_back("File");
19  selected_source = 0;
20  active_stream_type = 0;
21  data_store_flag = false;
22  data_stream = NULL;
23  data_buffer.clear();
24 }
25 
27 {
28  if(data_stream != NULL)
29  delete data_stream;
30 }
31 
32 
33 
34 //----------------------------------------------------------------------
35 // GETTING OPERATIONS
36 
37 
39 // also stores the results into buffer if data store
40 // flag is set
42 {
43  if(!sourceReady())
44  throw DataException("Selected data source is not ready.");
45  EEGData ret = data_stream->read(samples);
46 
47  // save into buffer
48  if(this->data_store_flag)
49  {
50  this->data_buffer.append(ret);
51  }
52  return ret;
53 }
54 
56 // also stores the results into buffer if data store
57 // flag is set
59 {
60  if(!sourceReady())
61  throw DataException("Selected data source is not ready.");
62  EEGData ret = data_stream->readAll();
63  // save into buffer
64  if(this->data_store_flag)
65  {
66  this->data_buffer.append(ret);
67  }
68 
69  return ret;
70 }
71 
73 {
74  if(!sourceReady())
75  throw DataException("Selected data source is not ready.");
76  return data_stream->getSamplesAvailable();
77 }
78 
80 {
81  if(data_stream == NULL || !data_stream->isStarted())
82  return false;
83  else
84  return true;
85 }
86 
88 {
89  if(data_stream != NULL)
90  return data_stream->isStarted();
91  else
92  return false;
93 }
94 
96 {
97  return this->data_store_flag;
98 }
99 
101 {
102  return this->data_buffer.numSamples();
103 }
105 {
106  return this->data_buffer;
107 }
108 
109 //----------------------------------------------------------------------
110 // SETTING OPERATIONS
111 
113 {
114  this->data_buffer.clear();
115 }
116 
118 {
119  this->data_store_flag = flag;
120 }
121 
122 void DataSource::setSource(int source)
123 {
124  this->selected_source = source;
125 }
126 
127 void DataSource::setSource(string source)
128 {
129  for(unsigned int i=0; i<source_names.size();i++)
130  {
131  if(source_names[i]==source)
132  {
133  selected_source = i;
134  break;
135  }
136  }
137 
138 }
139 
141 {
142  if(data_stream != NULL)
143  data_stream->readAll();
144 }
145 
146 void DataSource::createDataStream()
147 {
148  if(this->data_stream != NULL)
149  {
150  delete this->data_stream;
151  this->data_stream = NULL;
152  }
153 
154  switch(this->selected_source)
155  {
156  case 0:
157  this->data_stream = new MindsetStream(this->model);
158  break;
159  case 1:
160  this->data_stream = new RandomDataStream();
161  break;
162  case 2:
163  this->data_stream = new RandomDataStream();
164  break;
165  case 3:
166  this->data_stream = new FileDataStream(this->model);
167  break;
168 
169  }
170  this->active_stream_type = selected_source;
171 }
172 
174 {
175  // create data stream if it has not been created or is of the wrong type
176  if(data_stream == NULL || active_stream_type != selected_source)
177  createDataStream();
178  if(data_stream != NULL)
179  data_stream->start();
180  else
181  cerr << "DataSource: Illegal source selected.\n";
182 }
183 
185 {
186  if(data_stream != NULL)
187  {
188  data_stream->stop();
189  }
190 }