public void HandleData(object sender, PacketReceivedEventArgs args) { if(args.Version != 3) { Console.WriteLine("Unsupport raw data output version: " + args.Version); return; } var timestamp = new DateTime(1970, 1, 1, 0, 0, 0, 0) + TimeSpan.FromSeconds(args.Timestamp); if(args.DataType == Utility.EDataType.Event) { if(EventReceived != null) { var eventArgs = new EventHandlerEventArgs { Timestamp = timestamp, TimestampSubsecond = args.TimestampSubsecond, EventType = BitConverter.ToUInt32(args.Data, 1) }; } return; } if(args.DataType == Utility.EDataType.SliceEnd) { var sliceArgs = new SliceHandlerEventArgs { Slice = _slice }; if(SliceReceived != null) SliceReceived(this, sliceArgs); _slice = new Slice(); return; } if(args.DataType == Utility.EDataType.Waveform) { for(int i=1; i <= 256*2; i += 2) { Int16 intValue = BitConverter.ToInt16(args.Data, i); double realValue = (double)(intValue*315) / (double)0x8000; _slice.Data.Add(realValue); } // todo: consider filtering 60Hz interference out return; } if(args.DataType == Utility.EDataType.FrequencyBins) { for(int i=0; i < 8; ++i) { UInt16 intValue = BitConverter.ToUInt16(args.Data, 1 + (i*2)); double realValue = (double)intValue / (double)0x8000; _slice.FrequencyBins[i] = realValue; } return; } if(args.DataType == Utility.EDataType.BadSignal) { _slice.BadData = BitConverter.ToUInt32(args.Data, 1); return; } if(args.DataType == Utility.EDataType.SleepStage) { _slice.SleepStage = BitConverter.ToUInt32(args.Data, 1); return; } if(args.DataType == Utility.EDataType.Impedance) { UInt32 intValue = BitConverter.ToUInt32(args.Data, 1); UInt32 inPhase = (intValue & 0x0000ffff) - 0x00008000; UInt32 quadrature = ((intValue & 0xffff0000) >> 16) - 0x00008000; if(inPhase != 0x7FFF) // 32767 indicates the impedance is bad { double impSquared = (inPhase * inPhase) + (quadrature * quadrature); _slice.Impedance = Math.Sqrt(impSquared); } return; } if(args.DataType == Utility.EDataType.SQI) { _slice.SQI = BitConverter.ToUInt32(args.Data, 1); return; } }
public Parser() { _slice = new Slice(); }