BOOL EmotivRead::EB_CloseDevice() { if(m_hData!=NULL) { EE_DataFree(m_hData); } if(EE_EngineDisconnect()!=EDK_OK) { return FALSE; } if(m_neEvent!=NULL) { EE_EmoEngineEventFree(m_neEvent); } if(m_neState!=NULL) { EE_EmoStateFree(m_neState); } return TRUE; }
int main(int argc, char** argv) { comPort=openPort(portS);//***PUT YOUR COM PORT NUMBER HERE!*** // location of the machine running the 3D motion cube std::string receiverHost = "localhost"; if (argc > 2) { std::cout << "Usage: " << argv[0] << " <hostname>" << std::endl; std::cout << "The arguments specify the host of the motion cube (Default: localhost)" << std::endl; return 1; } if (argc > 1) { receiverHost = std::string(argv[1]); } EmoEngineEventHandle eEvent = EE_EmoEngineEventCreate(); EmoStateHandle eState = EE_EmoStateCreate(); unsigned int userID = 0; try { // if (EE_EngineConnect() != EDK_OK) { if (EE_EngineRemoteConnect("127.0.0.1", 3008) != EDK_OK) { throw std::exception("Emotiv Engine start up failed."); } else { std::cout << "Emotiv Engine started!" << std::endl; neutral();//send all servos to their neutral positions } int startSendPort = 6868; std::map<unsigned int, SocketClient> socketMap; promptUser(); while (true) { // Handle the user input if (_kbhit()) { if (!handleUserInput()) { break; } } if(paused == true){ int state = EE_EngineGetNextEvent(eEvent); // New event needs to be handled if (state == EDK_OK) { EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent); EE_EmoEngineEventGetUserId(eEvent, &userID); switch (eventType) { // New headset connected, create a new socket to send the animation case EE_UserAdded: { std::cout << std::endl << "New user " << userID << " added, sending Cognitiv animation to "; std::cout << receiverHost << ":" << startSendPort << "..." << std::endl; promptUser(); socketMap.insert(std::pair<unsigned int, SocketClient>( userID, SocketClient(receiverHost, startSendPort, UDP))); startSendPort++; break; } // Headset disconnected, remove the existing socket case EE_UserRemoved: { std::cout << std::endl << "User " << userID << " has been removed." << std::endl; promptUser(); std::map<unsigned int, SocketClient>::iterator iter; iter = socketMap.find(userID); if (iter != socketMap.end()) { socketMap.erase(iter); } break; } // Send the Cognitiv animation if EmoState has been updated case EE_EmoStateUpdated: { //std::cout << "New EmoState from user " << userID << "..." << std::endl; EE_EmoEngineEventGetEmoState(eEvent, eState); std::map<unsigned int, SocketClient>::iterator iter; iter = socketMap.find(userID); if (iter != socketMap.end()) { sendCognitiv(eState); } break; } // Handle Cognitiv training related event case EE_CognitivEvent: { handleCognitivEvent(std::cout, eEvent); break; } default: break; } } else if (state != EDK_NO_EVENT) { std::cout << "Internal error in Emotiv Engine!" << std::endl; break; } } Sleep(1); } } catch (const std::exception& e) { std::cerr << e.what() << std::endl; std::cout << "Press any keys to exit..." << std::endl; getchar(); } EE_EngineDisconnect(); EE_EmoStateFree(eState); EE_EmoEngineEventFree(eEvent); return 0; }
bool EpocHandler::disconnect() { connected = EE_EngineDisconnect() == EDK_OK; useradded = false; return connected; }
int main(int argc, char** argv) { EmoEngineEventHandle eEvent = EE_EmoEngineEventCreate(); EmoStateHandle eState = EE_EmoStateCreate(); unsigned int userID = 0; float secs = 1; unsigned int datarate = 0; bool readytocollect = false; int state = 0; bool connected = true; long long count = 0; // Connect to the EE_Engine. if (EE_EngineConnect() != EDK_OK) { std::cout << "Could not connect to EEG" << std::endl; return 0; } std::string directory = argv[2]; std::string filename = argv[1]; filename = directory + filename + "-EEGLogger.csv"; std::cout << filename << std::endl; // were the file will be saved std::cout << "Start receiving EEG Data! Press any key to stop logging...\n" << std::endl; std::ofstream ofs(filename.c_str(),std::ios::trunc); ofs << header << std::endl; if(connected){ DataHandle hData = EE_DataCreate(); EE_DataSetBufferSizeInSec(secs); std::cout << "Buffer size in secs:" << secs << std::endl; while (!kbhit()){ state = EE_EngineGetNextEvent(eEvent); if(!printFace(count)){ break; } // print new face to make every 20 seconds if (state == EDK_OK){ EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent); EE_EmoEngineEventGetUserId(eEvent, &userID); // Log the EmoState if it has been updated if (eventType == EE_UserAdded){ std::cout << "User added" << std::endl; EE_DataAcquisitionEnable(userID,true); readytocollect = true; } } if (readytocollect){ EE_DataUpdateHandle(0, hData); unsigned int nSamplesTaken=0; EE_DataGetNumberOfSample(hData,&nSamplesTaken); if (nSamplesTaken != 0) { double* data = new double[nSamplesTaken]; for (int sampleIdx=0 ; sampleIdx<(int)nSamplesTaken ; ++ sampleIdx) { for (int i = 0 ; i<sizeof(targetChannelList)/sizeof(EE_DataChannel_t) ; i++) { EE_DataGet(hData, targetChannelList[i], data, nSamplesTaken); ofs << data[sampleIdx] << ","; } ofs << std::endl; count++; } delete[] data; } } } ofs.close(); EE_DataFree(hData); } std::cout << "Disconnected" << std::endl; EE_EngineDisconnect(); EE_EmoStateFree(eState); EE_EmoEngineEventFree(eEvent); return 0; }
~EmoEngine() { EE_EngineDisconnect(); EE_EmoStateFree(_state); EE_EmoEngineEventFree(_event); }