Example #1
0
int main(int argc, char **argv)
{
    // location of the machine running the "BlueGirl" head model
    std::string receiverHost = "localhost";
    
    if (argc > 2) {
        std::cout << "Usage: " << argv[0] << " <hostname>" << std::endl;
        std::cout << "The arguments specify the host of the head model "
                     "(Default: localhost)" << std::endl;
        return 1;
    }

    if (argc > 1) {
        receiverHost = std::string(argv[1]);
    }

    EmoEngineEventHandle eEvent = IEE_EmoEngineEventCreate();
    EmoStateHandle eState       = IEE_EmoStateCreate();
    unsigned int userID         = 0;
    const int CONTROL_PANEL_PORT = 3008;
    
    try {

        // Connect to EmoEngine
        if (IEE_EngineConnect() != EDK_OK) {
            throw std::runtime_error("EmoEngine start up failed.");
        }
        else {
            std::cout << "EmoEngine started!" << std::endl;
        }

        int startSendPort = 30000;
        std::map<unsigned int, SocketClient> socketMap;

        std::cout << "Type \"exit\" to quit, \"help\" to list available commands..."
                  << std::endl;
        promptUser();

        while (true) {

            // Handle the user input
            if (_kbhit()) {
                if (!handleUserInput()) {
                    break;
                }
            }

            int state = IEE_EngineGetNextEvent(eEvent);

            // New event needs to be handled
            if (state == EDK_OK) {

                IEE_Event_t eventType = IEE_EmoEngineEventGetType(eEvent);
                IEE_EmoEngineEventGetUserId(eEvent, &userID);

                switch (eventType) {

                // New headset connected
                // Create a new socket to send the animation
                case IEE_UserAdded:
                {
                    std::cout << std::endl << "New user " << userID
                              << " added, sending FacialExpression 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 IEE_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 FacialExpression animation
                // if EmoState has been updated
                case IEE_EmoStateUpdated:
                {
                    IEE_EmoEngineEventGetEmoState(eEvent, eState);

                    std::map<unsigned int, SocketClient>::iterator iter;
                    iter = socketMap.find(userID);
                    if (iter != socketMap.end()) {
                        sendFacialExpressionAnimation(iter->second, eState);
                    }
                    break;
                }

                // Handle FacialExpression training event
                case IEE_FacialExpressionEvent:
                {
                    handleFacialExpressionEvent(std::cout, eEvent);
                }

                default:
                    break;
                }
            }
            else if (state != EDK_NO_EVENT) {
                std::cout << std::endl << "Internal error in Emotiv Engine!"
                          << std::endl;
                break;
            }

#ifdef _WIN32
            Sleep(15);
#endif
#ifdef __linux__
            sleep(1);
#endif
        }
    }
    catch (const std::runtime_error& e) {
        std::cerr << e.what() << std::endl;
        std::cout << "Press 'Enter' to exit..." << std::endl;
        getchar();
    }

    // Clean up
    IEE_EngineDisconnect();
    IEE_EmoStateFree(eState);
    IEE_EmoEngineEventFree(eEvent);

    return 0;
}
Example #2
0
int main(int argc, char** argv) {

	EmoEngineEventHandle eEvent			= IEE_EmoEngineEventCreate();
	EmoStateHandle eState				= IEE_EmoStateCreate();
	unsigned int userID					= 0;
	const unsigned short composerPort	= 1726;
	int option = 0;
	int state  = 0;
	std::string input;

	try {

		if (argc != 2) {
            throw std::runtime_error("Please supply the log file name.\n"
                                     "Usage: EmoStateLogger [log_file_name].");
		}

        std::cout << "==================================================================="
                  << std::endl;
        std::cout << "Example to show how to log the EmoState from EmoEngine/EmoComposer."
                  << std::endl;
        std::cout << "==================================================================="
                  << std::endl;
        std::cout << "Press '1' to start and connect to the EmoEngine                    "
                  << std::endl;
        std::cout << "Press '2' to connect to the EmoComposer                            "
                  << std::endl;
		std::cout << ">> ";

		std::getline(std::cin, input, '\n');
		option = atoi(input.c_str());

		switch (option) {
        case 1:
        {
            if (IEE_EngineConnect() != EDK_OK) {
                throw std::runtime_error("Emotiv Driver start up failed.");
            }
            break;
        }
        case 2:
        {
            std::cout << "Target IP of EmoComposer? [127.0.0.1] ";
            std::getline(std::cin, input, '\n');

            if (input.empty()) {
                input = std::string("127.0.0.1");
            }

            if (IEE_EngineRemoteConnect(input.c_str(), composerPort) != EDK_OK) {
                std::string errMsg = "Cannot connect to EmoComposer on [" +
                                            input + "]";
                throw std::runtime_error(errMsg.c_str());
            }
            break;
        }
        default:
            throw std::runtime_error("Invalid option...");
            break;
		}
		
		
        std::cout << "Start receiving EmoState! Press any key to stop logging...\n"
                  << std::endl;

		std::ofstream ofs(argv[1]);
		bool writeHeader = true;
		
		while (!_kbhit()) {

			state = IEE_EngineGetNextEvent(eEvent);

			// New event needs to be handled
			if (state == EDK_OK) {

				IEE_Event_t eventType = IEE_EmoEngineEventGetType(eEvent);
				IEE_EmoEngineEventGetUserId(eEvent, &userID);

				// Log the EmoState if it has been updated
				if (eventType == IEE_EmoStateUpdated) {

					IEE_EmoEngineEventGetEmoState(eEvent, eState);
					const float timestamp = IS_GetTimeFromStart(eState);

                    printf("%10.3fs : New EmoState from user %d ...\r",
                           timestamp, userID);
					
					logEmoState(ofs, userID, eState, writeHeader);
					writeHeader = false;
				}
			}
			else if (state != EDK_NO_EVENT) {
				std::cout << "Internal error in Emotiv Engine!" << std::endl;
				break;
			}

#ifdef _WIN32
            Sleep(1);
#endif
#ifdef __linux__
            sleep(1);
#endif
		}

		ofs.close();
	}
    catch (const std::runtime_error& e) {
		std::cerr << e.what() << std::endl;
		std::cout << "Press any key to exit..." << std::endl;
		getchar();
	}

	IEE_EngineDisconnect();
	IEE_EmoStateFree(eState);
	IEE_EmoEngineEventFree(eEvent);

	return 0;
}