Ejemplo n.º 1
0
        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);

            bool shown = false;
            int  secondaryDataCounter = 0;

            const int secondaryDataTrigger = 200;

            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);

                            if (secondaryDataCounter++ >= secondaryDataTrigger)
                            {
                                //_gyroscopeDevice.AquireData(gyroSegment);
                                _rtcDevice.GetCurrentDate(rtcSegment);
                                GetCpuTemp(cpuSegment);
                                secondaryDataCounter = 0;
                            }
                            else
                            {
                                gyroSegment.Fill(0);
                                rtcSegment.Fill(0);
                                cpuSegment.Fill(0);
                            }

                            stream.Write(gyroSegment);

                            stream.Write(rtcSegment);

                            stream.Write(cpuSegment);

                            DataSetCounter++;

                            if (!shown)
                            {
                                Console.WriteLine($"{string.Join(',', accelSegment.ToArray())}\t{BitConverter.ToInt32(accelSegment.ToArray(), 0)}\t{BitConverter.ToInt32(accelSegment.ToArray(), 4)}\t{BitConverter.ToInt32(accelSegment.ToArray(), 8)}");
                                Console.WriteLine(string.Join(',', gyroSegment.ToArray()));
                                Console.WriteLine(string.Join(',', rtcSegment.ToArray()));
                                Console.WriteLine(string.Join(',', cpuSegment.ToArray()));
                                shown = true;
                            }

                            accelSegment.Clear();
                            gyroSegment.Clear();
                            rtcSegment.Clear();
                            cpuSegment.Clear();
                        }

                        stream.Position = 0;
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
        }
        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> 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]);

            bool shown = false;

            //int secondaryDataCounter = 0;

            //const int secondaryDataTrigger = 200;


            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    while (!token.IsCancellationRequested)
                    {
                        if (token.IsCancellationRequested)
                        {
                            token.ThrowIfCancellationRequested();
                        }

                        for (int i = 0; i < chunkSize; i++)
                        {
                            //clear before getting new data to ensure integrity
                            accelSegment.Clear();
                            _accelerometerDevice.Read(accelSegment);
                            stream.Write(accelSegment);

                            //if (secondaryDataCounter++ >= secondaryDataTrigger)
                            //{
                            gyroSegment.Clear();
                            _gyroscopeDevice.AcquireData(gyroSegment);

                            rtcSegment.Clear();
                            _rtcDevice.GetCurrentDate(rtcSegment);

                            cpuSegment.Clear();
                            GetCpuTemp(cpuSegment);

                            //    secondaryDataCounter = 0;
                            //}

                            stream.Write(gyroSegment);

                            stream.Write(rtcSegment);

                            stream.Write(cpuSegment);

                            DataSetCounter++;

                            if (!shown)
                            {
                                Console.WriteLine($"{string.Join(',', accelSegment.ToArray())}\t{BitConverter.ToInt32(accelSegment.ToArray(), 0)}\t{BitConverter.ToInt32(accelSegment.ToArray(), 4)}\t{BitConverter.ToInt32(accelSegment.ToArray(), 8)}");
                                Console.WriteLine(string.Join(',', gyroSegment.ToArray()));
                                Console.WriteLine(string.Join(',', rtcSegment.ToArray()));
                                Console.WriteLine(string.Join(',', cpuSegment.ToArray()));
                                shown = true;
                            }
                        }
                        stream.WriteTo(fs);
                        fs.Flush();
                        stream.Position = 0;
                    }
                }
            }
            catch (OperationCanceledException)
            {
                fs.Close();
            }
        }