コード例 #1
0
 /// <summary>
 /// Starts continuously sampling the sensor.
 ///
 /// This method also starts raising `Changed` events and IObservable
 /// subscribers getting notified. Use the `readIntervalDuration` parameter
 /// to specify how often events and notifications are raised/sent.
 /// </summary>
 /// <param name="sampleCount">How many samples to take during a given
 /// reading. These are automatically averaged to reduce noise.</param>
 /// <param name="sampleIntervalDuration">The time, in milliseconds,
 /// to wait in between samples during a reading.</param>
 /// <param name="standbyDuration">The time, in milliseconds, to wait
 /// between sets of sample readings. This value determines how often
 /// `Changed` events are raised and `IObservable` consumers are notified.</param>
 public void StartUpdating(
     int sampleCount            = 10,
     int sampleIntervalDuration = 40,
     int standbyDuration        = 100)
 {
     AnalogInputPort.StartSampling(sampleCount, sampleIntervalDuration, standbyDuration);
 }
コード例 #2
0
        /// <summary>
        ///     Create a new ADXL335 sensor object.
        /// </summary>
        /// <param name="x">Analog pin connected to the X axis output from the ADXL335 sensor.</param>
        /// <param name="y">Analog pin connected to the Y axis output from the ADXL335 sensor.</param>
        /// <param name="z">Analog pin connected to the Z axis output from the ADXL335 sensor.</param>
        /// <param name="updateInterval">Update interval for the sensor, set to 0 to put the sensor in polling mode.</param>
        /// <<param name="accelerationChangeNotificationThreshold">Acceleration change threshold.</param>
        public ADXL335(IAnalogPin x, IAnalogPin y, IAnalogPin z, ushort updateInterval = 100,
                       double accelerationChangeNotificationThreshold = 0.1F)
        {
            if ((updateInterval != 0) && (updateInterval < MinimumPollingPeriod))
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval),
                                                      "Update interval should be 0 or greater than " + MinimumPollingPeriod);
            }

            _x = new AnalogInputPort(x);
            _y = new AnalogInputPort(y);
            _z = new AnalogInputPort(z);
            //
            //  Now set the default calibration data.
            //
            XVoltsPerG    = 0.325;
            YVoltsPerG    = 0.325;
            ZVoltsPerG    = 0.550;
            SupplyVoltage = 3.3;

            if (updateInterval > 0)
            {
                StartUpdating();
            }
            else
            {
                Update().RunSynchronously();
            }
        }
コード例 #3
0
        void StartSampling()
        {
            int sampleCount            = samplingSetting.sampleCount;
            int sampleIntervalDuration = samplingSetting.sampleIntervalDuration;
            int standbyDuration        = samplingSetting.standbyDuration;
            var counter = 0;
            var total   = 0f;
            var average = 0f;

            for (int i = 0; i < sampleCount; i++)
            {
                // read the voltage
                float temp = AnalogInputPort.ReadValue();

                total += temp;
                counter++;
                average = total / counter;
                if (!IsSampling)
                {
                    break;
                }
                Thread.Sleep(sampleIntervalDuration);
            }
            IsSampling = false;
            RaiseChangedAndNotify(new FloatChangeResult(average, 0));
        }
コード例 #4
0
        void StartSampling()
        {
            int sampleCount            = samplingSetting.sampleCount;
            int sampleIntervalDuration = samplingSetting.sampleIntervalDuration;
            int standbyDuration        = samplingSetting.standbyDuration;
            var counter = 0;
            var total   = 0f;
            var average = 0f;

            for (int i = 0; i < sampleCount; i++)
            {
                // read the voltage
                float voltage = AnalogInputPort.ReadValue();
                // (sampleCount, sampleIntervalDuration);
                // convert and save to our temp property for later retreival
                var temp = VoltageToTemperature(voltage);
                total += temp;
                counter++;
                average = total / counter;
                if (!IsSampling)
                {
                    break;
                }
                Thread.Sleep(sampleIntervalDuration);
            }
            IsSampling = false;
            RaiseEventsAndNotify
            (
                new AtmosphericConditionChangeResult(new AtmosphericConditions(average, 0, 0), null)
            );
        }
