/// <summary> /// Reset the sensor. /// </summary> public void Reset() { var data = _mpl3115a2.ReadRegister(Registers.Control1); data |= 0x04; _mpl3115a2.WriteRegister(Registers.Control1, data); }
/// <summary> /// Update the configuration for the BME280. /// </summary> /// <remarks> /// This method uses the data in the configuration properties in order to set up the /// BME280. Ensure that the following are set correctly before calling this method: /// - Standby /// - Filter /// - HumidityOverSampling /// - TemperatureOverSampling /// - PressureOverSampling /// - Mode /// </remarks> public void UpdateConfiguration() { // // Put to sleep to allow the configuration to be changed. // _bme280.WriteRegister(Registers.Measurement, 0x00); var data = (byte)((((byte)Standby << 5) & 0xe0) | (((byte)Filter << 2) & 0x1c)); _bme280.WriteRegister(Registers.Configuration, data); data = (byte)((byte)HumidityOverSampling & 0x07); _bme280.WriteRegister(Registers.Humidity, data); data = (byte)((((byte)TemperatureOverSampling << 5) & 0xe0) | (((byte)PressureOversampling << 2) & 0x1c) | ((byte)Mode & 0x03)); _bme280.WriteRegister(Registers.Measurement, data); }
/// <summary> /// Create a new MPL3115A2 object with the default address and speed settings. /// </summary> /// <param name="address">Address of the sensor (default = 0x60).</param> /// <param name="speed">Bus speed to use when communicating with the sensor (Maximum is 400 kHz).</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> /// <param name="pressureChangedNotificationThreshold">Changes in pressure greater than this value will trigger an event when updatePeriod > 0.</param> public MPL3115A2(byte address = 0x60, ushort speed = 400, ushort updateInterval = MinimumPollingPeriod, float temperatureChangeNotificationThreshold = 0.001F, float pressureChangedNotificationThreshold = 10.0F) { if ((speed < 10) || (speed > 1000)) { throw new ArgumentOutOfRangeException(nameof(speed), "Speed should be 10 KHz to 3,400 KHz."); } if (temperatureChangeNotificationThreshold < 0) { throw new ArgumentOutOfRangeException(nameof(temperatureChangeNotificationThreshold), "Temperature threshold should be >= 0"); } if (pressureChangedNotificationThreshold < 0) { throw new ArgumentOutOfRangeException(nameof(pressureChangedNotificationThreshold), "Pressure threshold should be >= 0"); } if ((updateInterval != 0) && (updateInterval < MinimumPollingPeriod)) { throw new ArgumentOutOfRangeException(nameof(updateInterval), "Update period should be 0 or >= than " + MinimumPollingPeriod); } TemperatureChangeNotificationThreshold = temperatureChangeNotificationThreshold; PressureChangeNotificationThreshold = pressureChangedNotificationThreshold; _updateInterval = updateInterval; var device = new I2cBus(address, speed); _mpl3115a2 = device; if (_mpl3115a2.ReadRegister(Registers.WhoAmI) != 0xc4) { throw new Exception("Unexpected device ID, expected 0xc4"); } _mpl3115a2.WriteRegister(Registers.Control1, (byte)(ControlRegisterBits.Active | ControlRegisterBits.OverSample128)); _mpl3115a2.WriteRegister(Registers.DataConfiguration, (byte)(ConfigurationRegisterBits.DataReadyEvent | ConfigurationRegisterBits.EnablePressureEvent | ConfigurationRegisterBits.EnableTemperatureEvent)); if (updateInterval > 0) { StartUpdating(); } else { Update(); } }
/// <summary> /// Force the sensor to make a reading and update the relevant properties. /// </summary> public void Update() { int temp = 0; // // Get the humidity first. // _groveTH02.WriteRegister(Registers.Config, StartMeasurement); // // Maximum conversion time should be 40ms but loop just in case // it takes longer. // Thread.Sleep(40); while ((_groveTH02.ReadRegister(Registers.Status) & 0x01) > 0) { ; } byte[] data = _groveTH02.ReadRegisters(Registers.DataHigh, 2); temp = data[0] << 8; temp |= data[1]; temp >>= 4; Humidity = (((float)temp) / 16) - 24; // // Now get the temperature. // _groveTH02.WriteRegister(Registers.Config, StartMeasurement | MeasureTemperature); // // Maximum conversion time should be 40ms but loop just in case // it takes longer. // Thread.Sleep(40); while ((_groveTH02.ReadRegister(Registers.Status) & 0x01) > 0) { ; } data = _groveTH02.ReadRegisters(Registers.DataHigh, 2); temp = data[0] << 8; temp |= data[1]; temp >>= 2; Temperature = (((float)temp) / 32) - 50; }
/// <summary> /// Set the PowerControl register (see pages 25 and 26 of the data sheet) /// </summary> /// <param name="linkActivityAndInactivity">Link the activity and inactivity events.</param> /// <param name="autoASleep">Enable / disable auto sleep when the activity and inactivity are linked.</param> /// <param name="measuring">Enable or disable measurements (turn on or off).</param> /// <param name="sleep">Put the part to sleep (true) or run in normal more (false).</param> /// <param name="frequency">Frequency of measurements when the part is in sleep mode.</param> public void SetPowerState(bool linkActivityAndInactivity, bool autoASleep, bool measuring, bool sleep, Frequency frequency) { byte data = 0; if (linkActivityAndInactivity) { data |= 0x20; } if (autoASleep) { data |= 0x10; } if (measuring) { data |= 0x08; } if (sleep) { data |= 0x40; } data |= (byte)frequency; _adxl345.WriteRegister(Registers.PowerControl, data); }
/// <summary> /// Turn the TSL2561 off. /// </summary> /// <remarks> /// Reset the power bits in the control register (page 13 of the datasheet). /// </remarks> public void TurnOff() { _tsl2561.WriteRegister(Registers.Control, 0x00); }