コード例 #1
0
ファイル: Bme280.cs プロジェクト: Gravicode/TinyCLR.Drivers
        public void StartUpdating(
            Oversample temperatureSampleCount = Oversample.OversampleX8,
            Oversample pressureSampleCount    = Oversample.OversampleX8,
            Oversample humiditySampleCount    = Oversample.OversampleX1,
            int standbyDuration = 1000)
        {
            // TODO: for standby durations of 1,000ms and less, the sensor
            // will actually handle the reading loop. you put it into `Normal`
            // mode and set it to one of the known `StandbyDuration`s.
            //
            // So perhaps this should be an option. From a method signature
            // standpoint, i think that we would add an overload that took
            // a known `StandbyDuration` instead of an int.
            //
            // With that said, however, as far as I can tell, the sensor won't
            // send an interrupt when a new reading is taken, so i'm not sure
            // how we would synchronize with it, since the time that each read
            // takes is determined by the samples, filter, etc. -b
            //
            // TODO: for longer standby durations, we should put the sensor into
            // Modes.Sleep to save power. Need to figure out what the stanby
            // duration threshold is for that. i'm guessing 5 seconds might be a
            // good value.


            // thread safety
            lock (_lock) {
                if (IsSampling)
                {
                    return;
                }

                // state muh-cheen
                IsSampling = true;

                //SamplingTokenSource = new CancellationTokenSource();
                //CancellationToken ct = SamplingTokenSource.Token;
                if (UpdateData == null)
                {
                    UpdateData = new TempUpdateData();
                }
                UpdateData.humiditySampleCount    = humiditySampleCount;
                UpdateData.temperatureSampleCount = temperatureSampleCount;
                UpdateData.pressureSampleCount    = pressureSampleCount;
                UpdateData.standbyDuration        = standbyDuration;
                Thread task1 = new Thread(new ThreadStart(UpdateTemp));
                task1.Start();
            }
        }
コード例 #2
0
ファイル: Bme280.cs プロジェクト: Gravicode/TinyCLR.Drivers
        /// <summary>
        /// Convenience method to get the current sensor readings. For frequent reads, use
        /// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
        /// </summary>
        /// <param name="temperatureSampleCount">The number of sample readings to take.
        /// Must be greater than 0. These samples are automatically averaged.</param>
        public AtmosphericConditions Read(
            Oversample temperatureSampleCount = Oversample.OversampleX8,
            Oversample pressureSampleCount    = Oversample.OversampleX8,
            Oversample humiditySampleCount    = Oversample.OversampleX8)
        {
            // update confiruation for a one-off read
            configuration.TemperatureOverSampling = temperatureSampleCount;
            configuration.PressureOversampling    = pressureSampleCount;
            configuration.HumidityOverSampling    = humiditySampleCount;
            configuration.Mode   = Modes.Forced;
            configuration.Filter = FilterCoefficient.Off;
            UpdateConfiguration(configuration);

            this.Conditions = Read();

            return(Conditions);
        }
コード例 #3
0
ファイル: BME280.cs プロジェクト: mozts2005/Meadow.Foundation
        public void StartUpdating(
            Oversample temperatureSampleCount = Oversample.OversampleX8,
            Oversample pressureSampleCount    = Oversample.OversampleX8,
            Oversample humiditySampleCount    = Oversample.OversampleX1,
            int standbyDuration = 1000)
        {
            // TODO: for standby durations of 1,000ms and less, the sensor
            // will actually handle the reading loop. you put it into `Normal`
            // mode and set it to one of the known `StandbyDuration`s.
            //
            // So perhaps this should be an option. From a method signature
            // standpoint, i think that we would add an overload that took
            // a known `StandbyDuration` instead of an int.
            //
            // With that said, however, as far as I can tell, the sensor won't
            // send an interrupt when a new reading is taken, so i'm not sure
            // how we would synchronize with it, since the time that each read
            // takes is determined by the samples, filter, etc. -b
            //
            // TODO: for longer standby durations, we should put the sensor into
            // Modes.Sleep to save power. Need to figure out what the stanby
            // duration threshold is for that. i'm guessing 5 seconds might be a
            // good value.


            // thread safety
            lock (_lock) {
                if (IsSampling)
                {
                    return;
                }

                // state muh-cheen
                IsSampling = true;

                SamplingTokenSource = new CancellationTokenSource();
                CancellationToken ct = SamplingTokenSource.Token;

                AtmosphericConditions            oldConditions;
                AtmosphericConditionChangeResult result;
                Task.Factory.StartNew(async() => {
                    while (true)
                    {
                        if (ct.IsCancellationRequested)
                        {
                            // do task clean up here
                            _observers.ForEach(x => x.OnCompleted());
                            break;
                        }
                        // capture history
                        oldConditions = Conditions;

                        // read
                        await Read(temperatureSampleCount, pressureSampleCount, humiditySampleCount);

                        // build a new result with the old and new conditions
                        result = new AtmosphericConditionChangeResult(oldConditions, Conditions);

                        // let everyone know
                        RaiseChangedAndNotify(result);

                        // sleep for the appropriate interval
                        await Task.Delay(standbyDuration);
                    }
                }, SamplingTokenSource.Token);
            }
        }