コード例 #5
0
 /// <summary>
 /// Convenience method to get the current temperature. For frequent reads, use
 /// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
 /// </summary>
 /// <param name="sampleCount">The number of sample readings to take. 
 /// Must be greater than 0. These samples are automatically averaged.</param>
 /// <param name="sampleIntervalDuration">The time, in milliseconds,
 /// to wait in between samples during a reading.</param>
 /// <returns>A float value that's ann average value of all the samples taken.</returns>
 public async Task<AtmosphericConditions> Read(int sampleCount = 10, int sampleIntervalDuration = 40)
 {
     // read the voltage
     float voltage = await AnalogInputPort.Read(sampleCount, sampleIntervalDuration);
     // convert and save to our temp property for later retreival
     Temperature = VoltageToTemperature(voltage);
     // return
     return new AtmosphericConditions(Temperature, 0, 0);
     //return Temperature;
 }
コード例 #6
0
        /// <summary>
        /// Convenience method to get the current soil moisture. For frequent reads, use
        /// StartUpdating() and StopUpdating().
        /// </summary>
        /// <param name="sampleCount">The number of sample readings to take.
        /// Must be greater than 0.</param>
        /// <param name="sampleInterval">The interval, in milliseconds, between
        /// sample readings.</param>
        /// <returns></returns>
        public async Task <float> Read(int sampleCount = 10, int sampleInterval = 40)
        {
            // read the voltage
            float voltage = await AnalogInputPort.Read(sampleCount, sampleInterval);

            // convert and save to our temp property for later retrieval
            Moisture = VoltageToMoisture(voltage);
            // return
            return(Moisture);
        }
コード例 #7
0
ファイル: FC28.cs プロジェクト: Rhynier/Meadow.Foundation
        /// <summary>
        /// Convenience method to get the current soil moisture. For frequent reads, use
        /// StartUpdating() and StopUpdating().
        /// </summary>
        /// <param name="sampleCount">The number of sample readings to take.
        /// Must be greater than 0.</param>
        /// <param name="sampleInterval">The interval, in milliseconds, between
        /// sample readings.</param>
        /// <returns></returns>
        public async Task <float> Read(int sampleCount = 10, int sampleInterval = 40)
        {
            DigitalPort.State = true;
            float voltage = await AnalogInputPort.Read(sampleCount, sampleInterval);

            DigitalPort.State = false;

            // convert and save to our temp property for later retrieval
            Moisture = VoltageToMoisture(voltage);
            return(Moisture);
        }
コード例 #8
0
        /// <summary>
        /// Convenience method to get the current temperature. For frequent reads, use
        /// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
        /// </summary>
        /// <param name="sampleCount">The number of sample readings to take.
        /// Must be greater than 0. These samples are automatically averaged.</param>
        /// <param name="sampleIntervalDuration">The time, in milliseconds,
        /// to wait in between samples during a reading.</param>
        /// <returns>A float value that's ann average value of all the samples taken.</returns>
        public async Task <float> Read(int sampleCount = 10, int sampleIntervalDuration = 40)
        {
            // read the voltage
            float voltage = await AnalogInputPort.Read(sampleCount, sampleIntervalDuration);

            // convert and save to our temp property for later retreival
            WaterLevel = VoltageToWaterLevel(voltage);

            // return
            return(WaterLevel);
        }
コード例 #9
0
        /// <summary>
        /// Convenience method to get the current soil moisture. For frequent reads, use
        /// StartUpdating() and StopUpdating().
        /// </summary>
        /// <param name="sampleCount">The number of sample readings to take.
        /// Must be greater than 0.</param>
        /// <param name="sampleInterval">The interval, in milliseconds, between
        /// sample readings.</param>
        /// <returns></returns>
        public async Task <FloatChangeResult> Read(int sampleCount = 10, int sampleInterval = 40)
        {
            // save previous moisture value
            float previousMoisture = Moisture;
            // read the voltage
            float voltage = await AnalogInputPort.Read(sampleCount, sampleInterval);

            // convert and save to our temp property for later retrieval
            Moisture = VoltageToMoisture(voltage);
            // return new and old Moisture values
            return(new FloatChangeResult(Moisture, previousMoisture));
        }
コード例 #10
0
        /// <summary>
        /// Convenience method to get the current soil moisture. For frequent reads, use
        /// StartUpdating() and StopUpdating().
        /// </summary>
        /// <param name="sampleCount">The number of sample readings to take.
        /// Must be greater than 0.</param>
        /// <param name="sampleInterval">The interval, in milliseconds, between
        /// sample readings.</param>
        /// <returns></returns>
        public float Read(int sampleCount = 10, int sampleInterval = 40)
        {
            float voltage = 0;

            // read the voltage
            for (int i = 0; i < sampleCount; i++)
            {
                var tmp = AnalogInputPort.ReadValue();
                voltage += tmp;
                Thread.Sleep(sampleInterval);
            }
            voltage /= sampleCount;
            // convert and save to our temp property for later retrieval
            Moisture = VoltageToMoisture(voltage);
            // return
            return(Moisture);
        }
