Implement all supported thermocouple types for MAX31856 (#7218)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com>
This commit is contained in:
ArkanStasarik 2024-09-11 12:53:09 +08:00 committed by GitHub
parent ffc2b58714
commit dbecade122
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 50 additions and 5 deletions

View file

@ -32,6 +32,12 @@ void MAX31856Sensor::dump_config() {
LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" CS Pin: ", this->cs_);
ESP_LOGCONFIG(TAG, " Mains Filter: %s", ESP_LOGCONFIG(TAG, " Mains Filter: %s",
(filter_ == FILTER_60HZ ? "60 Hz" : (filter_ == FILTER_50HZ ? "50 Hz" : "Unknown!"))); (filter_ == FILTER_60HZ ? "60 Hz" : (filter_ == FILTER_50HZ ? "50 Hz" : "Unknown!")));
if (this->thermocouple_type_ < 0 || this->thermocouple_type_ > 7) {
ESP_LOGCONFIG(TAG, " Thermocouple Type: Unknown");
} else {
ESP_LOGCONFIG(TAG, " Thermocouple Type: %c", "BEJKNRST"[this->thermocouple_type_]);
}
LOG_UPDATE_INTERVAL(this); LOG_UPDATE_INTERVAL(this);
} }
@ -129,7 +135,12 @@ void MAX31856Sensor::clear_fault_() {
} }
void MAX31856Sensor::set_thermocouple_type_() { void MAX31856Sensor::set_thermocouple_type_() {
MAX31856ThermocoupleType type = MAX31856_TCTYPE_K; MAX31856ThermocoupleType type;
if (this->thermocouple_type_ < 0 || this->thermocouple_type_ > 7) {
type = MAX31856_TCTYPE_K;
} else {
type = this->thermocouple_type_;
}
ESP_LOGCONFIG(TAG, "set_thermocouple_type_: 0x%02X", type); ESP_LOGCONFIG(TAG, "set_thermocouple_type_: 0x%02X", type);
uint8_t t = this->read_register_(MAX31856_CR1_REG); uint8_t t = this->read_register_(MAX31856_CR1_REG);
t &= 0xF0; // mask off bottom 4 bits t &= 0xF0; // mask off bottom 4 bits

View file

@ -50,7 +50,6 @@ enum MAX31856Registers {
/** /**
* Multiple types of thermocouples supported by the chip. * Multiple types of thermocouples supported by the chip.
* Currently only K type implemented here.
*/ */
enum MAX31856ThermocoupleType { enum MAX31856ThermocoupleType {
MAX31856_TCTYPE_B = 0b0000, // 0x00 MAX31856_TCTYPE_B = 0b0000, // 0x00
@ -78,11 +77,15 @@ class MAX31856Sensor : public sensor::Sensor,
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
float get_setup_priority() const override; float get_setup_priority() const override;
void set_filter(MAX31856ConfigFilter filter) { filter_ = filter; } void set_filter(MAX31856ConfigFilter filter) { this->filter_ = filter; }
void set_thermocouple_type(MAX31856ThermocoupleType thermocouple_type) {
this->thermocouple_type_ = thermocouple_type;
}
void update() override; void update() override;
protected: protected:
MAX31856ConfigFilter filter_; MAX31856ConfigFilter filter_;
MAX31856ThermocoupleType thermocouple_type_;
uint8_t read_register_(uint8_t reg); uint8_t read_register_(uint8_t reg);
uint32_t read_register24_(uint8_t reg); uint32_t read_register24_(uint8_t reg);

View file

@ -3,6 +3,7 @@ from esphome.components import sensor, spi
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_MAINS_FILTER, CONF_MAINS_FILTER,
CONF_THERMOCOUPLE_TYPE,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS, UNIT_CELSIUS,
@ -18,6 +19,17 @@ FILTER = {
50: MAX31865ConfigFilter.FILTER_50HZ, 50: MAX31865ConfigFilter.FILTER_50HZ,
60: MAX31865ConfigFilter.FILTER_60HZ, 60: MAX31865ConfigFilter.FILTER_60HZ,
} }
MAX31856ThermocoupleType = max31856_ns.enum("MAX31856ThermocoupleType")
THERMOCOUPLE_TYPE = {
"B": MAX31856ThermocoupleType.MAX31856_TCTYPE_B,
"E": MAX31856ThermocoupleType.MAX31856_TCTYPE_E,
"J": MAX31856ThermocoupleType.MAX31856_TCTYPE_J,
"K": MAX31856ThermocoupleType.MAX31856_TCTYPE_K,
"N": MAX31856ThermocoupleType.MAX31856_TCTYPE_N,
"R": MAX31856ThermocoupleType.MAX31856_TCTYPE_R,
"S": MAX31856ThermocoupleType.MAX31856_TCTYPE_S,
"T": MAX31856ThermocoupleType.MAX31856_TCTYPE_T,
}
CONFIG_SCHEMA = ( CONFIG_SCHEMA = (
sensor.sensor_schema( sensor.sensor_schema(
@ -34,6 +46,13 @@ CONFIG_SCHEMA = (
), ),
} }
) )
.extend(
{
cv.Optional(CONF_THERMOCOUPLE_TYPE, default="K"): cv.enum(
THERMOCOUPLE_TYPE, upper=True, space=""
),
}
)
.extend(cv.polling_component_schema("60s")) .extend(cv.polling_component_schema("60s"))
.extend(spi.spi_device_schema()) .extend(spi.spi_device_schema())
) )
@ -44,3 +63,4 @@ async def to_code(config):
await cg.register_component(var, config) await cg.register_component(var, config)
await spi.register_spi_device(var, config) await spi.register_spi_device(var, config)
cg.add(var.set_filter(config[CONF_MAINS_FILTER])) cg.add(var.set_filter(config[CONF_MAINS_FILTER]))
cg.add(var.set_thermocouple_type(config[CONF_THERMOCOUPLE_TYPE]))

View file

@ -1,14 +1,14 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor from esphome.components import i2c, sensor
import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_ID, CONF_ID,
CONF_THERMOCOUPLE_TYPE,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS, UNIT_CELSIUS,
) )
CONF_THERMOCOUPLE_TYPE = "thermocouple_type"
CONF_HOT_JUNCTION = "hot_junction" CONF_HOT_JUNCTION = "hot_junction"
CONF_COLD_JUNCTION = "cold_junction" CONF_COLD_JUNCTION = "cold_junction"

