Exemple #1
0
        public void Start(System.Threading.CancellationToken token)
        {
            const int segmentSize = 60;
            const int chunkSize   = (4096 / segmentSize) * 1024;
            const int accelBytes  = 24;
            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  gyroCounter = 0;

            const int gyroTrigger = 200;


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

                        for (int i = 0; i < chunkSize; i++)
                        {
                            _accelerometerDevice.GetScaledValues(accelSegment);
                            stream.Write(accelSegment);

                            if (gyroCounter++ >= gyroTrigger)
                            {
                                _gyroscopeDevice.AcquireData(gyroSegment);
                                gyroCounter = 0;
                            }
                            else
                            {
                                gyroSegment.Fill(0);
                            }
                            stream.Write(gyroSegment);

                            GetCurrentDate(rtcSegment);
                            stream.Write(rtcSegment);

                            GetCpuTemp(cpuSegment);
                            stream.Write(cpuSegment);

                            DataSetCounter++;

                            if (!shown)
                            {
                                Console.WriteLine($"{string.Join(',', accelSegment.ToArray())}\t{BitConverter.ToDouble(accelSegment.ToArray(), 0)}\t{BitConverter.ToDouble(accelSegment.ToArray(), 8)}\t{BitConverter.ToDouble(accelSegment.ToArray(), 16)}");
                                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.WriteTo(fs);
                        fs.Flush();
                    }
                }
            }
            catch (OperationCanceledException)
            {
                fs.Close();
            }
        }
        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();
            }
        }