CEBL  2.1
EEGDataStream.cpp
Go to the documentation of this file.
1 
8 #include "EEGDataStream.hpp"
9 #include "../Exceptions.hpp"
10 #include <cppR/cppR.hpp>
11 
12 
13 
14 using namespace cppR;
15 
16 //----------------------------------------------------------------------
17 // CONSTRUCTORS / DESTRUCTORS
18 
19 
21 {
22  setSampleRate(256);
23  //timeout length in milliseconds
24  timeout_length = 50;
25 }
26 
28 {
29 
30 }
31 
32 
33 //----------------------------------------------------------------------
34 
35 
37 {
38  if(getSamplesAvailable() < samples)
39  {
40  throw DataExceptionUnderflow("Fewer samples available than requested.");
41  }
42 
43  if(samples==getSamplesAvailable())
44  return readAll();
45  EEGData ret;
46  //lock the buffer
47  {
48  boost::mutex::scoped_lock lock(thread_lock);
49  ublas::matrix<double> data = buffer;
50  ret = EEGData(submatrix(data,0,0,0,samples));
51  buffer = EEGData(submatrix(data,0,0,samples+1,data.size2()-1));
52  }
53 
54  return ret;
55 
56 }
57 
59 {
60  EEGData ret;
61  //lock the buffer
62  {
63  boost::mutex::scoped_lock lock(thread_lock);
64  ret = buffer;
65  buffer.clear();
66  }
67 
68  return ret;
69 }
70 
71 //----------------------------------------------------------------------
72 // THREAD
73 
75 {
76  if(!isStarted())
77  {
78  //clear data
79  readAll();
80 
81  //run sublclass start method
82  onStart();
83 
84  timeoutStart();
85 
86  }
87 }
89 {
90  if(isStarted())
91  {
92  // let the thread finish
93  this->haltAndJoin();
94 
95  //clear data
96  readAll();
97 
98  //run sublclass stop method
99  try
100  {
101  onStop();
102  }
103  catch(...)
104  {
105  //don't worry about exception because we are stopping anyway
106  cerr << "EEGDataStream: Exception caugh when stopping stream.";
107  }
108  }
109 }
110 
111 
112 void EEGDataStream::timeoutFunction()
113 {
114  try
115  {
116  updater();
117  }
118  catch(exception& e)
119  {
120  halt = true;
121  cerr << "Halted data stream due to exception: " << e.what() << "\n";
122  }
123  catch(const char *m)
124  {
125  halt = true;
126  cerr << "Halted data stream due to exception: " << m << "\n";
127  }
128 }