	public void run() {
		eEvent				= Edk.INSTANCE.EE_EmoEngineEventCreate();
    	eState				= Edk.INSTANCE.EE_EmoStateCreate();
    	IntByReference userID 		= null;
		IntByReference nSamplesTaken= null;
    	int state  					= 0;
    	float secs 					= 60;
    	boolean readytocollect 		= false;
    	collecting = true;
    	writingMatrix = false;
    	userID 			= new IntByReference(0);
		nSamplesTaken	= new IntByReference(0);
		try {
			out = new BufferedWriter(new FileWriter("VisualizationData/" + fileName + ".txt"));
		} catch (IOException e) {
			System.err.println(e.getMessage());
			System.exit(-1);
		}
		if (Edk.INSTANCE.EE_EngineConnect("Emotiv Systems-5") != EdkErrorCode.EDK_OK.ToInt()) {
			System.err.println("Emotiv Engine start up failed.");
			return;
		}
		Pointer hData = Edk.INSTANCE.EE_DataCreate();
		Edk.INSTANCE.EE_DataSetBufferSizeInSec(secs); 
    	System.out.println("Started receiving EEG Data!");
		while (collecting) {	
			state = Edk.INSTANCE.EE_EngineGetNextEvent(eEvent);
			if (state == EdkErrorCode.EDK_OK.ToInt()) {
				int eventType = Edk.INSTANCE.EE_EmoEngineEventGetType(eEvent);
				Edk.INSTANCE.EE_EmoEngineEventGetUserId(eEvent, userID);
				if (eventType == Edk.EE_Event_t.EE_UserAdded.ToInt()) {
						if (userID != null) {
							System.out.println("User added");
							Edk.INSTANCE.EE_DataAcquisitionEnable(userID.getValue(),true);
							readytocollect = true;
						}
				}
				if (eventType == Edk.EE_Event_t.EE_EmoStateUpdated.ToInt()) {
					Edk.INSTANCE.EE_EmoEngineEventGetEmoState(eEvent, eState);
				}
			}
			else if (state != EdkErrorCode.EDK_NO_EVENT.ToInt()) {
				System.err.println("Internal error in Emotiv Engine!");
				break;
			}
			if (readytocollect) {
				Edk.INSTANCE.EE_DataUpdateHandle(0, hData);
				Edk.INSTANCE.EE_DataGetNumberOfSample(hData, nSamplesTaken);
				if (nSamplesTaken != null) {
					if (nSamplesTaken.getValue() != 0) {
						double[] data = new double[nSamplesTaken.getValue()];
						for (int sampleIdx=0 ; sampleIdx < nSamplesTaken.getValue(); ++sampleIdx) {
							try {
								Edk.INSTANCE.EE_DataGet(hData, 19, data, nSamplesTaken.getValue());
								out.write(Integer.toString((int) (data[sampleIdx] * 1000)) + " ");
								for (int i = 0 ; i < 25 ; i++) {
									Edk.INSTANCE.EE_DataGet(hData, i, data, nSamplesTaken.getValue());
									if ( writingMatrix && i >= 3 && i <= 16) {
										try {
											matrix.matrix[sample][i-3] = data[sampleIdx];
										} catch (ArrayIndexOutOfBoundsException e) {
											writingMatrix = false;
										}
									}
									out.write( Double.toString((data[sampleIdx])));
									out.write(" ");
								}
								if(writingMatrix)
									sample++;
								out.write("0");
								for (int i = 1; i < 15 ; i++)
									out.write(" " + EmoState.INSTANCE.ES_GetContactQuality(eState, i) + " ");
								out.newLine();
							} catch (IOException e) {
								System.err.println(e.getMessage());
								System.exit(-1);
							}
						}
					}
				}
			} 
		} 
		cleanUp();
	}

	public void run() {
		eEvent				= Edk.INSTANCE.EE_EmoEngineEventCreate();
    	eState				= Edk.INSTANCE.EE_EmoStateCreate();
    	IntByReference userID 		= null;
		IntByReference nSamplesTaken= null;
    	int state  					= 0;
    	float secs 					= 60;
    	boolean readytocollect 		= false;
    	collecting = true;
    	writingMatrix = false;
    	userID 			= new IntByReference(0);
		nSamplesTaken	= new IntByReference(0);
		try {
			out = new BufferedWriter(new FileWriter("VisualizationData/" + fileName + ".txt"));
		} catch (IOException e) {
			System.err.println(e.getMessage());
			System.exit(-1);
		}
		if (Edk.INSTANCE.EE_EngineConnect("Emotiv Systems-5") != EdkErrorCode.EDK_OK.ToInt()) {
			System.err.println("Emotiv Engine start up failed.");
			return;
		}
		Pointer hData = Edk.INSTANCE.EE_DataCreate();
		Edk.INSTANCE.EE_DataSetBufferSizeInSec(secs); 
    	System.out.println("Started receiving EEG Data!");
		while (collecting) {	
			state = Edk.INSTANCE.EE_EngineGetNextEvent(eEvent);
			if (state == EdkErrorCode.EDK_OK.ToInt()) {
				int eventType = Edk.INSTANCE.EE_EmoEngineEventGetType(eEvent);
				Edk.INSTANCE.EE_EmoEngineEventGetUserId(eEvent, userID);
				if (eventType == Edk.EE_Event_t.EE_UserAdded.ToInt()) {
						if (userID != null) {
							System.out.println("User added");
							Edk.INSTANCE.EE_DataAcquisitionEnable(userID.getValue(),true);
							readytocollect = true;
						}
				}
				if (eventType == Edk.EE_Event_t.EE_EmoStateUpdated.ToInt()) {
					Edk.INSTANCE.EE_EmoEngineEventGetEmoState(eEvent, eState);
				}
			}
			else if (state != EdkErrorCode.EDK_NO_EVENT.ToInt()) {
				System.err.println("Internal error in Emotiv Engine!");
				break;
			}
			if (readytocollect) {
				Edk.INSTANCE.EE_DataUpdateHandle(0, hData);
				Edk.INSTANCE.EE_DataGetNumberOfSample(hData, nSamplesTaken);
				if (nSamplesTaken != null) {
					if (nSamplesTaken.getValue() != 0) {
						double[] data = new double[nSamplesTaken.getValue()];
						for (int sampleIdx=0 ; sampleIdx < nSamplesTaken.getValue(); ++sampleIdx) {
							try {
								Edk.INSTANCE.EE_DataGet(hData, 19, data, nSamplesTaken.getValue());
								out.write(Integer.toString((int) (data[sampleIdx] * 1000)) + " ");
								for (int i = 0 ; i < 25 ; i++) {
									Edk.INSTANCE.EE_DataGet(hData, i, data, nSamplesTaken.getValue());
									if ( writingMatrix && i >= 3 && i <= 16) {
										try {
											matrix.matrix[sample][i-3] = data[sampleIdx];
										} catch (ArrayIndexOutOfBoundsException e) {
											writingMatrix = false;
										}
									}
									out.write( Double.toString((data[sampleIdx])));
									out.write(" ");
								}
								if(writingMatrix)
									sample++;
								out.write((writingMatrix)? "1" : "0");
								for (int i = 1; i < 15 ; i++)
									out.write(" " + EmoState.INSTANCE.ES_GetContactQuality(eState, i) + " ");
								out.newLine();
							} catch (IOException e) {
								System.err.println(e.getMessage());
								System.exit(-1);
							}
						}
					}
				}
			} 
		} 
		cleanUp();
	}
