ESPHome  2024.3.2
mcp9600.cpp
Go to the documentation of this file.
1 #include "mcp9600.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mcp9600 {
6 
7 static const char *const TAG = "mcp9600";
8 
9 static const uint8_t MCP9600_REGISTER_HOT_JUNCTION = 0x00;
10 // static const uint8_t MCP9600_REGISTER_JUNCTION_DELTA = 0x01; // Unused, but kept for future reference
11 static const uint8_t MCP9600_REGISTER_COLD_JUNTION = 0x02;
12 // static const uint8_t MCP9600_REGISTER_RAW_DATA_ADC = 0x03; // Unused, but kept for future reference
13 static const uint8_t MCP9600_REGISTER_STATUS = 0x04;
14 static const uint8_t MCP9600_REGISTER_SENSOR_CONFIG = 0x05;
15 static const uint8_t MCP9600_REGISTER_CONFIG = 0x06;
16 static const uint8_t MCP9600_REGISTER_ALERT1_CONFIG = 0x08;
17 static const uint8_t MCP9600_REGISTER_ALERT2_CONFIG = 0x09;
18 static const uint8_t MCP9600_REGISTER_ALERT3_CONFIG = 0x0A;
19 static const uint8_t MCP9600_REGISTER_ALERT4_CONFIG = 0x0B;
20 static const uint8_t MCP9600_REGISTER_ALERT1_HYSTERESIS = 0x0C;
21 static const uint8_t MCP9600_REGISTER_ALERT2_HYSTERESIS = 0x0D;
22 static const uint8_t MCP9600_REGISTER_ALERT3_HYSTERESIS = 0x0E;
23 static const uint8_t MCP9600_REGISTER_ALERT4_HYSTERESIS = 0x0F;
24 static const uint8_t MCP9600_REGISTER_ALERT1_LIMIT = 0x10;
25 static const uint8_t MCP9600_REGISTER_ALERT2_LIMIT = 0x11;
26 static const uint8_t MCP9600_REGISTER_ALERT3_LIMIT = 0x12;
27 static const uint8_t MCP9600_REGISTER_ALERT4_LIMIT = 0x13;
28 static const uint8_t MCP9600_REGISTER_DEVICE_ID = 0x20;
29 
31  ESP_LOGCONFIG(TAG, "Setting up MCP9600...");
32 
33  uint16_t dev_id = 0;
34  this->read_byte_16(MCP9600_REGISTER_DEVICE_ID, &dev_id);
35  this->device_id_ = (uint8_t) (dev_id >> 8);
36 
37  // Allows both MCP9600's and MCP9601's to be connected.
38  if (this->device_id_ != (uint8_t) 0x40 && this->device_id_ != (uint8_t) 0x41) {
39  this->error_code_ = COMMUNICATION_FAILED;
40  this->mark_failed();
41  return;
42  }
43 
44  bool success = this->write_byte(MCP9600_REGISTER_STATUS, 0x00);
45  success |= this->write_byte(MCP9600_REGISTER_SENSOR_CONFIG, uint8_t(0x00 | thermocouple_type_ << 4));
46  success |= this->write_byte(MCP9600_REGISTER_CONFIG, 0x00);
47  success |= this->write_byte(MCP9600_REGISTER_ALERT1_CONFIG, 0x00);
48  success |= this->write_byte(MCP9600_REGISTER_ALERT2_CONFIG, 0x00);
49  success |= this->write_byte(MCP9600_REGISTER_ALERT3_CONFIG, 0x00);
50  success |= this->write_byte(MCP9600_REGISTER_ALERT4_CONFIG, 0x00);
51  success |= this->write_byte(MCP9600_REGISTER_ALERT1_HYSTERESIS, 0x00);
52  success |= this->write_byte(MCP9600_REGISTER_ALERT2_HYSTERESIS, 0x00);
53  success |= this->write_byte(MCP9600_REGISTER_ALERT3_HYSTERESIS, 0x00);
54  success |= this->write_byte(MCP9600_REGISTER_ALERT4_HYSTERESIS, 0x00);
55  success |= this->write_byte_16(MCP9600_REGISTER_ALERT1_LIMIT, 0x0000);
56  success |= this->write_byte_16(MCP9600_REGISTER_ALERT2_LIMIT, 0x0000);
57  success |= this->write_byte_16(MCP9600_REGISTER_ALERT3_LIMIT, 0x0000);
58  success |= this->write_byte_16(MCP9600_REGISTER_ALERT4_LIMIT, 0x0000);
59 
60  if (!success) {
61  this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
62  this->mark_failed();
63  return;
64  }
65 }
66 
68  ESP_LOGCONFIG(TAG, "MCP9600:");
69  LOG_I2C_DEVICE(this);
70  LOG_UPDATE_INTERVAL(this);
71 
72  ESP_LOGCONFIG(TAG, " Device ID: 0x%x", this->device_id_);
73 
74  LOG_SENSOR(" ", "Hot Junction Temperature", this->hot_junction_sensor_);
75  LOG_SENSOR(" ", "Cold Junction Temperature", this->cold_junction_sensor_);
76 
77  switch (this->error_code_) {
79  ESP_LOGE(TAG, "Connected device does not match a known MCP9600 or MCP901 sensor");
80  break;
82  ESP_LOGE(TAG, "Failed to update device configuration");
83  break;
84  case NONE:
85  default:
86  break;
87  }
88 }
89 
91  if (this->hot_junction_sensor_ != nullptr) {
92  uint16_t raw_hot_junction_temperature;
93  if (!this->read_byte_16(MCP9600_REGISTER_HOT_JUNCTION, &raw_hot_junction_temperature)) {
94  this->status_set_warning();
95  return;
96  }
97  float hot_junction_temperature = int16_t(raw_hot_junction_temperature) * 0.0625;
98  this->hot_junction_sensor_->publish_state(hot_junction_temperature);
99  }
100 
101  if (this->cold_junction_sensor_ != nullptr) {
102  uint16_t raw_cold_junction_temperature;
103  if (!this->read_byte_16(MCP9600_REGISTER_COLD_JUNTION, &raw_cold_junction_temperature)) {
104  this->status_set_warning();
105  return;
106  }
107  float cold_junction_temperature = int16_t(raw_cold_junction_temperature) * 0.0625;
108  this->cold_junction_sensor_->publish_state(cold_junction_temperature);
109  }
110 
111  this->status_clear_warning();
112 }
113 
114 } // namespace mcp9600
115 } // namespace esphome
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
sensor::Sensor * cold_junction_sensor_
Definition: mcp9600.h:39
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:146
enum esphome::mcp9600::MCP9600Component::ErrorCode NONE
MCP9600ThermocoupleType thermocouple_type_
Definition: mcp9600.h:41
void status_clear_warning()
Definition: component.cpp:161
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:113
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:266
sensor::Sensor * hot_junction_sensor_
Definition: mcp9600.h:38