示例#1
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 );
}
/**
* 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 );
}
示例#3
0
/**
 * Program which prints ThinkGear Raw Wave Values to stdout.
 */
int
main( void ) {
    
    char *comPortName  = NULL;
    int   dllVersion   = 0;
    int   connectionId = 0;
    int   packetsRead  = 0;
    int   errCode      = 0;
    
    double secondsToRun = 0;
    time_t startTime    = 0;
    time_t currTime     = 0;
    char  *currTimeStr  = NULL;

	FILE* attention;
	FILE* bufferState=fopen("state.txt","w+");
	FILE* raw;
	FILE* alpha1;
	FILE* alpha2;
	FILE* beta1;
	FILE* beta2;
	FILE* delta;
	FILE* theta;
	FILE* gamma1;
	FILE* gamma2;
    
    /* Print driver version number */
    dllVersion = TG_GetDriverVersion();
    printf( "ThinkGear DLL version: %d\n", dllVersion );
	fwrite("1",sizeof(char),1,bufferState);
	fclose(bufferState);
    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();
    if( connectionId < 0 ) {
        fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n",
                connectionId );
        wait2();
        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 );
       wait2();
        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 );
        wait2();
        exit( EXIT_FAILURE );
    }
    
    /* Attempt to connect the connection ID handle to serial port "COM5" */
    /* NOTE: On Windows, COM10 and higher must be preceded by \\.\, as in
     *       "\\\\.\\COM12" (must escape backslashes in strings).  COM9
     *       and lower do not require the \\.\, but are allowed to include
     *       them.  On Mac OS X, COM ports are named like
     *       "/dev/tty.MindSet-DevB-1".
     */
    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 );
        wait2();
        exit( EXIT_FAILURE );
    }
   

    /* Keep reading ThinkGear Packets from the connection for 5 seconds... */
    secondsToRun = 5;
    startTime = time( NULL );
	while(1){
		int i=0;
		attention=fopen("attention.txt","w+");
		raw=fopen("raw.txt","w+");
        alpha1=fopen("alpha1.txt","w+");
		alpha2=fopen("alpha2.txt","w+");
		beta1=fopen("beta1.txt","w+");
		beta2=fopen("beta2.txt","w+");
		delta=fopen("delta.txt","w+");
		theta=fopen("theta.txt","w+");
		gamma1=fopen("gamma1.txt","w+");
		gamma2=fopen("gamma2.txt","w+");
		
    while(i<50){// difftime(time(NULL), startTime) < secondsToRun ) {
		
        /* Read all currently available Packets, one at a time... */ 
        do {
            //printf("%d\n",i);
            /* Read a single Packet from the connection */
            packetsRead = TG_ReadPackets( connectionId, 1 );
            
            /* If TG_ReadPackets() was able to read a Packet of data... */
            if( packetsRead == 1 ) {
                
                /* If the Packet containted a new raw wave value... */
                if( TG_GetValueStatus(connectionId, TG_DATA_RAW) != 0 ) {
                    
                    /* Get the current time as a string */
                    currTime = time( NULL );
        			currTimeStr = ctime( &currTime );
					//int check;
					//check= (int)TG_GetValue(connectionId,TG_DATA_POOR_SIGNAL);
					if(TG_GetValue(connectionId,TG_DATA_POOR_SIGNAL)> 90)
					{
						printf("%d\n",(int)TG_GetValue(connectionId,TG_DATA_POOR_SIGNAL));
						fprintf(raw,FORMAT,0);
						fprintf(alpha1,FORMAT,0);
						fprintf(alpha2,FORMAT,0);
						fprintf(beta1,FORMAT,0);
						fprintf(beta2,FORMAT,0);
						fprintf(delta,FORMAT,0);
						fprintf(gamma1,FORMAT,0);
						fprintf(gamma2,FORMAT,0);
						fprintf(theta,FORMAT,0);
						fprintf(attention,FORMAT,0);
					}
					else
					{
						fprintf(raw,FORMAT,TG_GetValue(connectionId,TG_DATA_RAW));
						fprintf(alpha1,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_ALPHA1));
						fprintf(alpha2,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_ALPHA2));
						fprintf(beta1,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_BETA1));
						fprintf(beta2,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_BETA2));
						fprintf(delta,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_DELTA));
						fprintf(gamma1,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_GAMMA1));
						fprintf(gamma2,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_GAMMA2));
						fprintf(theta,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_THETA));
						fprintf(attention,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_ATTENTION));
					}
					//printf(stdout,FORMAT,TG_GetValue(connectionId,TG_DATA_THETA));
                    /* Get and print out the new raw value */
                    //fprintf( stdout, "%s: raw: %d\n", currTimeStr,
                          //  (int)TG_GetValue(connectionId, TG_DATA_RAW) );
                    //fflush( stdout );
                      i++;
                } /* end "If Packet contained a raw wave value..." */
                
            } /* end "If TG_ReadPackets() was able to read a Packet..." */
          
        } while( packetsRead > 0 ); /* Keep looping until all Packets read */
    } /* end "Keep reading ThinkGear Packets for 5 seconds..." */
	fclose(raw);
	fclose(alpha1);
	fclose(alpha2);
	fclose(beta1);
	fclose(beta2);
	fclose(delta);
	fclose(theta);
	fclose(gamma1);
	fclose(gamma2);
	fclose(attention);
	bufferState=fopen("state.txt","w");
	fwrite("0",sizeof(char),1,bufferState);
	fclose(bufferState);
	wait();
	//printf("check\n");
	//fflush(stdout);
	/*bufferState=fopen("state.txt","w+");
	fprintf(bufferState,"%c","0");
	fclose(bufferState);*/
	}
    /* Clean up */
    TG_FreeConnection( connectionId );
    
    /* End program */
    //wait();
    return( EXIT_SUCCESS );
}
/**
 * Program which prints ThinkGear Raw Wave Values to stdout.
 */
