Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions components/ocs_sensor/sht4x/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ Sensor::Sensor(io::i2c::ITransceiver& transceiver,
configASSERT(params_.send_wait_interval);
configASSERT(params_.bus_wait_interval);

reset();

heating_delay_ = estimate_heating_delay_(params.heating_command);
configASSERT(heating_delay_);

Expand All @@ -51,22 +49,13 @@ Sensor::Sensor(io::i2c::ITransceiver& transceiver,
"failed to read sensor heating count from storage: code=%s",
status::code_to_str(code));
}

if (const auto code = read_serial_number_(); code != status::StatusCode::OK) {
ocs_loge(log_tag_.c_str(), "failed to read sensor serial number: code=%s",
status::code_to_str(code));
}

ocs_logi(log_tag_.c_str(),
"serial_number=%s measure_command=%s "
"heating_command=%s heating_delay=%lu(ms) heating_count=%u",
serial_number_to_str(serial_number_).c_str(),
command_to_str_(params_.measure_command),
command_to_str_(params.heating_command), pdTICKS_TO_MS(heating_delay_),
heating_count_);
}

status::StatusCode Sensor::run() {
if (!initialized_) {
OCS_STATUS_RETURN_ON_ERROR(initialize_());
}

OCS_STATUS_RETURN_ON_ERROR(send_command_(params_.measure_command));

Data data;
Expand Down Expand Up @@ -175,6 +164,28 @@ TickType_t Sensor::estimate_heating_delay_(Command command) {
return 0;
}

status::StatusCode Sensor::initialize_() {
const auto code = read_serial_number_();
if (code != status::StatusCode::OK) {
ocs_loge(log_tag_.c_str(), "failed to read sensor serial number: code=%s",
status::code_to_str(code));

return code;
}

ocs_logi(log_tag_.c_str(),
"initialized: serial_number=%s measure_command=%s "
"heating_command=%s heating_delay=%lu(ms) heating_count=%u",
serial_number_to_str(serial_number_).c_str(),
command_to_str_(params_.measure_command),
command_to_str_(params_.heating_command), pdTICKS_TO_MS(heating_delay_),
heating_count_);

initialized_ = true;

return status::StatusCode::OK;
}

status::StatusCode Sensor::reset_() {
OCS_STATUS_RETURN_ON_ERROR(send_command_(Command::SoftReset));

Expand Down
3 changes: 3 additions & 0 deletions components/ocs_sensor/sht4x/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Sensor : public scheduler::ITask, private core::NonCopyable<> {

static constexpr const char* heating_count_key_ = "heating_count";

status::StatusCode initialize_();
status::StatusCode reset_();
status::StatusCode heat_();
status::StatusCode read_serial_number_();
Expand Down Expand Up @@ -136,6 +137,8 @@ class Sensor : public scheduler::ITask, private core::NonCopyable<> {

TickType_t heating_delay_ { 0 };
unsigned heating_count_ { 0 };

bool initialized_ { false };
SerialNumber serial_number_ { 0 };

core::SpmcNode<Data> data_;
Expand Down
11 changes: 8 additions & 3 deletions projects/sht4x-verifier/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ extern "C" void app_main(void) {
io::i2c::IStore::TransferSpeed::Fast);
configASSERT(bus_transceiver);

const auto code = bus_transceiver->send(&io::i2c::IStore::bus_reset_command,
sizeof(io::i2c::IStore::bus_reset_command),
bus_wait_interval);
auto code = bus_transceiver->send(&io::i2c::IStore::bus_reset_command,
sizeof(io::i2c::IStore::bus_reset_command),
bus_wait_interval);
if (code != status::StatusCode::OK) {
ocs_logw(log_tag, "failed to reset i2c bus: %s", status::code_to_str(code));
}
Expand All @@ -66,6 +66,11 @@ extern "C" void app_main(void) {
}));
configASSERT(sensor);

code = sensor->reset();
if (code != status::StatusCode::OK) {
ocs_logw(log_tag, "failed to reset sensor: %s", status::code_to_str(code));
}

const unsigned total_attempts = CONFIG_OCS_TOOLS_SHT4x_VERIFIER_TOTAL_ATTEMPTS;
unsigned failed_attempts = 0;

Expand Down