Skip to content

Commit

Permalink
sht41: properly format serial number
Browse files Browse the repository at this point in the history
  • Loading branch information
dshil committed Nov 21, 2024
1 parent 30609df commit c261548
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
1 change: 1 addition & 0 deletions components/ocs_sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ idf_component_register(

"sht41/sensor.cpp"
"sht41/sensor_pipeline.cpp"
"sht41/serial_number_to_str.cpp"

"bme280/sensor.cpp"
"bme280/spi_sensor_pipeline.cpp"
Expand Down
10 changes: 5 additions & 5 deletions components/ocs_sensor/sht41/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ocs_algo/math_ops.h"
#include "ocs_core/log.h"
#include "ocs_sensor/sht41/sensor.h"
#include "ocs_sensor/sht41/serial_number_to_str.h"
#include "ocs_status/code_to_str.h"
#include "ocs_status/macros.h"

Expand Down Expand Up @@ -59,9 +60,10 @@ Sensor::Sensor(io::i2c::ITransceiver& transceiver,
}

ocs_logi(log_tag,
"serial_number=0x%8lX measure_command=%s "
"serial_number=%s measure_command=%s "
"heating_command=%s heating_delay=%lu(ms) heating_count=%u",
serial_number_, command_to_str_(params_.measure_command),
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_);
}
Expand Down Expand Up @@ -210,7 +212,6 @@ status::StatusCode Sensor::read_serial_number_() {
OCS_STATUS_RETURN_ON_ERROR(
transceiver_.receive(buf, sizeof(buf), params_.bus_wait_interval));

const uint16_t hi = algo::BitOps::pack_u8(buf[0], buf[1]);
const uint8_t hi_checksum = buf[2];
const uint8_t hi_checksum_calculated = calculate_crc(buf[0], buf[1]);
if (hi_checksum != hi_checksum_calculated) {
Expand All @@ -222,7 +223,6 @@ status::StatusCode Sensor::read_serial_number_() {
return status::StatusCode::InvalidState;
}

const uint16_t lo = algo::BitOps::pack_u8(buf[3], buf[4]);
const uint8_t lo_checksum = buf[5];
const uint8_t lo_checksum_calculated = calculate_crc(buf[3], buf[4]);
if (lo_checksum != lo_checksum_calculated) {
Expand All @@ -234,7 +234,7 @@ status::StatusCode Sensor::read_serial_number_() {
return status::StatusCode::InvalidState;
}

serial_number_ = algo::BitOps::pack_u16(hi, lo);
memcpy(serial_number_, buf, sizeof(serial_number_));

return status::StatusCode::OK;
}
Expand Down
3 changes: 2 additions & 1 deletion components/ocs_sensor/sht41/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ocs_core/spmc_node.h"
#include "ocs_io/i2c/itransceiver.h"
#include "ocs_scheduler/itask.h"
#include "ocs_sensor/sht41/serial_number.h"
#include "ocs_storage/istorage.h"

namespace ocs {
Expand Down Expand Up @@ -127,7 +128,7 @@ class Sensor : public scheduler::ITask, public core::NonCopyable<> {

TickType_t heating_delay_ { 0 };
unsigned heating_count_ { 0 };
uint32_t serial_number_ { 0 };
SerialNumber serial_number_ { 0 };

core::SpmcNode<Data> data_;
};
Expand Down
29 changes: 29 additions & 0 deletions components/ocs_sensor/sht41/serial_number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Open Control Systems authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <cstdint>

#define SHT_SENSOR_SERIAL_NUMBER_LEN 4

#define SHT_SENSOR_SERIAL_NUMBER_STR "%02X:%02X:%02X:%02X"

#define SHT_SENSOR_SERIAL_NUMBER_TO_STR(serial_number) \
serial_number[0], serial_number[1], serial_number[2], serial_number[3]

namespace ocs {
namespace sensor {
namespace sht41 {

//! SHT sensor serial number, MSB byte order.
using SerialNumber = uint8_t[SHT_SENSOR_SERIAL_NUMBER_LEN];

} // namespace sht41
} // namespace sensor
} // namespace ocs
34 changes: 34 additions & 0 deletions components/ocs_sensor/sht41/serial_number_to_str.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024, Open Control Systems authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <cstdio>
#include <cstring>

#include "freertos/FreeRTOSConfig.h"

#include "ocs_sensor/sht41/serial_number_to_str.h"

namespace ocs {
namespace sensor {
namespace sht41 {

serial_number_to_str::serial_number_to_str(const SerialNumber& serial_number) {
memset(buf_, 0, sizeof(buf_));

configASSERT(snprintf(buf_, sizeof(buf_), SHT_SENSOR_SERIAL_NUMBER_STR,
SHT_SENSOR_SERIAL_NUMBER_TO_STR(serial_number))
== str_length);
}

const char* serial_number_to_str::c_str() const {
return buf_;
}

} // namespace sht41
} // namespace sensor
} // namespace ocs
37 changes: 37 additions & 0 deletions components/ocs_sensor/sht41/serial_number_to_str.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024, Open Control Systems authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <cstring>

#include "ocs_core/noncopyable.h"
#include "ocs_sensor/sht41/serial_number.h"

namespace ocs {
namespace sensor {
namespace sht41 {

//! Format sensor serial number to string.
class serial_number_to_str : public core::NonCopyable<> {
public:
static const unsigned str_length = strlen("AA:BB:CC:DD");

//! Initialize.
serial_number_to_str(const SerialNumber& serial_number);

//! Return formatted serial number.
const char* c_str() const;

private:
char buf_[str_length + 1];
};

} // namespace sht41
} // namespace sensor
} // namespace ocs

0 comments on commit c261548

Please sign in to comment.