public void Start(System.Threading.CancellationToken token) { const int segmentSize = 48; const int chunkSize = (4096 / segmentSize) * 1024; const int accelBytes = 12; const int gyroBytes = 20; const int rtcBytes = 8; const int cpuBytes = 8; Span <byte> data = new Span <byte>(new byte[segmentSize]); Span <byte> accelSegment = data.Slice(0, accelBytes); Span <byte> gyroSegment = data.Slice(accelBytes, gyroBytes); Span <byte> rtcSegment = data.Slice(accelBytes + gyroBytes, rtcBytes); Span <byte> cpuSegment = data.Slice(accelBytes + gyroBytes + rtcBytes, cpuBytes); byte[] array = data.ToArray(); Task dataHandler = null; try { using (MemoryStream stream = new MemoryStream()) { while (!token.IsCancellationRequested) { if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } for (int i = 0; i < chunkSize; i++) { _accelerometerDevice.Read(accelSegment); stream.Write(accelSegment); //_gyroscopeDevice.AquireData(gyroSegment); stream.Write(gyroSegment); //GetCurrentDate(rtcSegment); stream.Write(rtcSegment); // GetCpuTemp(cpuSegment); stream.Write(cpuSegment); DataSetCounter++; } array = stream.ToArray(); if (dataHandler != null) { dataHandler.Wait(); } dataHandler = WriteData(array); } } } catch (OperationCanceledException) { } }
public void Start(System.Threading.CancellationToken token) { const int accelBytes = 12; const int gyroBytes = 20; const int rtcBytes = 8; const int cpuBytes = 8; Span <byte> accelSegment = new Span <byte>(new byte[accelBytes]); Span <byte> gyroSegment = new Span <byte>(new byte[gyroBytes]); Span <byte> rtcSegment = new Span <byte>(new byte[rtcBytes]); Span <byte> cpuSegment = new Span <byte>(new byte[cpuBytes]); try { while (!token.IsCancellationRequested) { if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } _accelerometerDevice.Read(accelSegment); stream.Write(accelSegment); //_gyroscopeDevice.AquireData(gyroSegment); stream.Write(gyroSegment); //GetCurrentDate(rtcSegment); stream.Write(rtcSegment); // GetCpuTemp(cpuSegment); stream.Write(cpuSegment); DataSetCounter++; stream.Flush(); } } catch (OperationCanceledException) { stream.Close(); } }