Пример #1
0
        /// <summary>
        ///     Create a new DS18B20 temperature sensor object with the specified configuration.
        /// </summary>
        /// <param name="oneWirePin">GPIO pin the DS18B20 is connected to.</param>
        /// <param name="deviceID">Address of the DS18B20 device.</param>
        /// <param name="updateInterval">Update period in milliseconds.  Note that this most be greater than the conversion period for the sensor.</param>
        /// <param name="temperatureChangeNotificationThreshold">Threshold for temperature changes that will generate an interrupt.</param>
        public DS18B20(Cpu.Pin oneWirePin, UInt64 deviceID          = 0, ushort updateInterval = MinimumPollingPeriod,
                       float temperatureChangeNotificationThreshold = 0.001F)
        {
            if (oneWirePin == Cpu.Pin.GPIO_NONE)
            {
                throw new ArgumentException("OneWire pin cannot be null.", nameof(oneWirePin));
            }
            lock (OneWireBus.Instance)
            {
                Sensor = OneWireBus.Add(oneWirePin);
                if (Sensor.DeviceBus.TouchReset() == 0)
                {
                    throw new Exception("Cannot find DS18B20 sensor on the OneWire interface.");
                }
                if (Sensor.DeviceIDs.Count == 1)
                {
                    BusMode = BusModeType.SingleDevice;
                }
                else
                {
                    if (deviceID == 0)
                    {
                        throw new ArgumentException("Device deviceID cannot be 0 on a OneWireBus with multiple devices.", nameof(deviceID));
                    }
                    BusMode = BusModeType.MultimpleDevices;
                }
                //
                //  Check for the ROM ID in the list of devices connected to the bus.
                //
                if (deviceID != 0)
                {
                    bool found = false;
                    foreach (UInt64 id in Sensor.DeviceIDs)
                    {
                        if (id == deviceID)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        throw new Exception("Cannot locate the specified device ID on the OneWire bus.");
                    }
                }
            }

            DeviceID = deviceID;
            ReadConfiguration();
            if ((updateInterval != 0) && (MaximumConversionPeriod > updateInterval))
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval), "Temperature readings can take " + MaximumConversionPeriod + "ms at this resolution.");
            }

            TemperatureChangeNotificationThreshold = temperatureChangeNotificationThreshold;
            _updateInterval = updateInterval;

            if (updateInterval > 0)
            {
                StartUpdating();
            }
            else
            {
                Update();
            }
        }
Пример #2
0
        /// <summary>
        ///     Create a new DS18B20 temperature sensor object with the specified configuration.
        /// </summary>
        /// <param name="oneWirePin">GPIO pin the DS18B20 is connected to.</param>
        /// <param name="deviceID">Address of the DS18B20 device.</param>
        /// <param name="updateInterval">Update period in milliseconds.  Note that this most be greater than the conversion period for the sensor.</param>
        /// <param name="temperatureChangeNotificationThreshold">Threshold for temperature changes that will generate an interrupt.</param>
        public DS18B20(int oneWirePin, UInt64 deviceID = 0, ushort updateInterval = MinimumPollingPeriod,
                       float temperatureChangeNotificationThreshold = 0.001F)
        {
            Sensor = new GHIElectronics.TinyCLR.Devices.Onewire.OneWireController
                         (oneWirePin);

            if (oneWirePin < 0)
            {
                throw new ArgumentException("OneWire pin cannot be null.", nameof(oneWirePin));
            }
            //lock (OneWireBus.Instance)
            {
                //Sensor = OneWireBus.Add(oneWirePin);
                if (Sensor.TouchReset() == 0)
                {
                    throw new Exception("Cannot find DS18B20 sensor on the OneWire interface.");
                }
                var oneWireDevices = Sensor.FindAllDevices();
                if (oneWireDevices.Count == 1)
                {
                    BusMode = BusModeType.SingleDevice;
                }
                else
                {
                    if (deviceID == 0)
                    {
                        throw new ArgumentException("Device deviceID cannot be 0 on a OneWireBus with multiple devices.", nameof(deviceID));
                    }
                    BusMode = BusModeType.MultimpleDevices;
                }
                //
                //  Check for the ROM ID in the list of devices connected to the bus.
                //
                if (deviceID != 0)
                {
                    bool found = false;
                    foreach (byte[] serialNumber in oneWireDevices)
                    {
                        for (int i = 0; i < serialNumber.Length; i++)
                        {
                            UInt64 id = serialNumber[i];
                            if (id == deviceID)
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found)
                    {
                        throw new Exception("Cannot locate the specified device ID on the OneWire bus.");
                    }
                }
            }

            DeviceID = deviceID;
            ReadConfiguration();
            if ((updateInterval != 0) && (MaximumConversionPeriod > updateInterval))
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval), "Temperature readings can take " + MaximumConversionPeriod + "ms at this resolution.");
            }

            TemperatureChangeNotificationThreshold = temperatureChangeNotificationThreshold;
            _updateInterval = updateInterval;

            if (updateInterval > 0)
            {
                StartUpdating();
            }
            else
            {
                Update();
            }
        }