コード例 #11
0
ファイル: FC28.cs プロジェクト: Gravicode/TinyCLR.Drivers
        /// <summary>
        /// Convenience method to get the current soil moisture. For frequent reads, use
        /// StartUpdating() and StopUpdating().
        /// </summary>
        /// <param name="sampleCount">The number of sample readings to take.
        /// Must be greater than 0.</param>
        /// <param name="sampleInterval">The interval, in milliseconds, between
        /// sample readings.</param>
        /// <returns></returns>
        public float Read(int sampleCount = 10, int sampleInterval = 40)
        {
            DigitalPort.Write(GpioPinValue.High);
            //float voltage = await AnalogInputPort.Read(sampleCount, sampleInterval);
            float voltage = 0;

            // read the voltage
            for (int i = 0; i < sampleCount; i++)
            {
                var tmp = AnalogInputPort.ReadValue();
                voltage += tmp;
                Thread.Sleep(sampleInterval);
            }
            voltage /= sampleCount;
            DigitalPort.Write(GpioPinValue.Low);

            // convert and save to our temp property for later retrieval
            Moisture = VoltageToMoisture(voltage);
            return(Moisture);
        }
コード例 #12
0
 /// <summary>
 /// Stops sampling the temperature.
 /// </summary>
 public void StopUpdating()
 {
     AnalogInputPort.StopSampling();
 }
コード例 #13
0
        public AnalogTemperature(
            IAnalogInputPort analogInputPort,
            KnownSensorType sensorType,
            Calibration calibration = null
            )
        {
            this.AnalogInputPort = analogInputPort;

            //
            //  If the calibration object is null use the defaults for TMP35.
            //
            if (calibration == null)
            {
                calibration = new Calibration();
            }

            switch (sensorType)
            {
            case KnownSensorType.TMP35:
            case KnownSensorType.LM35:
            case KnownSensorType.LM45:
                _yIntercept = 0;
                _millivoltsPerDegreeCentigrade = 10;
                break;

            case KnownSensorType.LM50:
            case KnownSensorType.TMP36:
                _yIntercept = 500;
                _millivoltsPerDegreeCentigrade = 10;
                break;

            case KnownSensorType.TMP37:
                _yIntercept = 0;
                _millivoltsPerDegreeCentigrade = 20;
                break;

            case KnownSensorType.Custom:
                _yIntercept = calibration.MillivoltsAtSampleReading - (calibration.SampleReading * calibration.MillivoltsAtSampleReading);
                _millivoltsPerDegreeCentigrade = calibration.MillivoltsPerDegreeCentigrade;
                break;

            default:
                throw new ArgumentException("Unknown sensor type", nameof(sensorType));
            }

            // wire up our observable
            // have to convert from voltage to temp units for our consumers
            // this is where the magic is: this allows us to extend the IObservable
            // pattern through the sensor driver
            AnalogInputPort.Subscribe(
                new FilterableObserver <FloatChangeResult, float>(
                    h => {
                var newTemp      = VoltageToTemperature(h.New);
                var oldTemp      = VoltageToTemperature(h.Old);
                this.Temperature = newTemp;         // save state
                RaiseEventsAndNotify(
                    new AtmosphericConditionChangeResult(
                        new AtmosphericConditions(newTemp, 0, 0),
                        new AtmosphericConditions(oldTemp, 0, 0)
                        ));
            })
                );
        }
コード例 #14
0
 /// <summary>
 ///     Create a new light sensor object using a dynaic reference voltage.
 /// </summary>
 /// <param name="pin">Analog channel connected to the sensor.</param>
 /// <param name="referenceVoltagePin">Analog channel connected to the reference voltage souce.</param>
 public ALSPT19315C(IAnalogPin pin, IAnalogPin referenceVoltagePin)
 {
     _sensor = new AnalogInputPort(pin);
     _referenceVoltagePort = new AnalogInputPort(referenceVoltagePin);
 }
コード例 #15
0
 /// <summary>
 ///     Create a new light sensor object using a static reference voltage.
 /// </summary>
 /// <param name="pin">AnalogChannel connected to the sensor.</param>
 /// <param name="referenceVoltage">Reference voltage.</param>
 public ALSPT19315C(IAnalogPin pin, double referenceVoltage)
 {
     _sensor = new AnalogInputPort(pin);
     _referenceVoltagePort = null;
     _referenceVoltage     = referenceVoltage;
 }