t6615: dump more infos about sensor on startup

This commit is contained in:
Bernhard Urban-Forster 2023-12-31 16:45:48 +01:00
parent 773cd0f414
commit 53d5831837
2 changed files with 56 additions and 4 deletions

View file

@ -14,11 +14,14 @@ static const uint8_t T6615_COMMAND_GET_PPM[] = {0x02, 0x03};
static const uint8_t T6615_COMMAND_GET_SERIAL[] = {0x02, 0x01};
static const uint8_t T6615_COMMAND_GET_VERSION[] = {0x02, 0x0D};
static const uint8_t T6615_COMMAND_GET_ELEVATION[] = {0x02, 0x0F};
static const uint8_t T6615_COMMAND_GET_STATUS[] = {0xB6};
static const uint8_t T6615_COMMAND_GET_ABC[] = {0xB7, 0x00};
static const uint8_t T6615_COMMAND_ENABLE_ABC[] = {0xB7, 0x01};
static const uint8_t T6615_COMMAND_DISABLE_ABC[] = {0xB7, 0x02};
static const uint8_t T6615_COMMAND_SET_ELEVATION[] = {0x03, 0x0F};
static uint8_t bootup_state = 0;
void T6615Component::send_ppm_command_() {
this->command_time_ = millis();
this->command_ = T6615Command::GET_PPM;
@ -28,6 +31,24 @@ void T6615Component::send_ppm_command_() {
this->write_array(T6615_COMMAND_GET_PPM, sizeof(T6615_COMMAND_GET_PPM));
}
void T6615Component::send_serial_command_() {
this->command_time_ = millis();
this->command_ = T6615Command::GET_SERIAL;
this->write_byte(T6615_MAGIC);
this->write_byte(T6615_ADDR_SENSOR);
this->write_byte(sizeof(T6615_COMMAND_GET_SERIAL));
this->write_array(T6615_COMMAND_GET_SERIAL, sizeof(T6615_COMMAND_GET_SERIAL));
}
void T6615Component::send_status_command_() {
this->command_time_ = millis();
this->command_ = T6615Command::GET_STATUS;
this->write_byte(T6615_MAGIC);
this->write_byte(T6615_ADDR_SENSOR);
this->write_byte(sizeof(T6615_COMMAND_GET_STATUS));
this->write_array(T6615_COMMAND_GET_STATUS, sizeof(T6615_COMMAND_GET_STATUS));
}
void T6615Component::loop() {
if (this->available() < 5) {
if (this->command_ == T6615Command::GET_PPM && millis() - this->command_time_ > T6615_TIMEOUT) {
@ -39,10 +60,11 @@ void T6615Component::loop() {
return;
}
uint8_t response_buffer[6];
uint8_t response_buffer[4 + 15];
memset(response_buffer, '\0', 4 + 15);
/* by the time we get here, we know we have at least five bytes in the buffer */
this->read_array(response_buffer, 5);
/* by the time we get here, we know we have at least four bytes in the buffer */
this->read_array(response_buffer, 4);
// Read header
if (response_buffer[0] != T6615_MAGIC || response_buffer[1] != T6615_ADDR_HOST) {
@ -61,11 +83,24 @@ void T6615Component::loop() {
switch (this->command_) {
case T6615Command::GET_PPM: {
/* GET_PPM reply is 5 bytes long, read last byte */
this->read_array(response_buffer + 4, 1);
const uint16_t ppm = encode_uint16(response_buffer[3], response_buffer[4]);
ESP_LOGD(TAG, "T6615 Received CO₂=%uppm", ppm);
this->co2_sensor_->publish_state(ppm);
break;
}
case T6615Command::GET_SERIAL: {
/* GET_SERIAL reply is 4+15 bytes long, read last byte */
this->read_array(response_buffer + 4, 15);
ESP_LOGD(TAG, "T6615 Received serial=%s", response_buffer + 4);
break;
}
case T6615Command::GET_STATUS: {
const uint8_t status = response_buffer[3];
ESP_LOGD(TAG, "T6615 Received status=0x%02x", status);
break;
}
default:
break;
}
@ -73,7 +108,21 @@ void T6615Component::loop() {
this->command_ = T6615Command::NONE;
}
void T6615Component::update() { this->query_ppm_(); }
void T6615Component::update() {
switch (bootup_state) {
case 0:
this->send_serial_command_();
bootup_state++;
break;
case 1:
this->send_status_command_();
bootup_state++;
break;
default:
this->query_ppm_();
break;
}
}
void T6615Component::query_ppm_() {
if (this->co2_sensor_ == nullptr ||

View file

@ -14,6 +14,7 @@ enum class T6615Command : uint8_t {
GET_SERIAL,
GET_VERSION,
GET_ELEVATION,
GET_STATUS,
GET_ABC,
ENABLE_ABC,
DISABLE_ABC,
@ -33,6 +34,8 @@ class T6615Component : public PollingComponent, public uart::UARTDevice {
protected:
void query_ppm_();
void send_ppm_command_();
void send_serial_command_();
void send_status_command_();
T6615Command command_ = T6615Command::NONE;
uint32_t command_time_ = 0;