예제 #1
0
파일: mindwave.c 프로젝트: cyberpeople/mind
/**
 * Program which prints ThinkGear EEG_POWERS values to stdout.
 */
int main( void ) {

	opensocket();
	dataFile = fopen("dataStore.txt", "a+");

    /* Print driver version number */
    dllVersion = TG_GetDriverVersion();
    printf( "ThinkGear DLL version: %d\n", dllVersion );

    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();
    if( connectionId < 0 ) {
        fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
                 connectionId );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Attempt to connect the connection ID handle to serial port "COM5" */
    //comPortName = "\\\\.\\COM19";
	comPortName = "COM7:";
    errCode = TG_Connect( connectionId, 
                          comPortName, 
                          TG_BAUD_57600, 
                          TG_STREAM_PACKETS );

	printf("TG_Connect struct: %d, %s, %d \n", connectionId, comPortName, TG_BAUD_57600);
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    errCode = TG_EnableBlinkDetection( connectionId, 1 );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_EnableBlinkDetection() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }


	do
	{
		readdata();
		if(blink>=80)
			trncmd();
		else if(att>=68)
			fwdcmd();
		//wait();
		 
	}while(1);

    /* Clean up */
    TG_FreeConnection( connectionId );

    /* End program */
    wait();
    return( EXIT_SUCCESS );
}
bool ThinkgearCommsDriver::connect() {

    reset();
    
	comPortName = "\\\\.\\COM3";

	// driver version
	dllVersion = TG_GetDriverVersion();
	printf("ThinkGear DLL version: %d\n", dllVersion);

	// id conection
	connectionId = TG_GetNewConnectionId();
	printf("ThinkGear connectionId: %d\n", connectionId);


	if (connectionId < 0) {
		fprintf(stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n",connectionId);
	}

    // attempt to connect    
    int conResult = TG_Connect(connectionId, deviceName.c_str(), TG_BAUD_57600, TG_STREAM_PACKETS);

	ofLogVerbose() << "Connecting to connectionID " << connectionId << ". Driver comPortName: " << deviceName;

    if (conResult != 0) {
        ofLogVerbose() << "Error: Connection Failed! " << conResult;
        if(conResult == -2)            
            ofLogVerbose() << "Serial port could not be opened!";
        isReady = false;
        return isReady;
    }
    
    
    // enable blinking for connection
    int resBlink = TG_EnableBlinkDetection(connectionId, 1);
    if (resBlink != 0)
        ofLogVerbose() << "Blink: Failed to enable blink detection";
    
    isReady = true;
    return isReady;
}
예제 #3
0
void Mindwave::connect()
{
	_MdataFile = fopen("dataStore.txt", "a+");

	/* Print driver version number */
	_MdllVersion = TG_GetDriverVersion();
	printf( "ThinkGear DLL version: %d\n", _MdllVersion );

	/* Get a connection ID handle to ThinkGear */
	_MconnectionId = TG_GetNewConnectionId();
	if( _MconnectionId < 0 ) {
		fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
			_MconnectionId );
		wait();
		exit( EXIT_FAILURE );
	}

	/* Attempt to connect the connection ID handle to serial port "COM5" */
	_McomPort = "\\\\.\\COM28";
	errCode = TG_Connect( _MconnectionId, 
		_McomPort, 
		TG_BAUD_57600, 
		TG_STREAM_PACKETS );
	if( errCode < 0 ) {
		fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
		wait();
		exit( EXIT_FAILURE );
	}

	errCode = TG_EnableBlinkDetection( _MconnectionId, 1 );
	if( errCode < 0 ) {
		fprintf( stderr, "ERROR: TG_EnableBlinkDetection() returned %d.\n", errCode );
		wait();
		exit( EXIT_FAILURE );
	}

}
예제 #4
0
/**
 * Program which prints ThinkGear EEG_POWERS values to stdout.
 */
