public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((ShimmerBluetooth)sender).GetDeviceName() + " State = " + ((ShimmerBluetooth)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { System.Diagnostics.Debug.Write("Connected"); RunOnUiThread(() => tvShimmerState.Text = "Shimmer State: Connected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { System.Diagnostics.Debug.Write("Connecting"); RunOnUiThread(() => tvShimmerState.Text = "Shimmer State: Connecting"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { System.Diagnostics.Debug.Write("Disconnected"); RunOnUiThread(() => tvShimmerState.Text = "Shimmer State: Disconnected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { System.Diagnostics.Debug.Write("Streaming"); RunOnUiThread(() => tvShimmerState.Text = "Shimmer State: Streaming"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = new ObjectCluster((ObjectCluster)eventArgs.getObject()); List <Double> data = objectCluster.GetData(); List <String> dataNames = objectCluster.GetNames(); String result = ""; String resultNames = ""; foreach (Double d in data) { result = d.ToString() + " " + result; } foreach (String s in dataNames) { resultNames = s + " " + resultNames; } System.Console.WriteLine(resultNames); System.Console.WriteLine(result); SensorData dataAccelX = objectCluster.GetData(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_X, "CAL"); RunOnUiThread(() => tvAccelX.Text = "AccelX: " + dataAccelX.Data); break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((ShimmerBluetooth)sender).GetDeviceName() + " State = " + ((ShimmerBluetooth)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { System.Console.WriteLine("Connected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { System.Console.WriteLine("Connecting"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { System.Console.WriteLine("Disconnected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { System.Console.WriteLine("Streaming"); System.Console.WriteLine("Data being written to exgtestsignal.csv"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = (ObjectCluster)eventArgs.getObject(); SensorData dataTS = objectCluster.GetData(IndexTimeStamp); //SensorData data = objectCluster.GetData(Shimmer3Configuration.SignalNames.EXG1_CH1_16BIT, "CAL"); //SensorData data = objectCluster.GetData(Shimmer3Configuration.SignalNames.EXG1_CH1, "CAL"); SensorData[] data = new SensorData[3]; data[0] = objectCluster.GetData(Shimmer3Configuration.SignalNames.ECG_LA_RA, "CAL"); data[1] = objectCluster.GetData(Shimmer3Configuration.SignalNames.ECG_LL_RA, "CAL"); data[2] = objectCluster.GetData(Shimmer3Configuration.SignalNames.ECG_VX_RL, "CAL"); ECGToHRAdaptive.DataECGToHROutputArray output = ECG_To_HR_and_RR.ecgToHrConversionWithRR(data, dataTS); System.Console.WriteLine("EXG Channel 1: " + data.Data); logging.WriteData(objectCluster); break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((ShimmerBluetooth)sender).GetDeviceName() + " State = " + ((ShimmerBluetooth)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { System.Console.WriteLine("Connected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { System.Console.WriteLine("Connecting"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { System.Console.WriteLine("Disconnected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { System.Console.WriteLine("Streaming"); System.Console.WriteLine("Data being written to ecg.csv"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = (ObjectCluster)eventArgs.getObject(); SensorData data = objectCluster.GetData(Shimmer3Configuration.SignalNames.ECG_LA_RA, "CAL"); if (data != null) { System.Console.Write(data.Data + ","); } logging.WriteData(objectCluster); break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { System.Console.WriteLine("Connected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { System.Console.WriteLine("Connecting"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { System.Console.WriteLine("Disconnected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { System.Console.WriteLine("Streaming"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = (ObjectCluster)eventArgs.getObject(); SensorData data = objectCluster.GetData("Low Noise Accelerometer X", "CAL"); System.Console.WriteLine("AccelX: " + data.Data); break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((ShimmerBluetooth)sender).GetDeviceName() + " State = " + ((ShimmerBluetooth)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: // this is essential to ensure the object is not a reference ObjectCluster objectCluster = new ObjectCluster((ObjectCluster)eventArgs.getObject()); ojc = objectCluster; ojcArray.Add(objectCluster); break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: // this is essential to ensure the object is not a reference ObjectClusterByteArray objectCluster = (ObjectClusterByteArray)eventArgs.getObject(); ojcArray.Add(objectCluster); break; } }
/// <summary> /// Blanket event handler for packets. Covers a number of events from Shimmer device. /// Includes code for marking connection status, notifications, and data packets. /// TODO: rework code to make cleaner. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)Shimmer.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: //System.Diagnostics.Debug.Write(((Shimmer)sender).GetDeviceName() + " State = " + ((Shimmer)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)Shimmer.SHIMMER_STATE_CONNECTED) { //labelConnectionState.Text = "Connected"; ChangeStatusLabel("Connected to " + ShimmerDevice.GetComPort() + ". Firmware Version: " + ShimmerDevice.GetFirmwareVersionFullName()); //ChangeStatusLabel("Connected"); //firstTimeTimer = new System.Windows.Forms.Timer(); //firstTimeTimer.Tick += new EventHandler(firstTimeCycleConnect); //firstTimeTimer.Interval = (int)(5000); //firstTimeTimer.Start(); } else if (state == (int)Shimmer.SHIMMER_STATE_CONNECTING) { //labelConnectionState.Text = "Connecting"; ChangeStatusLabel("Connecting"); } else if (state == (int)Shimmer.SHIMMER_STATE_NONE) { //labelConnectionState.Text = "Disconnected"; ChangeStatusLabel("Disconnected"); } else if (state == (int)Shimmer.SHIMMER_STATE_STREAMING) { //labelConnectionState.Text = "Streaming"; ChangeStatusLabel("Streaming"); if (firstTime) { } } break; case (int)Shimmer.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: // this is essential to ensure the object is not a reference DateTime timestamp = DateTime.Now; ObjectCluster objectCluster = new ObjectCluster((ObjectCluster)eventArgs.getObject()); List <String> names = objectCluster.GetNames(); List <String> formats = objectCluster.GetFormats(); List <String> units = objectCluster.GetUnits(); List <Double> data = objectCluster.GetData(); //add element to list of biometricClusters biometricCluster bc = new biometricCluster(); bc.currentTime = timestamp; bc.receivedCluster = objectCluster; storedClusterData.Add(bc); //print log to file writeLogToFile(bc); Console.WriteLine("Current Timestamp: " + timestamp); int gsrRawIndex = objectCluster.GetIndex("GSR", "RAW"); int gsrCalIndex = objectCluster.GetIndex("GSR", "CAL"); int adcRawIndex = objectCluster.GetIndex("Internal ADC A13", "RAW"); int adcCalIndex = objectCluster.GetIndex("Internal ADC A13", "CAL"); for (int i = 0; i < names.Count; i++) { //Console.WriteLine(names[i]); } //Console.WriteLine("Current Raw GSR Readings: " + data[gsrRawIndex]); //Console.WriteLine("Current Calculated GSR Readings: " + data[gsrCalIndex]); //Console.WriteLine("Current Raw PPG Readings: " + data[adcRawIndex]); //Console.WriteLine("Current Calculated PPG Readings: " + data[adcCalIndex]); //Console.WriteLine(); break; case (int)Shimmer.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: //rework this as function? string message = (string)eventArgs.getObject(); System.Diagnostics.Debug.Write(((Shimmer)sender).GetDeviceName() + message + System.Environment.NewLine); //Message BOX int minorIdentifier = eventArgs.getMinorIndication(); if (minorIdentifier == (int)ShimmerSDBT.ShimmerSDBTMinorIdentifier.MSG_WARNING) { MessageBox.Show(message, ConnectionTest.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (minorIdentifier == (int)ShimmerSDBT.ShimmerSDBTMinorIdentifier.MSG_EXTRA_REMOVABLE_DEVICES_DETECTED) { MessageBox.Show(message, "Message"); FolderBrowserDialog fbd = new FolderBrowserDialog(); DialogResult result = fbd.ShowDialog(); ShimmerDevice.SetDrivePath(fbd.SelectedPath); } else if (minorIdentifier == (int)ShimmerSDBT.ShimmerSDBTMinorIdentifier.MSG_ERROR) { MessageBox.Show(message, ConnectionTest.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (message.Equals("Connection lost")) { MessageBox.Show("Connection with device lost while streaming", ConnectionTest.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { ChangeStatusLabel(message); } break; default: break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { updateMessage("device connected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { updateMessage("connecting shimmer device"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { updateMessage("device disconnected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { updateMessage("Streaming"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = (ObjectCluster)eventArgs.getObject(); if (FirstTime) { IndexAccelX = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_X, ShimmerConfiguration.SignalFormats.CAL); IndexAccelY = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_Y, ShimmerConfiguration.SignalFormats.CAL); IndexAccelZ = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_Z, ShimmerConfiguration.SignalFormats.CAL); IndexGSR = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.GSR, ShimmerConfiguration.SignalFormats.CAL); IndexPPG = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.INTERNAL_ADC_A13, ShimmerConfiguration.SignalFormats.CAL); FirstTime = false; } // List<string> s= objectCluster.GetNames(); // foreach (string st in s) { // Debug.WriteLine(st); // } SensorData datax = objectCluster.GetData(IndexAccelX); SensorData datay = objectCluster.GetData(IndexAccelY); SensorData dataz = objectCluster.GetData(IndexAccelZ); SensorData dataGSR = objectCluster.GetData(IndexGSR); SensorData dataPPG = objectCluster.GetData(IndexPPG); //TimeSpan time = stopwatch.Elapsed; //double time = DateTime.Now.ToOADate(); double time = mts.GetTime() - startTime; ShimmerData dataObj = new ShimmerData(mts.DoubleToTimeString(time), dataGSR.Data, dataPPG.Data, datax.Data, datay.Data, dataz.Data); //Debug.WriteLine(dataObj.Time); updateData(dataObj); break; } }
public void HandleShimmerEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)Shimmer.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((Shimmer)sender).GetDeviceName() + " State = " + ((Shimmer)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)Shimmer.SHIMMER_STATE_CONNECTED) { AppendTextBox("Connected", ((Shimmer)sender).GetDeviceName()); ChangeStatusLabel("Connected to " + ((Shimmer)sender).GetComPort() + ". Firmware Version: " + ((Shimmer)sender).GetFirmwareVersionFullName(), ((Shimmer)sender).GetDeviceName()); EnableButtons((int)Shimmer.SHIMMER_STATE_CONNECTED, ((Shimmer)sender).GetDeviceName()); } else if (state == (int)Shimmer.SHIMMER_STATE_CONNECTING) { AppendTextBox("Connecting", ((Shimmer)sender).GetDeviceName()); ChangeStatusLabel("Connecting", ((Shimmer)sender).GetDeviceName()); EnableButtons((int)Shimmer.SHIMMER_STATE_CONNECTING, ((Shimmer)sender).GetDeviceName()); } else if (state == (int)Shimmer.SHIMMER_STATE_NONE) { AppendTextBox("Disconnected", ((Shimmer)sender).GetDeviceName()); ChangeStatusLabel("Disconnected", ((Shimmer)sender).GetDeviceName()); EnableButtons((int)Shimmer.SHIMMER_STATE_NONE, ((Shimmer)sender).GetDeviceName()); } else if (state == (int)Shimmer.SHIMMER_STATE_STREAMING) { AppendTextBox("Streaming", ((Shimmer)sender).GetDeviceName()); ChangeStatusLabel("Streaming", ((Shimmer)sender).GetDeviceName()); EnableButtons((int)Shimmer.SHIMMER_STATE_STREAMING, ((Shimmer)sender).GetDeviceName()); } break; case (int)Shimmer.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: string message = (string)eventArgs.getObject(); System.Diagnostics.Debug.Write(((Shimmer)sender).GetDeviceName() + message + System.Environment.NewLine); //Message BOX int minorIdentifier = eventArgs.getMinorIndication(); if (minorIdentifier == (int)ShimmerSDBT.ShimmerSDBTMinorIdentifier.MSG_WARNING) { MessageBox.Show(message, Control.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (minorIdentifier == (int)ShimmerSDBT.ShimmerSDBTMinorIdentifier.MSG_ERROR) { MessageBox.Show(message, Control.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (message.Equals("Connection lost")) { MessageBox.Show("Connection with device lost while streaming", Control.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { ChangeStatusLabel(message, ((Shimmer)sender).GetDeviceName()); } break; case (int)Shimmer.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: // this is essential to ensure the object is not a reference ObjectCluster objectCluster = new ObjectCluster((ObjectCluster)eventArgs.getObject()); List <String> names = objectCluster.GetNames(); List <String> formats = objectCluster.GetFormats(); List <String> units = objectCluster.GetUnits(); List <Double> data = objectCluster.GetData(); if (EnablePPGtoHRConversion) { int index = objectCluster.GetIndex(PPGSignalName, "CAL"); int indexts = objectCluster.GetIndex("Timestamp", "CAL"); if (index != -1) { double dataFilteredLP = LPF_PPG.filterData(data[index]); double dataFilteredHP = HPF_PPG.filterData(dataFilteredLP); double[] dataTS = new double[] { data[indexts] }; int heartRate = (int)Math.Round(PPGtoHeartRateCalculation.ppgToHrConversion(dataFilteredHP, dataTS[0])); names.Add("Heart Rate PPG"); formats.Add(""); units.Add("Beats/min"); data.Add(heartRate); } } if (_enableECGtoHRConversion) { //ECG-HR Conversion int index = -1; { index = objectCluster.GetIndex(ECGSignalName, "RAW"); } if (index != -1) { int hr = -1; double ecgData = -1; ecgData = data[index]; double calTimestamp = objectCluster.GetData("Timestamp", "CAL").GetData(); hr = (int)ECGtoHR.ECGToHRConversion(ecgData, calTimestamp); names.Add("Heart Rate ECG"); formats.Add(""); units.Add("Beats/min"); data.Add(hr); } } if (_firstTimeShimmer1 && ((Shimmer)sender).GetDeviceName() == "Shimmer1") { List <String> fnames = new List <String>(); int icount = 0; foreach (String s in names) { fnames.Add(s + " " + formats[icount] + " (" + units[icount] + ")"); icount++; } ShowChannelLabels(fnames, ((Shimmer)sender).GetDeviceName()); ShowChannelTextBoxes(fnames.Count, ((Shimmer)sender).GetDeviceName()); ShimmerIdSetup.Add(objectCluster.GetShimmerID()); _firstTimeShimmer1 = false; } if (_firstTimeShimmer2 && ((Shimmer)sender).GetDeviceName() == "Shimmer2") { List <String> fnames = new List <String>(); int icount = 0; foreach (String s in names) { fnames.Add(s + " " + formats[icount] + " (" + units[icount] + ")"); icount++; } ShowChannelLabels(fnames, ((Shimmer)sender).GetDeviceName()); ShowChannelTextBoxes(fnames.Count, ((Shimmer)sender).GetDeviceName()); ShimmerIdSetup.Add(objectCluster.GetShimmerID()); _firstTimeShimmer2 = false; } ShowTBcountShimmer1++; if (ShowTBcountShimmer1 % Math.Truncate(ShimmerDevice1.GetSamplingRate() / 5) == 0) { UpdateChannelTextBoxes(data, ((Shimmer)sender).GetDeviceName()); } ShowTBcountShimmer2++; if (ShowTBcountShimmer2 % Math.Truncate(ShimmerDevice2.GetSamplingRate() / 5) == 0) { UpdateChannelTextBoxes(data, ((Shimmer)sender).GetDeviceName()); } //Write to file if (_writeToFileShimmer1 != null && (_writeData || _writeBaseLineData) && ((Shimmer)sender).GetDeviceName() == "Shimmer1") { try { _writeToFileShimmer1.WriteData(objectCluster); } catch (Exception e) { MessageBox.Show(e.Message, Control.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning); throw; } } if (_writeToFileShimmer2 != null && (_writeData || _writeBaseLineData) && ((Shimmer)sender).GetDeviceName() == "Shimmer2") { try { _writeToFileShimmer2.WriteData(objectCluster); } catch (Exception e) { MessageBox.Show(e.Message, Control.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning); throw; } } break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((ShimmerBluetooth)sender).GetDeviceName() + " State = " + ((ShimmerBluetooth)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { System.Diagnostics.Debug.Write("Connected"); System.Console.WriteLine(count + " " + "Connected"); if (stream) { //Needs to be executed on a seperate thread, and a sleep to ensure everything on the current thread is completed new Thread(() => { Thread.Sleep(500); shimmer.StartStreaming(); }).Start(); } else { new Thread(() => { Thread.Sleep(500); shimmer.Disconnect(); }).Start(); } } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { System.Diagnostics.Debug.Write("Connecting"); System.Console.WriteLine(count + " " + "Connecting"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { System.Diagnostics.Debug.Write("Disconnected"); System.Console.WriteLine(count + " " + "Disconnected"); stream = true; new Thread(() => { Thread.Sleep(500); count++; shimmer.Connect(); }).Start(); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { System.Diagnostics.Debug.Write("Streaming"); System.Console.WriteLine(count + " " + "Streaming"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = (ObjectCluster)eventArgs.getObject(); SensorData data = objectCluster.GetData(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_X, "CAL"); if (lastknowncount != count) { System.Console.WriteLine(count + " " + "AccelX: " + data.Data); lastknowncount = count; stream = false; new Thread(() => { Thread.Sleep(500); shimmer.StopStreaming(); }).Start(); } break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((ShimmerBluetooth)sender).GetDeviceName() + " State = " + ((ShimmerBluetooth)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { System.Console.WriteLine("Shimmer is Connected"); Task ignoredAwaitableResult = this.delayedWork(); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { System.Console.WriteLine("Establishing Connection to Shimmer Device"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { System.Console.WriteLine("Shimmer is Disconnected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { System.Console.WriteLine("Shimmer is Streaming"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = (ObjectCluster)eventArgs.getObject(); if (FirstTime) { IndexAccelX = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_X, ShimmerConfiguration.SignalFormats.CAL); IndexAccelY = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_Y, ShimmerConfiguration.SignalFormats.CAL); IndexAccelZ = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.LOW_NOISE_ACCELEROMETER_Z, ShimmerConfiguration.SignalFormats.CAL); IndexGSR = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.GSR, ShimmerConfiguration.SignalFormats.CAL); IndexPPG = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.INTERNAL_ADC_A13, ShimmerConfiguration.SignalFormats.CAL); IndexTimeStamp = objectCluster.GetIndex(ShimmerConfiguration.SignalNames.SYSTEM_TIMESTAMP, ShimmerConfiguration.SignalFormats.CAL); FirstTime = false; } SensorData datax = objectCluster.GetData(IndexAccelX); SensorData datay = objectCluster.GetData(IndexAccelY); SensorData dataz = objectCluster.GetData(IndexAccelZ); SensorData dataGSR = objectCluster.GetData(IndexGSR); SensorData dataPPG = objectCluster.GetData(IndexPPG); SensorData dataTS = objectCluster.GetData(IndexTimeStamp); //Process PPG signal and calculate heart rate double dataFilteredLP = LPF_PPG.filterData(dataPPG.Data); double dataFilteredHP = HPF_PPG.filterData(dataFilteredLP); int heartRate = (int)Math.Round(PPGtoHeartRateCalculation.ppgToHrConversion(dataFilteredHP, dataTS.Data)); if (Count % SamplingRate == 0) //only display data every second { System.Console.WriteLine("AccelX: " + datax.Data + " " + datax.Unit + " AccelY: " + datay.Data + " " + datay.Unit + " AccelZ: " + dataz.Data + " " + dataz.Unit); System.Console.WriteLine("Time Stamp: " + dataTS.Data + " " + dataTS.Unit + " GSR: " + dataGSR.Data + " " + dataGSR.Unit + " PPG: " + dataPPG.Data + " " + dataPPG.Unit + " HR: " + heartRate + " BPM"); } Count++; break; } }
public void HandleEvent(object sender, EventArgs args) { CustomEventArgs eventArgs = (CustomEventArgs)args; int indicator = eventArgs.getIndicator(); switch (indicator) { case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_STATE_CHANGE: System.Diagnostics.Debug.Write(((ShimmerBluetooth)sender).GetDeviceName() + " State = " + ((ShimmerBluetooth)sender).GetStateString() + System.Environment.NewLine); int state = (int)eventArgs.getObject(); if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTED) { System.Console.WriteLine("Shimmer is Connected"); Task ignoredAwaitableResult = this.delayedWork(); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_CONNECTING) { System.Console.WriteLine("Establishing Connection to Shimmer Device"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_NONE) { System.Console.WriteLine("Shimmer is Disconnected"); } else if (state == (int)ShimmerBluetooth.SHIMMER_STATE_STREAMING) { System.Console.WriteLine("Shimmer is Streaming"); } break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE: break; case (int)ShimmerBluetooth.ShimmerIdentifier.MSG_IDENTIFIER_DATA_PACKET: ObjectCluster objectCluster = (ObjectCluster)eventArgs.getObject(); if (FirstTime) { IndexGSR = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.GSR, ShimmerConfiguration.SignalFormats.CAL); IndexPPG = objectCluster.GetIndex(Shimmer3Configuration.SignalNames.INTERNAL_ADC_A13, ShimmerConfiguration.SignalFormats.CAL); IndexTimeStamp = objectCluster.GetIndex(ShimmerConfiguration.SignalNames.SYSTEM_TIMESTAMP, ShimmerConfiguration.SignalFormats.CAL); FirstTime = false; } SensorData dataGSR = objectCluster.GetData(IndexGSR); SensorData dataTS = objectCluster.GetData(IndexTimeStamp); if (Count % SamplingRate == 0) //only display data every second { // LOG HR and GSR values System.IO.File.AppendAllText(logLocation + "dataGSR.txt", dataGSR.Data + Environment.NewLine); System.Console.WriteLine("Time Stamp: " + dataTS.Data + " " + dataTS.Unit + " GSR: " + dataGSR.Data + " " + dataGSR.Unit); if (Count < SamplingRate * initDelay) { // Before the end of 10s init delay System.Console.WriteLine(" - Data skipped (init delay)"); } else { double[] newDataPoint = new double[] { dataGSR.Data }; rawData[Convert.ToInt32((Count / SamplingRate) - initDelay) % kmeansDelay] = newDataPoint; if (Count > (kmeansDelay + initDelay + 1) * SamplingRate && curMeans != null) { // At least the first k means has to have been performed int stressLevel = findBestLabel(newDataPoint); System.IO.File.AppendAllText(logLocation + "stressLevelsLog.txt", stressLevel + Environment.NewLine); if (stressLevel != prevStressLvl) { prevStressLvl = stressLevel; writeToSocket(stressLevel); System.IO.File.WriteAllText(logLocation + "stressLevelStream.txt", stressLevel + Environment.NewLine); } } } if (Count >= (kmeansDelay + initDelay + 0.6) * SamplingRate && Convert.ToInt32((Count / SamplingRate) - initDelay) % kmeansDelay == 0) { // Run kmeans every 'kmeansDelay' seconds to update means System.Console.WriteLine("Running new K-means"); (curClustering, curMeans) = Cluster(rawData, numClusters); Array.Sort(curMeans, (v1, v2) => v1[0] > v2[0] ? 1 : v2[0] < v1[0] ? -1 : 0); //Display clusters means for (int i = 0; i < curMeans.Length; i++) { System.Console.Write("Cluster " + i + "\t"); for (int j = 0; j < curMeans[i].Length; j++) { System.Console.Write(curMeans[i][j] + "\t"); } System.Console.Write("\n"); } } } Count++; break; } }