View file

@ -852,6 +852,7 @@ CONF_TEMPERATURE_STEP = "temperature_step"
CONF_TEXT = "text" CONF_TEXT = "text"
CONF_TEXT_SENSORS = "text_sensors" CONF_TEXT_SENSORS = "text_sensors"
CONF_THEN = "then" CONF_THEN = "then"
CONF_THERMOCOUPLE_TYPE = "thermocouple_type"
CONF_THRESHOLD = "threshold" CONF_THRESHOLD = "threshold"
CONF_THROTTLE = "throttle" CONF_THROTTLE = "throttle"
CONF_TILT = "tilt" CONF_TILT = "tilt"

View file

@ -10,3 +10,4 @@ sensor:
cs_pin: 12 cs_pin: 12
update_interval: 15s update_interval: 15s
mains_filter: 50Hz mains_filter: 50Hz
thermocouple_type: N

View file

@ -10,3 +10,5 @@ sensor:
cs_pin: 8 cs_pin: 8
update_interval: 15s update_interval: 15s
mains_filter: 50Hz mains_filter: 50Hz
thermocouple_type: N

View file

@ -10,3 +10,5 @@ sensor:
cs_pin: 8 cs_pin: 8
update_interval: 15s update_interval: 15s
mains_filter: 50Hz mains_filter: 50Hz
thermocouple_type: N

View file

@ -10,3 +10,4 @@ sensor:
cs_pin: 12 cs_pin: 12
update_interval: 15s update_interval: 15s
mains_filter: 50Hz mains_filter: 50Hz
thermocouple_type: N

View file

@ -10,3 +10,5 @@ sensor:
cs_pin: 15 cs_pin: 15
update_interval: 15s update_interval: 15s
mains_filter: 50Hz mains_filter: 50Hz
thermocouple_type: N

View file

@ -10,3 +10,5 @@ sensor:
cs_pin: 6 cs_pin: 6
update_interval: 15s update_interval: 15s
mains_filter: 50Hz mains_filter: 50Hz
thermocouple_type: N