Skip to content

Commit

Permalink
Further abstraction since interfaces can have static methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
EAGrahamJr committed Jul 10, 2024
1 parent efe491d commit 741f0d1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.diozero.devices;
/*-
* #%L
* Organisation: diozero
* Project: diozero - Core
* Filename: AirQualitySensorInterface.java
*
* This file is part of the diozero project. More information about this project
* can be found at https://www.diozero.com/.
* %%
* Copyright (C) 2016 - 2024 diozero
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/

/**
* An air-quality sensor uses relative humidity and a "resistance" value to measure air quaility.
*/
public interface AirQualitySensorInterface extends HygrometerInterface {
/**
* "Standard" humidity baseline typically used for indoor air-quality.
*/
float STANDARD_INDOOR_HUMIDITY = 40f;

float getGasResistance();

/**
* Calculates the indoor air quality as a percentage based on a baseline reading. Based off of Pimoroni's
* <a href="https://github.com/pimoroni/bme680-python/blob/main/examples/indoor-air-quality.py">indoor-air-quality.py</a>
* <p>
* Under non-calibrated, general usage, it is recommended that the sensor "warm up" in the current mode for at least
* 30 minutes before taking readings. After that time, any kind of statistical baseline (e.g. average) for the gas
* and humidity readings can be used to get an indication of general air-quality.
* </p>
*
* @param gasReading current reading
* @param gasBaseline the "baseline" to score off of
* @param humidityReading current reading
* @param humidityBaseline the "baseline" to score off of
* @param humidityWeighting weighting applied to scoring (a good default is 0.25f)
*/
static float airQuality(float gasReading, float gasBaseline, float humidityReading, float humidityBaseline,
float humidityWeighting) {
float gasOffset = gasBaseline - gasReading;
float humidityOffset = humidityReading - humidityBaseline;

float humidityScore;
if (humidityOffset > 0) {
humidityScore = (100 - humidityBaseline - humidityOffset) / (100 - humidityBaseline) * (humidityWeighting * 100);
}
else {
humidityScore = (humidityBaseline + humidityOffset) / humidityBaseline * (humidityWeighting * 100);
}

float gasScore;
if (gasOffset > 0) {
gasScore = (gasReading / gasBaseline) * (100 - (humidityWeighting * 100));
}
else {
gasScore = 100 - (humidityWeighting * 100);
}

// Calculate air_quality_score.
return humidityScore + gasScore;
}

}
46 changes: 4 additions & 42 deletions diozero-core/src/main/java/com/diozero/devices/BME68x.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* https://github.com/pimoroni/bme680-python
* https://github.com/knobtviker/bme680
*/
public class BME68x implements BarometerInterface, ThermometerInterface, HygrometerInterface {
public class BME68x implements BarometerInterface, ThermometerInterface, AirQualitySensorInterface {
// Chip vendor for the BME680
private static final String CHIP_VENDOR = "Bosch";
// Chip name for the BME680
Expand Down Expand Up @@ -552,7 +552,7 @@ public BME68x(final I2CDeviceInterface device) {
* @param controller I2C bus the sensor is connected to.
* @param address I2C address of the sensor.
* @param humidityOversampling Humidity oversampling.
* @param termperatureOversampling Temperature oversampling.
* @param temperatureOversampling Temperature oversampling.
* @param pressureOversampling Pressure oversampling.
* @param filter Infinite Impulse Response (IIR) filter.
* @param standbyDuration Standby time between sequential mode
Expand All @@ -570,7 +570,7 @@ public BME68x(final int controller, final int address, OversamplingMultiplier hu
*
* @param device I2C device.
* @param humidityOversampling Humidity oversampling.
* @param termperatureOversampling Temperature oversampling.
* @param temperatureOversampling Temperature oversampling.
* @param pressureOversampling Pressure oversampling.
* @param filter Infinite Impulse Response (IIR) filter.
* @param standbyDuration Standby time between sequential mode
Expand Down Expand Up @@ -910,6 +910,7 @@ public float getRelativeHumidity() {
return getSensorData()[0].getHumidity();
}

@Override
public float getGasResistance() {
return getSensorData()[0].getGasResistance();
}
Expand Down Expand Up @@ -2013,43 +2014,4 @@ public String toString() {
}
}

/**
* Calculates the indoor air quality as a percentage based on a baseline reading. Based off of Pimoroni's
* <a href="https://github.com/pimoroni/bme680-python/blob/main/examples/indoor-air-quality.py">indoor-air-quality.py</a>
* <p>
* Under non-calibrated, general usage, it is recommended that the sensor "warm up" in the current mode for at least
* 30 minutes before taking readings. After that time, any kind of statistical baseline (e.g. average) for the gas
* and humidity readings can be used to get an indication of general air-quality.
* </p>
*
* @param gasReading current reading
* @param gasBaseline the "baseline" to score off of
* @param humidityReading current reading
* @param humidityBaseline the "baseline" to score off of
* @param humidityWeighting weighting applied to scoring (a good default is 0.25f)
*/
public static float airQuality(float gasReading, float gasBaseline, float humidityReading, float humidityBaseline,
float humidityWeighting) {
float gasOffset = gasBaseline - gasReading;
float humidityOffset = humidityReading - humidityBaseline;

float humidityScore;
if (humidityOffset > 0) {
humidityScore = (100 - humidityBaseline - humidityOffset) / (100 - humidityBaseline) * (humidityWeighting * 100);
}
else {
humidityScore = (humidityBaseline + humidityOffset) / humidityBaseline * (humidityWeighting * 100);
}

float gasScore;
if (gasOffset > 0) {
gasScore = (gasReading / gasBaseline) * (100 - (humidityWeighting * 100));
}
else {
gasScore = 100 - (humidityWeighting * 100);
}

// Calculate air_quality_score.
return humidityScore + gasScore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.ArrayList;
import java.util.List;

import com.diozero.devices.AirQualitySensorInterface;
import com.diozero.devices.BME68x;
import com.diozero.devices.BME68x.Data;
import com.diozero.devices.BME68x.HeaterConfig;
Expand Down Expand Up @@ -277,7 +278,7 @@ private static void iaqTest(BME68x bme68x) {
float hum = data[0].getHumidity();

// Calculate air_quality_score.
float air_quality_score = BME68x.airQuality(gas, gas_baseline, hum, hum_baseline, hum_weighting);
float air_quality_score = AirQualitySensorInterface.airQuality(gas, gas_baseline, hum, hum_baseline, hum_weighting);
System.out.format("Gas: %,.2f Ohms, humidity: %,.2f %%RH, air quality: %,.2f%n", gas, hum, air_quality_score);

SleepUtil.sleepSeconds(1);
Expand Down

0 comments on commit 741f0d1

Please sign in to comment.