CEBL  2.1
DeviceConfig.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <sstream>
3 using namespace std;
4 
5 
6 #include "../CEBLModel.hpp"
7 #include "DeviceConfig.hpp"
8 #include "Mindset24.hpp"
9 
10 
11 //----------------------------------------------------------------------
12 // Constructors / Destructors
13 
15 {
16  this->model = model;
17  device_location = "/dev/eeg";
18  device_connected = false;
19  device_exists = false;
20  device_writeable = false;
21  error_text = "";
22  inquiry_text = "";
23  this->scanForDevices();
24  this->isReady();
25 
26  this->sample_rate = 3;//we should read this from device
27  this->block_size = 3;//we should read this from device
28 }
29 
31 
32 
33 
34 //----------------------------------------------------------------------
35 // Getting Operations
36 
38 {
39  return this->sample_rate;
40 }
41 
43 {
44  return this->block_size;
45 }
46 
47 
48 
50 {
51  error_text = "";
52  inquiry_text = "";
53  ifstream read;
54  ofstream write;
55 
56  device_connected = false;
57  read.open(device_location.c_str(), ifstream::in);
58  read.close();
59  if(read.fail())
60  {
61  device_exists = false;
62  device_writeable = false;
63  device_connected = false;
64  error_text = model->getString("DeviceError2");
65  inquiry_text = "";
66  return false;
67  }
68  else
69  {
70  device_exists = true;
71  device_connected = false;
72  }
73 
74 
75  //check if device is writeable now
76  if(device_exists)
77  {
78  write.open(device_location.c_str(), ios::app);
79  write.close();
80  if(write.fail())
81  {
82  device_writeable = false;
83  device_connected = false;
84  error_text = model->getString("DeviceError1");
85  return false;
86  }
87  else
88  {
89  device_writeable = true;
90  error_text = "";
91  }
92 
93  }
94 
95 
96  if(device_writeable)
97  {
98  try
99  {
100  Mindset24 mindset;
101  mindset.Open(device_location.c_str());
102  if(!mindset.IsOpen())
103  {
104  error_text = model->getString("DeviceError6");
105  }
106  else if(!mindset.IsMindset())
107  {
108  string rep = model->getString("DeviceError5");
109  rep += mindset.Inquiry();
110  error_text = rep;
111  }
112  else if(!mindset.Ready())
113  {
114  error_text = model->getString("DeviceError4");
115  }
116  else
117  {
118  device_connected = true;
119  }
120  }
121  catch(...)
122  {
123  error_text = model->getString("DeviceError3");
124  device_connected = false;
125  return false;
126  }
127  }
128  //change widget appearances based on information gathered
129  if(device_connected)
130  {
131  string rep = model->getString("DeviceSuccess1");
132  try
133  {
134  //creating mindset null
135  Mindset24 mindset;
136  mindset.Open(device_location.c_str());
137  rep += mindset.Inquiry();
138  mindset.Close();
139  }catch(const char *m)
140  {
141  cout << m << "\n";
142  }
143  inquiry_text = rep;
144  }
145 
146  return device_connected;
147 }
148 
149 //----------------------------------------------------------------------
150 // Setting Operations
151 
152 
153 void DeviceConfig::setDeviceLocation(std::string location)
154 {
155  device_location = location;
156  this->isReady();
157 }
158 
159 void DeviceConfig::setSampleRate(int sample_rate)
160 {
161  this->sample_rate = sample_rate;
162 }
163 
164 void DeviceConfig::setBlockSize(int block_size)
165 {
166  this->block_size = block_size;
167 }
168 
170 {
171 
172  cout << "Searching for EEG Device...\n" << std::flush;
173  ifstream read;
174  ofstream write;
175  bool device_writeable, device_exists;
176  std::vector<string> device_labels;
177  //add device labels to search for
178  device_labels.push_back("eeg");
179  //device_labels.push_back("eeg_amplifier");
180  //device_labels.push_back("mindset");
181  for(int i=0;i<9;i++)
182  {
183  stringstream dev;
184  dev << "sg" << i;
185  device_labels.push_back(dev.str());
186  }
187 
188  //search through all device labels
189  for(unsigned int i=0; i<device_labels.size(); i++)
190  {
191  stringstream dev;
192  dev << "/dev/" << device_labels[i] ;
193  read.open(dev.str().c_str(), ifstream::in);
194  read.close();
195  if(read.fail())
196  {
197  device_exists = false;
198  device_writeable = false;
199  }
200  else
201  device_exists = true;
202  //check if device is writeable now
203  if(device_exists)
204  {
205  write.open(dev.str().c_str(), ios::app);
206  write.close();
207  if(write.fail())
208  device_writeable = false;
209  else
210  device_writeable = true;
211  }
212  //check if device is a mindset
213  if(device_writeable)
214  {
215  Mindset24 mindset;
216  mindset.Open(dev.str().c_str());
217  if(mindset.IsOpen() && mindset.IsMindset() && mindset.Ready())
218  {
219  device_connected = true;
220  device_location = dev.str();
221  break;
222  }
223  }
224  }
225 }