コード例 #1
0
        /// <summary>
        ///     New instance of the AnalogTemperatureSensor class.
        /// </summary>
        /// <param name="analogPin">Analog pin the temperature sensor is connected to.</param>
        /// <param name="sensorType">Type of sensor attached to the analog port.</param>
        /// <param name="calibration">Calibration for the analog temperature sensor.</param>
        /// <param name="updateInterval">Number of milliseconds between samples (0 indicates polling to be used)</param>
        /// <param name="temperatureChangeNotificationThreshold">Changes in temperature greater than this value will trigger an event when updatePeriod > 0.</param>
        public AnalogTemperature(Cpu.AnalogChannel analogPin, KnownSensorType sensorType, Calibration calibration = null,
                                 ushort updateInterval = MinimumPollingPeriod, float temperatureChangeNotificationThreshold = 0.001F)
        {
            if (temperatureChangeNotificationThreshold < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(temperatureChangeNotificationThreshold),
                                                      "Temperature threshold should be >= 0");
            }

            if ((updateInterval != 0) && (updateInterval < MinimumPollingPeriod))
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval),
                                                      "Update period should be 0 or >= than " + MinimumPollingPeriod);
            }
            //
            //  If the calibration object is null use the defaults for TMP35.
            //
            if (calibration == null)
            {
                calibration = new Calibration();
            }

            TemperatureChangeNotificationThreshold = temperatureChangeNotificationThreshold;
            _updateInterval = updateInterval;

            AnalogPort = new AnalogInput(analogPin);
            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));
#pragma warning disable 0162
                break;
#pragma warning restore 0162
            }

            if (updateInterval > 0)
            {
                StartUpdating();
            }
            else
            {
                Update();
            }
        }
コード例 #2
0
        public AnalogTemperature(IAnalogInputPort analogInputPort,
                                 KnownSensorType sensorType,
                                 Calibration calibration = null)
        {
            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.MillivoltsPerDegreeCentigrade);
                _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);
                Temperature = newTemp;         // save state
                RaiseEventsAndNotify
                (
                    new AtmosphericConditionChangeResult(
                        new AtmosphericConditions(newTemp, null, null),
                        new AtmosphericConditions(oldTemp, null, null)
                        )
                );
            }
                    )
            );
        }
コード例 #3
0
        public AnalogTemperature(IAnalogInputPort analogInputPort,
                                 KnownSensorType sensorType,
                                 Calibration calibration = null)
        {
            AnalogInputPort = analogInputPort;

            switch (sensorType)
            {
            case KnownSensorType.TMP35:
            case KnownSensorType.LM35:
            case KnownSensorType.LM45:
                calibration = new Calibration(
                    degreesCelciusSampleReading: 25,
                    millivoltsAtSampleReading: 250,
                    millivoltsPerDegreeCentigrade: 10);
                break;

            case KnownSensorType.LM50:
            case KnownSensorType.TMP36:
                calibration = new Calibration(
                    degreesCelciusSampleReading: 25,
                    millivoltsAtSampleReading: 750,
                    millivoltsPerDegreeCentigrade: 10);
                break;

            case KnownSensorType.TMP37:
                calibration = new Calibration(
                    degreesCelciusSampleReading: 25,
                    millivoltsAtSampleReading: 750,
                    millivoltsPerDegreeCentigrade: 10);
                break;

            case KnownSensorType.Custom:
                //user provided calibration
                break;

            default:
                calibration = new Calibration();
                break;
            }

            SensorCalibration = calibration;

            // 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 FilterableChangeObserver <FloatChangeResult, float>(
                    h => {
                var newTemp = VoltageToTemperature(h.New);
                var oldTemp = VoltageToTemperature(h.Old);
                Temperature = newTemp;         // save state
                RaiseEventsAndNotify
                (
                    new AtmosphericConditionChangeResult(
                        new AtmosphericConditions(newTemp, null, null),
                        new AtmosphericConditions(oldTemp, null, null)
                        )
                );
            }
                    )
            );
        }