int
main( void ) {

    char *comPortName = NULL;
    int   dllVersion = 0;
    int   connectionId = 0;
    int   packetsRead = 0;
    int   errCode = 0;

    /* Print driver version number */
    dllVersion = TG_GetDriverVersion();
    printf( "ThinkGear DLL version: %d\n", dllVersion );

    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();
    if( connectionId < 0 ) {
        fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
                 connectionId );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Set/open stream (raw bytes) log file for connection */
    errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetStreamLog() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Set/open data (ThinkGear values) log file for connection */
    errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetDataLog() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Attempt to connect the connection ID handle to serial port "COM5" */
    comPortName = "\\\\.\\COM3";
    errCode = TG_Connect( connectionId, 
                          comPortName, 
                          TG_BAUD_57600, 
                          TG_STREAM_PACKETS );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

		//To get eyeblinks, you will need to first call the following function:
		TG_EnableBlinkDetection(connectionId, 1 );

    /* Read 10 ThinkGear Packets from the connection, 1 Packet at a time */
    packetsRead = 0;
    while( 1 ) {

        /* Attempt to read a Packet of data from the connection */
        errCode = TG_ReadPackets( connectionId, 1 );

        /* If TG_ReadPackets() was able to read a complete Packet of data... */
        if( errCode == 1 ) {
            packetsRead++;

            /* If attention value has been updated by TG_ReadPackets()... */
            if( TG_GetValueStatus(connectionId, TG_DATA_ATTENTION) != 0 ) {

                /* Get and print out the updated attention value */
                fprintf( stdout, "New attention value: %d\n",
                         (int)TG_GetValue(connectionId, TG_DATA_ATTENTION) );
                fflush( stdout );

            } /* end "If attention value has been updated..." */

            /* If attention value has been updated by TG_ReadPackets()... */
            if( TG_GetValueStatus(connectionId, TG_DATA_MEDITATION) != 0 ) {

                /* Get and print out the updated attention value */
                fprintf( stdout, "New meditation value: %d\n",
                         (int)TG_GetValue(connectionId, TG_DATA_MEDITATION) );
                fflush( stdout );
                fprintf( stdout, "New blink value: %f\n",
                         TG_GetValue(connectionId, TG_DATA_BLINK_STRENGTH) );
                fflush( stdout );

            } /* end "If attention value has been updated..." */

        } /* end "If a Packet of data was read..." */

    } /* end "Read 10 Packets of data from connection..." */

    /* Clean up */
    TG_FreeConnection( connectionId );

    /* End program */
    wait();
    return( EXIT_SUCCESS );
}
예제 #5
0
/**
* Program which prints ThinkGear EEG_POWERS values to stdout.
*/
int
	main( void ) {

		// http://andre.stechert.org/urwhatu/2006/01/error_c2143_syn.html
		// It's no surprise that Microsoft's compiler wants the variables at the beginnig of the function: Standard C wants them there.
		// At least, this is what C89 defines. C99 allows variable declarations also in the middle of a function.
		char *comPortName = NULL;
		char *portNumber = (char*)malloc(sizeof(char) * (2 + 1));
		char *comPortBase = NULL;
		int   dllVersion = 0;
		int   connectionId = 0;
		int   packetsRead = 0;
		int   errCode = 0;
		int comPortFound = 0;
		const int MAX_PORT = 16;
		size_t length = 0;
		int i = 0;
		int j = 0;
		int x = 0;
		int y = 0;
		INPUT input;
		POINT cursorPos;

		/* Print driver version number */
		dllVersion = TG_GetDriverVersion();
		printf( "ThinkGear DLL version: %d\n", dllVersion );

		/* Get a connection ID handle to ThinkGear */
		connectionId = TG_GetNewConnectionId();
		if( connectionId < 0 ) {
			fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
				connectionId );
			wait();
			exit( EXIT_FAILURE );
		}
		fprintf( stderr, "ThinkGear Connection ID is: %d.\n\n", connectionId );

		/* Set/open stream (raw bytes) log file for connection */
		errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
		if( errCode < 0 ) {
			fprintf( stderr, "ERROR: TG_SetStreamLog() returned %d.\n", errCode );
			wait();
			exit( EXIT_FAILURE );
		}

		/* Set/open data (ThinkGear values) log file for connection */
		errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
		if( errCode < 0 ) {
			fprintf( stderr, "ERROR: TG_SetDataLog() returned %d.\n", errCode );
			wait();
			exit( EXIT_FAILURE );
		}

		/* Attempt to connect the connection ID handle to serial ports between COM0 and "COM MAX_PORT" */
		fprintf(stdout, "Scanning COM ports 0 to %d...\n", MAX_PORT);
		comPortBase = "\\\\.\\COM";
		length = strlen(comPortBase);
		comPortName = (char *)realloc (comPortName, (length + 5)*sizeof(char)); 

		for(i=0; i <= MAX_PORT && comPortFound == 0; i++)
		{

			// Generating the serial port number			
			portNumber = itoa(i, portNumber, 10);
			fprintf( stderr, portNumber);
			strcpy(comPortName,comPortBase);

			for(j=0; j<strlen(portNumber); j++)
			{
				comPortName[length+j] = portNumber[j];
			}

			comPortName[length+strlen(portNumber)] = '\0';
			// Maybe I could have used something like strcat(comPortBase, portNumber);

			fprintf( stdout, "ok");
			fprintf( stdout, comPortName);

			//comPortName = "\\\\.\\COM11";

			// Trying to connect on the generated serial port number
			fprintf( stdout, "trying to connect on");
			fprintf( stdout, comPortName);
			fprintf( stdout, "\n");
			errCode = TG_Connect( connectionId, 
				comPortName, 
				TG_BAUD_9600, 
				TG_STREAM_PACKETS );
			if( errCode < 0 ) {
				fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
				if(errCode == -1) printf("FAILED connection (-1 connectionId does not refer to a valid ThinkGear Connection ID handle.)\n");
				if(errCode == -2) printf("FAILED connection (-2 serialPortName could not be opened as a serial communication port for any reason.)\n");
				if(errCode == -3) printf("FAILED connection (-3 serialBaudrate is not a valid TG_BAUD_* value.)\n");
				if(errCode == -4) printf("FAILED connection (-4 serialDataFormat is not a valid TG_STREAM_* type.)\n");
			} 
			else
			{

				// Trying to read one packet to check the connection.
				printf("Connection available...\n");
				Sleep(10000); // sometimes we need to wait a little...
				errCode = TG_ReadPackets(connectionId, 1);
				if(errCode >= 0)
				{	
					printf("OK\n");
					comPortFound = 1;
				}
				else
				{
					if(errCode == -1) printf("FAILED reading (Invalid connection ID)\n");
					if(errCode == -2) printf("FAILED reading (0 bytes on the stream)\n");
					if(errCode == -3) printf("FAILED reading (I/O error occured)\n");
				}


			}
		} 	/* end: "Attempt to connect the connection ID handle to serial ports between COM0 and "COM16"" */


		// Hopefully the connection should have been established now.
		// Otherwise we stop here.
		if (comPortFound == 1) {
			printf( "connection established");
		} else {
			printf( "connection could not be established");
			wait();
			exit( EXIT_FAILURE ); 
		}

		//To get eyeblinks, you will need to first call the following function:
		TG_EnableBlinkDetection(connectionId, 1 );

		/* Read 10 ThinkGear Packets from the connection, 1 Packet at a time */
		packetsRead = 0;
		while (1){
			//while( packetsRead < 10000 ) {

			/* Attempt to read a Packet of data from the connection */
			errCode = TG_ReadPackets( connectionId, 1 );


			/* If TG_ReadPackets() was able to read a complete Packet of data... */
			if( errCode == 1 ) {
				packetsRead++;
				printf( ".");

				/* If attention value has been updated by TG_ReadPackets()... */
				if( TG_GetValueStatus(connectionId, TG_DATA_ATTENTION) != 0 ) {

					/* Get and print out the updated attention value */
					fprintf( stdout, "New attention value: %f\n",
						TG_GetValue(connectionId, TG_DATA_ATTENTION) );
					fflush( stdout );

				} /* end "If attention value has been updated..." */


				/* If eye blinking value has been updated by TG_ReadPackets()... */
				if( TG_GetValueStatus(connectionId, TG_DATA_BLINK_STRENGTH) != 0 ) {

					/* Get and print out the updated eye blinking value */
					fprintf( stdout, "New eye blinking value: %f\n",
						TG_GetValue(connectionId, TG_DATA_BLINK_STRENGTH) );
					fflush( stdout );

					// Get mouse position: 
					// * http://msdn.microsoft.com/en-us/library/ms648380(v=vs.85).aspx#_win32_Using_the_Keyboard_to_Move_the_Cursor 
					// * http://msdn.microsoft.com/en-us/library/ms648390%28VS.85%29.aspx
					// * http://stackoverflow.com/questions/2396576/how-to-get-the-cursor-position

					GetCursorPos(&cursorPos);
					x = (int) cursorPos.x;
					y = (int) cursorPos.y;
					//x = 10;
					//y = 20;

					// http://stackoverflow.com/questions/4540282/using-mouse-with-sendinput-in-c
					input.type = INPUT_MOUSE;
					input.mi.mouseData=0;
					input.mi.dx = x*(65536.0f/GetSystemMetrics(SM_CXSCREEN));//x being coord in pixels
					input.mi.dy = y*(65536.0f/GetSystemMetrics(SM_CYSCREEN));//y being coord in pixels
					input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
					SendInput(1,&input,sizeof(input));

				} /* end "If eye blinking value has been updated..." */



			} /* end "If a Packet of data was read..." */

			// Posted by Friedemann Wolpert on July 04, 2012 @ 11:45 PM
			// Regarding your 100% CPU Problem
			// http://support.neurosky.com/discussions/mindwave/916-can-the-neurosky-mindwave-be-used-to-emulate-mouse-clicks-for-somebody-using-computers-all-day-long#comment_17102761
			if( errCode == 0 ) {
				printf( "z");
				Sleep(200);   // use #include Windows.h  
			}


		} /* end "Read 10 Packets of data from connection..." */

		/* Clean up */
		TG_FreeConnection( connectionId );

		/* End program */
		wait();
		return( EXIT_SUCCESS );
}