int
main( void ) {

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

    double secondsToRun = 0;
    time_t startTime    = 0;
    time_t currTime     = 0;
    char  *currTimeStr  = NULL;

    /* 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" */
    /* NOTE: On Windows, COM10 and higher must be preceded by \\.\, as in
     *       "\\\\.\\COM12" (must escape backslashes in strings).  COM9
     *       and lower do not require the \\.\, but are allowed to include
     *       them.  On Mac OS X, COM ports are named like 
     *       "/dev/tty.MindSet-DevB-1".
     */
    comPortName = "\\\\.\\COM5";
    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 );
    }

    /* Keep reading ThinkGear Packets from the connection for 5 seconds... */
    secondsToRun = 5;
    startTime = time( NULL );
    while( difftime(time(NULL), startTime) < secondsToRun ) {

        /* Read all currently available Packets, one at a time... */
        do {

            /* Read a single Packet from the connection */
            packetsRead = TG_ReadPackets( connectionId, 1 );

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

                /* If the Packet containted a new raw wave value... */
                if( TG_GetValueStatus(connectionId, TG_DATA_RAW) != 0 ) {

                    /* Get the current time as a string */
                    currTime = time( NULL );
        			currTimeStr = ctime( &currTime );

                    /* Get and print out the new raw value */
                    fprintf( stdout, "%s: raw: %d\n", currTimeStr,
                             (int)TG_GetValue(connectionId, TG_DATA_RAW) );
                    fflush( stdout );

                } /* end "If Packet contained a raw wave value..." */

            } /* end "If TG_ReadPackets() was able to read a Packet..." */

        } while( packetsRead > 0 ); /* Keep looping until all Packets read */

    } /* end "Keep reading ThinkGear Packets for 5 seconds..." */

    /* Clean up */
    TG_FreeConnection( connectionId );

    /* End program */
    wait();
    return( EXIT_SUCCESS );
}
void MindwaveModule::disconnect(int connectionID){
    TG_FreeConnection(connectionID);
}