Exemplo n.º 1
0
        /// <summary>
        /// Open driver
        /// </summary>
        /// <param name="conversionRate">Conversion rate</param>
        /// <param name="shutdownMode">Shutdown mode</param>
        /// <param name="thermostatMode">Thermostat mode</param>
        /// <param name="alertPolarity">Polarity of the alert pin</param>
        /// <param name="consecutiveFaults">Consecutive faults before activate alert pin</param>
        /// <param name="temperatureHigh">Temperature High for alert</param>
        /// <param name="temperatureLow">Temperature Low for alert</param>
        /// <param name="alertPin">Alert pin</param>
        /// <param name="i2cSelector">I2C selector string</param>
        /// <returns>Driver opened</returns>
        public async Task<bool> OpenAsync(
            ConversionRate conversionRate = ConversionRate._4Hz,
            bool shutdownMode = false,
            ThermostatMode thermostatMode = ThermostatMode.Comparator,
            AlertPolarity alertPolarity = AlertPolarity.ActiveLow,
            ConsecutiveFaults consecutiveFaults = ConsecutiveFaults._1,
            float temperatureHigh = TEMP_HIGH_DEFAULT,
            float temperatureLow = TEMP_LOW_DEFAULT,
            int alertPin = 0,
            string i2cSelector = PI2_I2C_SELECTOR)
        {
            try
            {
                string advancedQuerySyntax = I2cDevice.GetDeviceSelector(i2cSelector);
                DeviceInformationCollection device_information_collection = await DeviceInformation.FindAllAsync(advancedQuerySyntax);
                string deviceId = device_information_collection[0].Id;

                this.i2c = await I2cDevice.FromIdAsync(deviceId, this.i2cConfig);

                // load configuration register
                this.LoadConfiguration();

                // set conversion rate
                this.configuration = (ushort)(this.configuration & ~CONV_RATE_MASK);
                this.configuration |= (ushort)conversionRate;
                // set shutdown mode
                this.configuration = (shutdownMode) ? (ushort)(this.configuration | SHUTDOWN_MODE) : (ushort)(this.configuration & ~SHUTDOWN_MODE);
                // set thermostat mode
                this.configuration = (thermostatMode == ThermostatMode.Interrupt) ? (ushort)(this.configuration | THERMOSTAT_MODE) : (ushort)(this.configuration & ~THERMOSTAT_MODE);
                // set alert pin polarity
                this.configuration = (alertPolarity == AlertPolarity.ActiveHigh) ? (ushort)(this.configuration | POLARITY) : (ushort)(this.configuration & ~POLARITY);
                // set consecutive faults for alert
                this.configuration = (ushort)(this.configuration & ~FAULT_QUEUE_MASK);
                this.configuration |= (ushort)consecutiveFaults;

                // save configuration register
                this.ChangeConfiguration();

                // set temperature high for alert
                this.regAddress[0] = TEMP_HIGH_REG_ADDR;
                this.regData = this.TemperatureToBytes(temperatureHigh);
                this.WriteRegister(this.regAddress, this.regData);
                // set temperature low for alert
                this.regAddress[0] = TEMP_LOW_REG_ADDR;
                this.regData = this.TemperatureToBytes(temperatureLow);
                this.WriteRegister(this.regAddress, this.regData);

                if (alertPin != 0)
                {
                    GpioController gpioController = GpioController.GetDefault();
                    this.alertPin = gpioController.OpenPin(alertPin);
                    this.alertPin.ValueChanged += AlertPin_ValueChanged;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="a0addrSelect">A0 pin connection for address selection</param>
        /// <param name="clockRateKhz">I2C clock rate in KHz</param>
        /// <param name="conversionRate">Conversion rate</param>
        /// <param name="shutdownMode">Shutdown mode</param>
        /// <param name="thermostatMode">Thermostat mode</param>
        /// <param name="alertPolarity">Polarity of the alert pin</param>
        /// <param name="consecutiveFaults">Consecutive faults before activate alert pin</param>
        /// <param name="temperatureHigh">Temperature High for alert</param>
        /// <param name="temperatureLow">Temperature Low for alert</param>
        public TMP102(A0AddressSelect a0addrSelect        = A0AddressSelect.GND,
                      int clockRateKhz                    = CLOCK_RATE_KHZ_DEFAULT,
                      ConversionRate conversionRate       = ConversionRate._4Hz,
                      bool shutdownMode                   = false,
                      ThermostatMode thermostatMode       = ThermostatMode.Comparator,
                      AlertPolarity alertPolarity         = AlertPolarity.ActiveLow,
                      ConsecutiveFaults consecutiveFaults = ConsecutiveFaults._1,
                      float temperatureHigh               = TEMP_HIGH_DEFAULT,
                      float temperatureLow                = TEMP_LOW_DEFAULT)
        {
            // create buffers for one and two bytes for I2C communications
            this.regAddress = new byte[REG_ADDRESS_SIZE];
            this.regData    = new byte[REG_DATA_SIZE];

            // configure and create I2C reference device
            I2CDevice.Configuration i2cConfig = new I2CDevice.Configuration((ushort)(TMP102_ADDRESS_BASE + a0addrSelect), clockRateKhz);
            this.i2c = new I2CDevice(i2cConfig);

            // load configuration register
            this.LoadConfiguration();

            // set conversion rate
            this.configuration  = (ushort)(this.configuration & ~CONV_RATE_MASK);
            this.configuration |= (ushort)conversionRate;
            // set shutdown mode
            this.configuration = (shutdownMode) ? (ushort)(this.configuration | SHUTDOWN_MODE) : (ushort)(this.configuration & ~SHUTDOWN_MODE);
            // set thermostat mode
            this.configuration = (thermostatMode == ThermostatMode.Interrupt) ? (ushort)(this.configuration | THERMOSTAT_MODE) : (ushort)(this.configuration & ~THERMOSTAT_MODE);
            // set alert pin polarity
            this.configuration = (alertPolarity == AlertPolarity.ActiveHigh) ? (ushort)(this.configuration | POLARITY) : (ushort)(this.configuration & ~POLARITY);
            // set consecutive faults for alert
            this.configuration  = (ushort)(this.configuration & ~FAULT_QUEUE_MASK);
            this.configuration |= (ushort)consecutiveFaults;

            // save configuration register
            this.ChangeConfiguration();

            // set temperature high for alert
            this.regAddress[0] = TEMP_HIGH_REG_ADDR;
            this.regData       = this.TemperatureToBytes(temperatureHigh);
            this.WriteRegister(this.regAddress, this.regData);
            // set temperature low for alert
            this.regAddress[0] = TEMP_LOW_REG_ADDR;
            this.regData       = this.TemperatureToBytes(temperatureLow);
            this.WriteRegister(this.regAddress, this.regData);
        }
Exemplo n.º 3
0
        // -------------------------------------------------------------------------------------------------------------------------------------
        private bool Init(
            ADD0 addressSelect,
            bool oneShotMode,
            AlertPolarity alertPolarity,
            ConversionRate conversionRate,
            ThermostatMode thermostatMode,
            ConsecutiveFaults consecutiveFaults,
            ushort limitHigh,
            ushort limitLow)
        {
            // Sleep past first conversion
            Thread.Sleep(30);

            switch (addressSelect)
            {
                case ADD0.Gnd: _sensorAddress = 0x90 >> 1; break;
                case ADD0.Vcc: _sensorAddress = 0x92 >> 1; break;
                case ADD0.SDA: _sensorAddress = 0x94 >> 1; break;
                case ADD0.SCL: _sensorAddress = 0x96 >> 1; break;
            }

            _TMP102 = new I2CDevice(new I2CDevice.Configuration(_sensorAddress, 100));

            _alertPolarity = alertPolarity;
            _oneShotMode = oneShotMode;
            _thermostatMode = thermostatMode;
            _consecutiveFaults = consecutiveFaults;

            _registerNum[0] = (byte)Registers.Configuration;
            int bytesTransfered = ReadRegister();

            if (bytesTransfered == 3)
            {
                if (_oneShotMode)
                    _registerValue[0] = (byte)(_registerValue[0] | 0x01);
                else
                    _registerValue[0] = (byte)(_registerValue[0] & 0xfe);

                if (_thermostatMode == ThermostatMode.InterruptMode)
                    _registerValue[0] = (byte)(_registerValue[0] | 0x02);
                else
                    _registerValue[0] = (byte)(_registerValue[0] & 0xfd);

                if (_alertPolarity == AlertPolarity.activeLow)
                    _registerValue[0] = (byte)(_registerValue[0] | 0x04);
                else
                    _registerValue[0] = (byte)(_registerValue[0] & ~0x04);

                switch (conversionRate)
                {
                    case ConversionRate.quarter_Hz: _registerValue[1] = (byte)((_registerValue[1] & 0x3f) | (0x00 << 6)); break;
                    case ConversionRate.one_Hz: _registerValue[1] = (byte)((_registerValue[1] & 0x3f) | (0x01 << 6)); break;
                    case ConversionRate.four_Hz: _registerValue[1] = (byte)((_registerValue[1] & 0x3f) | (0x02 << 6)); break;
                    case ConversionRate.eight_Hz: _registerValue[1] = (byte)((_registerValue[1] & 0x3f) | (0x03 << 6)); break;
                }

                bytesTransfered = WriteRegister();
                Thread.Sleep(30);
            }

            return (bytesTransfered == 3);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="a0addrSelect">A0 pin connection for address selection</param>
        /// <param name="clockRateKhz">I2C clock rate in KHz</param>
        /// <param name="conversionRate">Conversion rate</param>
        /// <param name="shutdownMode">Shutdown mode</param>
        /// <param name="thermostatMode">Thermostat mode</param>
        /// <param name="alertPolarity">Polarity of the alert pin</param>
        /// <param name="consecutiveFaults">Consecutive faults before activate alert pin</param>
        /// <param name="temperatureHigh">Temperature High for alert</param>
        /// <param name="temperatureLow">Temperature Low for alert</param>
        public TMP102(A0AddressSelect a0addrSelect = A0AddressSelect.GND,
            int clockRateKhz = CLOCK_RATE_KHZ_DEFAULT,
            ConversionRate conversionRate = ConversionRate._4Hz,
            bool shutdownMode = false,
            ThermostatMode thermostatMode = ThermostatMode.Comparator,
            AlertPolarity alertPolarity = AlertPolarity.ActiveLow,
            ConsecutiveFaults consecutiveFaults = ConsecutiveFaults._1,
            float temperatureHigh = TEMP_HIGH_DEFAULT,
            float temperatureLow = TEMP_LOW_DEFAULT)
        {
            // create buffers for one and two bytes for I2C communications
            this.regAddress = new byte[REG_ADDRESS_SIZE];
            this.regData = new byte[REG_DATA_SIZE];

            // configure and create I2C reference device
            I2CDevice.Configuration i2cConfig = new I2CDevice.Configuration((ushort)(TMP102_ADDRESS_BASE + a0addrSelect), clockRateKhz);
            this.i2c = new I2CDevice(i2cConfig);

            // load configuration register
            this.LoadConfiguration();

            // set conversion rate
            this.configuration = (ushort)(this.configuration & ~CONV_RATE_MASK);
            this.configuration |= (ushort)conversionRate;
            // set shutdown mode
            this.configuration = (shutdownMode) ? (ushort)(this.configuration | SHUTDOWN_MODE) : (ushort)(this.configuration & ~SHUTDOWN_MODE);
            // set thermostat mode
            this.configuration = (thermostatMode == ThermostatMode.Interrupt) ? (ushort)(this.configuration | THERMOSTAT_MODE) : (ushort)(this.configuration & ~THERMOSTAT_MODE);
            // set alert pin polarity
            this.configuration = (alertPolarity == AlertPolarity.ActiveHigh) ? (ushort)(this.configuration | POLARITY) : (ushort)(this.configuration & ~POLARITY);
            // set consecutive faults for alert
            this.configuration = (ushort)(this.configuration & ~FAULT_QUEUE_MASK);
            this.configuration |= (ushort)consecutiveFaults;

            // save configuration register
            this.ChangeConfiguration();

            // set temperature high for alert
            this.regAddress[0] = TEMP_HIGH_REG_ADDR;
            this.regData = this.TemperatureToBytes(temperatureHigh);
            this.WriteRegister(this.regAddress, this.regData);
            // set temperature low for alert
            this.regAddress[0] = TEMP_LOW_REG_ADDR;
            this.regData = this.TemperatureToBytes(temperatureLow);
            this.WriteRegister(this.regAddress, this.regData);
        }