public string[,] GetEmittersDescription() { Resource_MultiChannel mresource = device.ActiveDeviceDescription.Resource as Resource_MultiChannel; string[,] emitters = new string[mresource.EmitterFactory.EmitterCount, 6]; foreach (EmitterDescription emm in mresource.EmitterFactory) { emitters[emm.HardwareIndex, 0] = emm.HardwareIndex.ToString(); emitters[emm.HardwareIndex, 1] = emm.UID.ToString(); emitters[emm.HardwareIndex, 2] = emm.BytesPerSample.ToString(); emitters[emm.HardwareIndex, 3] = emm.HardHighPass.ToString(); emitters[emm.HardwareIndex, 4] = emm.HardLowPass.ToString(); emitters[emm.HardwareIndex, 5] = emm.UnitsPerSample.ToString(); } return(emitters); }
private void InputTask() { if (device != null) { updateBatteryPercent(); SynchronizationContext context = SynchronizationContext.Current; Resource_MultiChannel mresource = device.ActiveDeviceDescription.Resource as Resource_MultiChannel; int ChannelCount = mresource.EmitterFactory.EmitterCount; int TickCount = mresource.DataBuffer.TicksMax; InputSupport = new ThreadSupport(device, ChannelCount, TickCount); InputSupport.ThreadSupportTransformData += TransformData; InputSupport.ThreadSupportReseiveData += ReseiveData; InputSupport.ThreadSupportEmitterData += EmitterData; InputSupport.Start(); //Waiting Task to complete InputSupport.Wait(); } }
private void Run() { Terminated = false; while (stop == false) { string tmp = ""; bool Result = device.Scan(); //Returns true if buffer contains valid number of tics if (device.HALError.Code != HALErrorCode.OK) { break; //Error encountered } if (Result == true) { Resource_MultiChannel Resource = device.ActiveDeviceDescription.Resource as Resource_MultiChannel; //Receiveing Data if (device.Grab() == true) {// if (device.HALError.CoreCode == HALCoreErrorCode.TickMiss) { device.HALError.Assign(device.ActiveDeviceDescription, HALErrorCode.OK); // Code = HALErrorCode.OK; device.HALError.AssignCoreCode(HALCoreErrorCode.OK); //TickMiss } //Remember number of ticks in input buffer, those was read from FIFO to DataOut int TicksReceived = Resource.DataBuffer.TicksReceived; int ChannelCount = Resource.InputHardwareChannelCountExternal; int time_on_bus = 0; data = new int[ChannelCount, TicksReceived]; //process each channel in input, linked with current datasource foreach (EmitterDescription em in Resource.EmitterFactory) { //Getting index of emitter channel in Data Out buffer int HardwareIndex = em.HardwareIndex; //Console.WriteLine(em.UID.ToString() + ' ' + em.HardwareIndex); tmp += em.UID.ToString() + ' ' + em.HardwareIndex + newLine; ThreadSupportEmitterData?.Invoke(this, tmp); tmp = ""; if (HardwareIndex < 0) { continue; } for (long i = 0; i < TicksReceived; i++) { //Сырые данные полученные с прибора в единицах АЦП. int value = Resource.DataBuffer.DataOut[i * ChannelCount + HardwareIndex]; if (em.UID == UID.AD0) { if (value > 0) { UID LogicalUID = (UID)((value & _UIDMask) >> 16); double Rvalue = (double)(value & _RMask) / 10.0; //Attributes bool Active = ((value & (uint)ImpedanceElementAttributes.Active) > 0) ? true : false; bool HardwareReferent = ((value & (uint)ImpedanceElementAttributes.Referent) > 0) ? true : false; Console.WriteLine(em.UID.ToString() + " UID " + LogicalUID.ToString() + " Rvalue " + Rvalue + " Active " + Active + " HardwareReferent " + HardwareReferent); } } else if (em.UID == UID.AD1) { if (value > 0) { /* UID.AD1 - Канал акселерометра.Значения датчиков положения тела. byte XAxis; byte YAxis; * byte ZAxis;byte Reserved; */ byte[] ad1bytes = BitConverter.GetBytes(value); int x = ad1bytes[0]; int y = ad1bytes[1]; int z = ad1bytes[2]; Console.WriteLine(em.UID.ToString() + " x " + x + " y " + y + " z " + z); } } else { //Преобразуем значение в единицах АЦП в значение в микровольтах умножая на коэффициент квантования. float fValue = value * em.UnitsPerSample; int fValueInt = (int)fValue; //Преобразовываем в думерный массив data[em.HardwareIndex, i] = fValueInt; } } time_on_bus += TicksReceived; } ThreadSupportTransformData?.Invoke(data); ThreadSupportReseiveData?.Invoke(this, new EEGReseiveDataEventArgs((int[, ])data.Clone(), time_on_bus)); } } else { Thread.Sleep(1); } } Terminated = true; }
/// <summary> /// Starts input /// </summary> /// <param name="device">Device object</param> /// <param name="ProtocolClass">Class of device to operate with</param> /// <param name="DataType">Type of data to input</param> /// <param name="InputTask">Data processing task</param> /// <param name="ErrorMessage">Error</param> /// <returns>Returns true if device was succesfully started and stopped </returns> public static bool Launch(DeviceInput device, ProtocolClassID ProtocolClass, string PrefferedSerialNumber, InputDataType DataType, DeviceInputTask InputTask, ref string ErrorMessage) { if (Running == true) { return(false); } try { Running = true; if (device == null) { ErrorMessage = "Device is null"; return(false); } #region Getting library versions //Getting HAL library Version information int Version = 0, Release = 0; device.GetHALVersion(ref Version, ref Release); Console.WriteLine("Mitsar.HAL version: {0}.{1}", Version, Release); Console.WriteLine(); Console.WriteLine("Assemblies_____________"); Console.WriteLine(GetAssemblyDescription("Mitsar.Essentials")); Console.WriteLine(GetAssemblyDescription("Mitsar.HAL")); Console.WriteLine(); #endregion #region Getting available hardware and selecting device DeviceDescription SelectedDescription = null; //Detecting Hardware List <DeviceDescription> descriptions = new List <DeviceDescription>(); Console.WriteLine("Enumerating devices for protocol " + ProtocolClass); device.Enumerate(HardwareClassID.EEG, ProtocolClass, ref descriptions); //Showing all discovered devices if (descriptions != null) { Console.WriteLine("Devices found ______________"); foreach (DeviceDescription desc in descriptions) { Console.WriteLine(desc.ToString()); Console.WriteLine("Driver Name: " + desc.GetDeviceDriverName()); Console.WriteLine("Driver SerialNumber: " + desc.DriverSerialNumber); Console.WriteLine("State: " + desc.State.ToString()); Console.WriteLine(); } } else { ErrorMessage = "No devices found"; return(false); } Console.WriteLine("________________________"); //Selecting device description if (descriptions.Count == 0) { ErrorMessage = "Error: No devices found"; return(false); } else { if (string.IsNullOrEmpty(PrefferedSerialNumber) == false) { Console.Write("Searching for device with serial number " + PrefferedSerialNumber + "..."); //Choosing EEG Device foreach (DeviceDescription desc in descriptions) { if (desc.DriverSerialNumber == PrefferedSerialNumber) { SelectedDescription = desc; break; } } if (SelectedDescription != null) { Console.WriteLine(" found " + SelectedDescription.ToString()); } else { Console.WriteLine(" not found"); } } if (SelectedDescription == null) { Console.Write("Searching for any device..."); //Choosing EEG Device foreach (DeviceDescription desc in descriptions) { if (desc.HardwareClass == HardwareClassID.EEG) { SelectedDescription = desc; break; } } if (SelectedDescription != null) { Console.WriteLine(" found " + SelectedDescription.ToString()); } else { Console.WriteLine(" not found"); } } if (SelectedDescription == null) { ErrorMessage = "Error: No devices found"; return(false); } } #endregion #region Opening, Powering and detecting device version //Opening HAL Console.Write("Opening..."); if (device.Open(SelectedDescription) == true) { Console.WriteLine("OK"); } else { return(false); } //Powering HAL Console.Write("Powering..."); if (device.PowerOn() == true) { Console.WriteLine("OK"); } else { return(false); } //Identifying HAL Console.Write("Indentifing..."); if (device.Identify() == true) { Console.WriteLine("OK"); } else { return(false); } if (device.ActiveDeviceDescription.Resource.Version == 0) { ErrorMessage = "Error: Unknown version " + device.ActiveDeviceDescription.Resource.ReservedVersion; return(false); } Console.WriteLine(); Console.WriteLine("Device vendor: " + device.ActiveDeviceDescription.Resource.Vendor); Console.WriteLine("Device version: " + device.ActiveDeviceDescription.Resource.Version); Console.WriteLine("Device serial number: " + device.ActiveDeviceDescription.Resource.SerialNumber); #endregion #region Loading and setting up calibration Console.Write("Getting calibration..."); Resource_MultiChannel mresource = device.ActiveDeviceDescription.Resource as Resource_MultiChannel; //If internal device calibration is supported if (mresource.IsROMSupported() == true) {//ROM supported - reading from ROM byte[] buf = null; //Reading calibration data from device's memory to buffer if (device.ReadMemory(ref buf) == true) {//if ROM read successfully - transfer to calibration //Decoding buffer to calibtaion data CalibrationProfileIOResult result = CalibrationProfile.ReadFromBuffer(ref device.ActiveDeviceDescription.Calibration, mresource, buf); if (result == CalibrationProfileIOResult.OK) //Translate OK { //After successfull read version and serial must be assigned to CalibrationFile device.ActiveDeviceDescription.Calibration.Parameters.Version = device.ActiveDeviceDescription.Resource.Version; device.ActiveDeviceDescription.Calibration.Parameters.FullSerialNumber = device.ActiveDeviceDescription.Resource.SerialNumber; //Serial is not validated because received from already validated device resource Console.WriteLine("OK"); } else //ROM translate or check sum error { device.ActiveDeviceDescription.Calibration.Clear(); } }//ROM Access error else { device.ActiveDeviceDescription.Calibration.Clear(); } }//if ROM supported else { Console.WriteLine(" not supported"); } Console.WriteLine(""); //else calibration must be read from calibration file eeg.cal //CalibrationFile.Read(ref device.ActiveDeviceDescription.Calibration, "eeg.cal"); #endregion #region Configuring device //Setting input mode to global input mode. EEG(data) or Impedance mresource.DataType = DataType; //Test signal mode disabled mresource.TestSignal = false; //Setting up referent operation mode //Getting list of supported referent types List <ReferentOperationMode> RefModeList = mresource.GetReferentOperationModeList(false); Console.Write("Supported referents: "); foreach (ReferentOperationMode refmode in RefModeList) { Console.Write(refmode + " "); } Console.WriteLine(); //Selecting first supported mode //mresource.EmitterFactory.RefMode = ReferentOperationMode.RefElectrode; //mresource.EmitterFactory.RefMode = ReferentOperationMode.Joined; //RefModeList[0]; mresource.EmitterFactory.RefMode = RefModeList[0]; mresource.EmitterFactory.ImpedanceChannelEnabled = true; mresource.EmitterFactory.AccelerometerChannelEnabled = true; //Setting up sampling frequency //GEtting list of supported sampling frequencies List <double> FreqList = mresource.GetSamplingFrequencyList(false); Console.Write("Supported frequencies: "); foreach (double freq in FreqList) { Console.Write(freq + " "); } Console.WriteLine(); //Selecting first frequency in list, generally nominal //int SamplingFrequency = (int)mresource.GetNominalSamplingFrequency(); int SamplingFrequency = (int)FreqList[0]; mresource.EmitterFactory.SamplingFrequency = SamplingFrequency; //Setting other data buffer parameters //Getting BYTES PER SAMPLE FROM FIRST ENCOUNTERED DATA CHANNEL mresource.DataBuffer.BytesPerTick = mresource.Interface_GetBytesPerSample(UID.NOP); mresource.DataBuffer.TicksMin = 2; mresource.DataBuffer.TicksMax = 100; mresource.DataBuffer.TicksSkip = 10;// 10; //Enable Front panel LED in Impedace Test mode mresource.FrontLedEnabled = true; mresource.FrontLedThesholdEnabled = false; //Set edge value, can be changed in any time //All impedances above this value will be highligted //Only 5, 10, 20, 40 values are indicated mresource.FrontLedThesholdValue = 10; Console.WriteLine(); #endregion #region Starting data input Console.Write("Starting..."); if (device.Start() == true) { Console.WriteLine("OK"); } else { return(false); } Console.WriteLine("Emitters__________________"); StringBuilder sb = new StringBuilder(); sb.AppendLine("UID\tIndex\tAlign\tBPS\tHigh\tLow\tNotch\tUnitsPerSample"); foreach (EmitterDescription emm in mresource.EmitterFactory) { sb.Append(emm.UID.ToString()); sb.Append("\t"); sb.Append(emm.HardwareIndex.ToString()); sb.Append("\t"); sb.Append(emm.Align.ToString()); sb.Append("\t"); sb.Append(emm.BytesPerSample.ToString()); sb.Append("\t"); sb.Append(emm.HardHighPass.ToString()); sb.Append("\t"); sb.Append(emm.HardLowPass.ToString()); sb.Append("\t"); sb.Append(emm.HardNotch.ToString()); sb.Append("\t"); sb.Append(emm.UnitsPerSample.ToString()); sb.AppendLine("\t"); } Console.WriteLine(sb.ToString()); Console.WriteLine("Calibration_____________"); //sb.Clear(); foreach (EmitterDescription emm in mresource.EmitterFactory) { sb.Append(emm.UID.ToString()); sb.Append("\t"); if (emm.CalEntryLink != null) { sb.Append(emm.CalEntryLink.ToString()); sb.Append("\t"); } else { sb.Append(" no calibration"); } sb.AppendLine("\t"); } Console.WriteLine(sb.ToString()); Console.WriteLine("Input parameters_____________"); Console.WriteLine("Data Type: " + mresource.DataType.ToString()); Console.WriteLine("Sampling Frequency: " + mresource.EmitterFactory.SamplingFrequency.ToString()); Console.WriteLine("BytesPerTick: " + mresource.DataBuffer.BytesPerTick.ToString()); Console.WriteLine("Minimum ticks to transmit: " + mresource.DataBuffer.TicksMin.ToString()); Console.WriteLine("Maximum ticks to transmit: " + mresource.DataBuffer.TicksMax.ToString()); Console.WriteLine("Internal number of channels: " + mresource.InputHardwareChannelCountInternal.ToString()); Console.WriteLine("External number of channels: " + mresource.InputHardwareChannelCountExternal.ToString()); Console.WriteLine(); #endregion //Start Data asquisition task Console.WriteLine("Executing input task..."); if (InputTask != null) { InputTask(); } bool DisplayImpedances = false; #region Displaying impedances if (DisplayImpedances) { Console.WriteLine("----- EmitterFactory -----"); foreach (EmitterDescription em in mresource.EmitterFactory) { ///Console.WriteLine(em.UID + "\t RF=" + em.ImpedancePreliminaryValue + "\t R=" + em.ImpedanceResultValue); Console.WriteLine(em.UID + "\t RValue=" + em.ImpedanceResultValue);//;;em.ImpedancePreliminaryValue + "\t R=" + em.ImpedanceResultValue); } Console.WriteLine("----- ImpedanceArray -----"); foreach (ImpedanceElement el in mresource.ImpedanceArray) { Console.WriteLine(el.LogicalUID + // " Name= " + el.Name + // " Active:" + el.Active + // " InMontage:" + el.PresentInMontage + //" REF:" + el.HardwareReferent + // " LED:" + el.LedState + " (" + el.LedIndex + ")" + // " PVal:" + el.PreliminaryValue + "\t RValue:" + el.ResultValue); } } #endregion #region Displaying power mode Console.WriteLine("Power mode: " + device.ActiveDeviceDescription.Resource.PowerMode); #endregion Console.WriteLine("Complete"); return(true); } finally { //Memorizing error code HALErrorCode errorcode = device.HALError.Code; Running = false; #region Finalising device if (device != null) { if (device.IsStarted) { Console.Write("Stopping..."); if (device.Stop() == true) { Console.WriteLine("OK"); } } if (device.IsPowered) { Console.Write("Powering off..."); if (device.PowerOff() == true) { Console.WriteLine("OK"); } } if (device.IsOpened) { Console.Write("Closing..."); if (device.Close() == true) { Console.WriteLine("OK"); } } if (errorcode != HALErrorCode.OK) { ErrorMessage = "Error: " + errorcode.ToString(); } } #endregion } }//Launch
private void Run() { Terminated = false; while (stop == false) { string tmp = ""; bool Result = device.Scan(); //Returns true if buffer contains valid number of tics if (device.HALError.Code != HALErrorCode.OK) { break; //Error encountered } if (Result == true) { Resource_MultiChannel Resource = device.ActiveDeviceDescription.Resource as Resource_MultiChannel; //Receiveing Data if (device.Grab() == true) {// if (device.HALError.CoreCode == HALCoreErrorCode.TickMiss) { device.HALError.Assign(device.ActiveDeviceDescription, HALErrorCode.OK); // Code = HALErrorCode.OK; device.HALError.AssignCoreCode(HALCoreErrorCode.OK); //TickMiss } //Remember number of ticks in input buffer, those was read from FIFO to DataOut int TicksReceived = Resource.DataBuffer.TicksReceived; int ChannelCount = Resource.InputHardwareChannelCountExternal; int time_on_bus = 0; data = new int[29, TicksReceived]; //process each channel in input, linked with current datasource foreach (EmitterDescription em in Resource.EmitterFactory) { //Getting index of emitter channel in Data Out buffer int HardwareIndex = em.HardwareIndex; //Console.WriteLine(em.UID.ToString() + ' ' + em.HardwareIndex); tmp += em.UID.ToString() + ' ' + em.HardwareIndex + newLine; ThreadSupportEmitterData?.Invoke(this, tmp); tmp = ""; if (HardwareIndex < 0) { continue; } for (long i = 0; i < TicksReceived; i++) { //Сырые данные полученные с прибора в единицах АЦП. int value = Resource.DataBuffer.DataOut[i * ChannelCount + HardwareIndex]; if (em.UID.ToString() == "AD0") { if (value != 0) { ImpedanceElement el = new ImpedanceElement(value); ad0Dictionary[el.LogicalUID] = (int)el.ResultValue; //Console.WriteLine(value.ToString("X8") + " " + el.ToString()); } } else if (em.UID.ToString() == "AD1") { if (value != 0) { /* UID.AD1 - Канал акселерометра.Значения датчиков положения тела. byte XAxis; byte YAxis; * byte ZAxis;byte Reserved; */ byte[] ad1bytes = BitConverter.GetBytes(value); int[] aclrmtr_arr = new int[] { ad1bytes[0], ad1bytes[1], ad1bytes[2] }; //Console.WriteLine(em.UID.ToString() + " x " + x + " y " + y + " z " + z); ThreadSupportAccelerometerData?.Invoke(this, aclrmtr_arr); } } else { //Преобразуем значение в единицах АЦП в значение в микровольтах умножая на коэффициент квантования. float fValue = value * em.UnitsPerSample; int fValueInt = (int)fValue; //if (fValue <0 ) // Console.WriteLine(em.UID.ToString() + ' ' + fValueInt); //Преобразовываем в думерный массив //data[em.HardwareIndex, i] = fValueInt; switch (em.UID) { case UID.FP1: data[0, i] = fValueInt; break; case UID.F3: data[1, i] = fValueInt; break; case UID.C3: data[2, i] = fValueInt; break; case UID.P3: data[3, i] = fValueInt; break; case UID.O1: data[4, i] = fValueInt; break; case UID.F7: data[5, i] = fValueInt; break; case UID.T3: data[6, i] = fValueInt; break; case UID.T5: data[7, i] = fValueInt; break; case UID.FZ: data[8, i] = fValueInt; break; case UID.PZ: data[9, i] = fValueInt; break; case UID.A1: data[10, i] = fValueInt; break; case UID.FP2: data[11, i] = fValueInt; break; case UID.F4: data[12, i] = fValueInt; break; case UID.C4: data[13, i] = fValueInt; break; case UID.P4: data[14, i] = fValueInt; break; case UID.O2: data[15, i] = fValueInt; break; case UID.F8: data[16, i] = fValueInt; break; case UID.T4: data[17, i] = fValueInt; break; case UID.T6: data[18, i] = fValueInt; break; case UID.COMPLEX1: data[19, i] = fValueInt; break; case UID.CZ: data[20, i] = fValueInt; break; case UID.COMPLEX2: data[21, i] = fValueInt; break; case UID.BIO1: data[22, i] = fValueInt; data[23, i] = fValueInt; data[24, i] = fValueInt; data[25, i] = fValueInt; data[26, i] = fValueInt; data[27, i] = fValueInt; data[28, i] = fValueInt; break; } } } time_on_bus += TicksReceived; } ThreadSupportAD0Data(this, ad0Dictionary); ThreadSupportTransformData?.Invoke(data); ThreadSupportReseiveData?.Invoke(this, new EEGReseiveDataEventArgs((int[, ])data.Clone(), time_on_bus)); } } else { Thread.Sleep(1); } } Terminated = true; }