forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using BMP280 with SPI (esphome#7053)
Co-authored-by: Keith Burzinski <kbx81x@gmail.com>
- Loading branch information
Showing
32 changed files
with
387 additions
and
181 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,5 @@ | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.components import i2c, sensor | ||
from esphome.const import ( | ||
CONF_ID, | ||
CONF_PRESSURE, | ||
CONF_TEMPERATURE, | ||
DEVICE_CLASS_PRESSURE, | ||
DEVICE_CLASS_TEMPERATURE, | ||
STATE_CLASS_MEASUREMENT, | ||
UNIT_CELSIUS, | ||
UNIT_HECTOPASCAL, | ||
CONF_IIR_FILTER, | ||
CONF_OVERSAMPLING, | ||
) | ||
|
||
DEPENDENCIES = ["i2c"] | ||
|
||
bmp280_ns = cg.esphome_ns.namespace("bmp280") | ||
BMP280Oversampling = bmp280_ns.enum("BMP280Oversampling") | ||
OVERSAMPLING_OPTIONS = { | ||
"NONE": BMP280Oversampling.BMP280_OVERSAMPLING_NONE, | ||
"1X": BMP280Oversampling.BMP280_OVERSAMPLING_1X, | ||
"2X": BMP280Oversampling.BMP280_OVERSAMPLING_2X, | ||
"4X": BMP280Oversampling.BMP280_OVERSAMPLING_4X, | ||
"8X": BMP280Oversampling.BMP280_OVERSAMPLING_8X, | ||
"16X": BMP280Oversampling.BMP280_OVERSAMPLING_16X, | ||
} | ||
|
||
BMP280IIRFilter = bmp280_ns.enum("BMP280IIRFilter") | ||
IIR_FILTER_OPTIONS = { | ||
"OFF": BMP280IIRFilter.BMP280_IIR_FILTER_OFF, | ||
"2X": BMP280IIRFilter.BMP280_IIR_FILTER_2X, | ||
"4X": BMP280IIRFilter.BMP280_IIR_FILTER_4X, | ||
"8X": BMP280IIRFilter.BMP280_IIR_FILTER_8X, | ||
"16X": BMP280IIRFilter.BMP280_IIR_FILTER_16X, | ||
} | ||
|
||
BMP280Component = bmp280_ns.class_( | ||
"BMP280Component", cg.PollingComponent, i2c.I2CDevice | ||
) | ||
|
||
CONFIG_SCHEMA = ( | ||
cv.Schema( | ||
{ | ||
cv.GenerateID(): cv.declare_id(BMP280Component), | ||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( | ||
unit_of_measurement=UNIT_CELSIUS, | ||
accuracy_decimals=1, | ||
device_class=DEVICE_CLASS_TEMPERATURE, | ||
state_class=STATE_CLASS_MEASUREMENT, | ||
).extend( | ||
{ | ||
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( | ||
OVERSAMPLING_OPTIONS, upper=True | ||
), | ||
} | ||
), | ||
cv.Optional(CONF_PRESSURE): sensor.sensor_schema( | ||
unit_of_measurement=UNIT_HECTOPASCAL, | ||
accuracy_decimals=1, | ||
device_class=DEVICE_CLASS_PRESSURE, | ||
state_class=STATE_CLASS_MEASUREMENT, | ||
).extend( | ||
{ | ||
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( | ||
OVERSAMPLING_OPTIONS, upper=True | ||
), | ||
} | ||
), | ||
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum( | ||
IIR_FILTER_OPTIONS, upper=True | ||
), | ||
} | ||
) | ||
.extend(cv.polling_component_schema("60s")) | ||
.extend(i2c.i2c_device_schema(0x77)) | ||
CONFIG_SCHEMA = cv.invalid( | ||
"The bmp280 sensor component has been renamed to bmp280_i2c." | ||
) | ||
|
||
|
||
async def to_code(config): | ||
var = cg.new_Pvariable(config[CONF_ID]) | ||
await cg.register_component(var, config) | ||
await i2c.register_i2c_device(var, config) | ||
|
||
if temperature_config := config.get(CONF_TEMPERATURE): | ||
sens = await sensor.new_sensor(temperature_config) | ||
cg.add(var.set_temperature_sensor(sens)) | ||
cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING])) | ||
|
||
if pressure_config := config.get(CONF_PRESSURE): | ||
sens = await sensor.new_sensor(pressure_config) | ||
cg.add(var.set_pressure_sensor(sens)) | ||
cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING])) | ||
|
||
cg.add(var.set_iir_filter(config[CONF_IIR_FILTER])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.components import sensor | ||
from esphome.const import ( | ||
CONF_ID, | ||
CONF_IIR_FILTER, | ||
CONF_OVERSAMPLING, | ||
CONF_PRESSURE, | ||
CONF_TEMPERATURE, | ||
DEVICE_CLASS_PRESSURE, | ||
DEVICE_CLASS_TEMPERATURE, | ||
STATE_CLASS_MEASUREMENT, | ||
UNIT_CELSIUS, | ||
UNIT_HECTOPASCAL, | ||
) | ||
|
||
CODEOWNERS = ["@ademuri"] | ||
|
||
bmp280_ns = cg.esphome_ns.namespace("bmp280_base") | ||
BMP280Oversampling = bmp280_ns.enum("BMP280Oversampling") | ||
OVERSAMPLING_OPTIONS = { | ||
"NONE": BMP280Oversampling.BMP280_OVERSAMPLING_NONE, | ||
"1X": BMP280Oversampling.BMP280_OVERSAMPLING_1X, | ||
"2X": BMP280Oversampling.BMP280_OVERSAMPLING_2X, | ||
"4X": BMP280Oversampling.BMP280_OVERSAMPLING_4X, | ||
"8X": BMP280Oversampling.BMP280_OVERSAMPLING_8X, | ||
"16X": BMP280Oversampling.BMP280_OVERSAMPLING_16X, | ||
} | ||
|
||
BMP280IIRFilter = bmp280_ns.enum("BMP280IIRFilter") | ||
IIR_FILTER_OPTIONS = { | ||
"OFF": BMP280IIRFilter.BMP280_IIR_FILTER_OFF, | ||
"2X": BMP280IIRFilter.BMP280_IIR_FILTER_2X, | ||
"4X": BMP280IIRFilter.BMP280_IIR_FILTER_4X, | ||
"8X": BMP280IIRFilter.BMP280_IIR_FILTER_8X, | ||
"16X": BMP280IIRFilter.BMP280_IIR_FILTER_16X, | ||
} | ||
|
||
CONFIG_SCHEMA_BASE = cv.Schema( | ||
{ | ||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( | ||
unit_of_measurement=UNIT_CELSIUS, | ||
accuracy_decimals=1, | ||
device_class=DEVICE_CLASS_TEMPERATURE, | ||
state_class=STATE_CLASS_MEASUREMENT, | ||
).extend( | ||
{ | ||
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( | ||
OVERSAMPLING_OPTIONS, upper=True | ||
), | ||
} | ||
), | ||
cv.Optional(CONF_PRESSURE): sensor.sensor_schema( | ||
unit_of_measurement=UNIT_HECTOPASCAL, | ||
accuracy_decimals=1, | ||
device_class=DEVICE_CLASS_PRESSURE, | ||
state_class=STATE_CLASS_MEASUREMENT, | ||
).extend( | ||
{ | ||
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( | ||
OVERSAMPLING_OPTIONS, upper=True | ||
), | ||
} | ||
), | ||
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum( | ||
IIR_FILTER_OPTIONS, upper=True | ||
), | ||
} | ||
).extend(cv.polling_component_schema("60s")) | ||
|
||
|
||
async def to_code_base(config): | ||
var = cg.new_Pvariable(config[CONF_ID]) | ||
await cg.register_component(var, config) | ||
|
||
if temperature_config := config.get(CONF_TEMPERATURE): | ||
sens = await sensor.new_sensor(temperature_config) | ||
cg.add(var.set_temperature_sensor(sens)) | ||
cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING])) | ||
|
||
if pressure_config := config.get(CONF_PRESSURE): | ||
sens = await sensor.new_sensor(pressure_config) | ||
cg.add(var.set_pressure_sensor(sens)) | ||
cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING])) | ||
|
||
cg.add(var.set_iir_filter(config[CONF_IIR_FILTER])) | ||
|
||
return var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "bmp280_i2c.h" | ||
#include "esphome/core/hal.h" | ||
#include "esphome/core/log.h" | ||
|
||
namespace esphome { | ||
namespace bmp280_i2c { | ||
|
||
bool BMP280I2CComponent::read_byte(uint8_t a_register, uint8_t *data) { | ||
return I2CDevice::read_byte(a_register, data); | ||
}; | ||
bool BMP280I2CComponent::write_byte(uint8_t a_register, uint8_t data) { | ||
return I2CDevice::write_byte(a_register, data); | ||
}; | ||
bool BMP280I2CComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) { | ||
return I2CDevice::read_bytes(a_register, data, len); | ||
}; | ||
bool BMP280I2CComponent::read_byte_16(uint8_t a_register, uint16_t *data) { | ||
return I2CDevice::read_byte_16(a_register, data); | ||
}; | ||
|
||
void BMP280I2CComponent::dump_config() { | ||
LOG_I2C_DEVICE(this); | ||
BMP280Component::dump_config(); | ||
} | ||
|
||
} // namespace bmp280_i2c | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include "esphome/components/bmp280_base/bmp280_base.h" | ||
#include "esphome/components/i2c/i2c.h" | ||
|
||
namespace esphome { | ||
namespace bmp280_i2c { | ||
|
||
static const char *const TAG = "bmp280_i2c.sensor"; | ||
|
||
/// This class implements support for the BMP280 Temperature+Pressure i2c sensor. | ||
class BMP280I2CComponent : public esphome::bmp280_base::BMP280Component, public i2c::I2CDevice { | ||
public: | ||
bool read_byte(uint8_t a_register, uint8_t *data) override; | ||
bool write_byte(uint8_t a_register, uint8_t data) override; | ||
bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override; | ||
bool read_byte_16(uint8_t a_register, uint16_t *data) override; | ||
void dump_config() override; | ||
}; | ||
|
||
} // namespace bmp280_i2c | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.components import i2c | ||
from ..bmp280_base import to_code_base, CONFIG_SCHEMA_BASE | ||
|
||
AUTO_LOAD = ["bmp280_base"] | ||
CODEOWNERS = ["@ademuri"] | ||
DEPENDENCIES = ["i2c"] | ||
|
||
bmp280_ns = cg.esphome_ns.namespace("bmp280_i2c") | ||
BMP280I2CComponent = bmp280_ns.class_( | ||
"BMP280I2CComponent", cg.PollingComponent, i2c.I2CDevice | ||
) | ||
|
||
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend( | ||
i2c.i2c_device_schema(default_address=0x77) | ||
).extend({cv.GenerateID(): cv.declare_id(BMP280I2CComponent)}) | ||
|
||
|
||
async def to_code(config): | ||
var = await to_code_base(config) | ||
await i2c.register_i2c_device(var, config) |
Empty file.
Oops, something went wrong.