From f618da58d5e285f25aa205b503a4f0d22d12ddc3 Mon Sep 17 00:00:00 2001 From: PonomarevDA Date: Mon, 11 Nov 2024 00:35:23 +0300 Subject: [PATCH] add simple tests for sht3x and refactor the driver --- Makefile | 6 +- Src/drivers/as5600/tests/CMakeLists.txt | 2 +- Src/drivers/sht3x/sht3x.cpp | 75 +++++++++++++------------ Src/drivers/sht3x/sht3x.hpp | 52 +++++++++-------- Src/drivers/sht3x/tests/CMakeLists.txt | 28 +++++++++ Src/drivers/sht3x/tests/test_sht3x.cpp | 29 ++++++++++ 6 files changed, 130 insertions(+), 62 deletions(-) create mode 100644 Src/drivers/sht3x/tests/CMakeLists.txt create mode 100644 Src/drivers/sht3x/tests/test_sht3x.cpp diff --git a/Makefile b/Makefile index b40d0cc..b44e9ad 100644 --- a/Makefile +++ b/Makefile @@ -56,8 +56,10 @@ code_style: cpplint Src/modules/*/*pp Src/peripheral/*/*pp Src/platform/*/*pp tests: - mkdir -p ${BUILD_DIR}/tests - cd ${BUILD_DIR}/tests && cmake ../../Src/drivers/as5600/tests && make && ./test_as5600 + mkdir -p ${BUILD_DIR}/tests/as5600 + mkdir -p ${BUILD_DIR}/tests/sht3x + cd ${BUILD_DIR}/tests/as5600 && cmake ../../../Src/drivers/as5600/tests && make && ./test_as5600 + cd ${BUILD_DIR}/tests/sht3x && cmake ../../../Src/drivers/sht3x/tests && make && ./test_sht3x upload: LATEST_TARGET=$$(ls -td ${BUILD_DIR}/release/*.bin | head -1) && ./scripts/tools/stm32/flash.sh $$LATEST_TARGET diff --git a/Src/drivers/as5600/tests/CMakeLists.txt b/Src/drivers/as5600/tests/CMakeLists.txt index 0870f42..880b5bb 100644 --- a/Src/drivers/as5600/tests/CMakeLists.txt +++ b/Src/drivers/as5600/tests/CMakeLists.txt @@ -6,7 +6,7 @@ project(as5600) add_library(as5600 ../as5600.cpp - ../../Src/platform/ubuntu/i2c.cpp + ../../../Src/platform/ubuntu/i2c.cpp ) target_include_directories(as5600 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/Src/drivers/sht3x/sht3x.cpp b/Src/drivers/sht3x/sht3x.cpp index 1f3969a..3d0f371 100644 --- a/Src/drivers/sht3x/sht3x.cpp +++ b/Src/drivers/sht3x/sht3x.cpp @@ -1,47 +1,19 @@ -#include "peripheral/sht3x/sht3x.hpp" +/** + * This program is free software under the GNU General Public License v3. + * See for details. + */ -#include - -#include "main.h" +#include "sht3x.hpp" #include "peripheral/i2c/i2c.hpp" -static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb) { - return (uint16_t)((uint16_t)msb << 8u) | lsb; -} - -static uint8_t calculate_crc(const uint8_t *data, size_t length) { - uint8_t crc = 0xff; - for (size_t i = 0; i < length; i++) { - crc ^= data[i]; - for (size_t j = 0; j < 8; j++) { - if ((crc & 0x80u) != 0) { - crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u); - } else { - crc <<= 1u; - } - } - } - return crc; -} - -bool SHT3XPeriphery::sendCommand(const SHT3XHandle &handle, SHT3XCommand command) { - uint8_t command_buffer[2] = {(uint8_t)((uint16_t)command >> 8u), - (uint8_t)((uint16_t)command & 0xffu)}; - - return HAL::I2C::transmit(handle.device_address << 1u, command_buffer, - sizeof(command_buffer)); -} +namespace Driver { -bool SHT3XPeriphery::readTemperatureHumidity(const SHT3XHandle &handle, - float *temperature, - float *humidity) { - sendCommand(handle, SHT3XCommand::SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH); - HAL_Delay(1); +bool SHT3X::read(float *temperature, float *humidity) const { + sendCommand(device_address, SHT3XCommand::SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH); uint8_t buffer[6]; - if (!HAL::I2C::receive(handle.device_address << 1u, buffer, - sizeof(buffer))) { + if (!HAL::I2C::receive(device_address << 1u, buffer, sizeof(buffer))) { return false; } @@ -59,3 +31,32 @@ bool SHT3XPeriphery::readTemperatureHumidity(const SHT3XHandle &handle, return true; } + +bool SHT3X::sendCommand(uint8_t device_address, SHT3XCommand command) { + uint8_t command_buffer[2] = {(uint8_t)((uint16_t)command >> 8u), + (uint8_t)((uint16_t)command & 0xffu)}; + + return HAL::I2C::transmit(device_address << 1u, command_buffer, + sizeof(command_buffer)); +} + +uint16_t SHT3X::uint8_to_uint16(uint8_t msb, uint8_t lsb) { + return (uint16_t)((uint16_t)msb << 8u) | lsb; +} + +uint8_t SHT3X::calculate_crc(const uint8_t *data, size_t length) { + uint8_t crc = 0xff; + for (size_t i = 0; i < length; i++) { + crc ^= data[i]; + for (size_t j = 0; j < 8; j++) { + if ((crc & 0x80u) != 0) { + crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u); + } else { + crc <<= 1u; + } + } + } + return crc; +} + +} // namespace Driver diff --git a/Src/drivers/sht3x/sht3x.hpp b/Src/drivers/sht3x/sht3x.hpp index 9ca92ab..c84b892 100644 --- a/Src/drivers/sht3x/sht3x.hpp +++ b/Src/drivers/sht3x/sht3x.hpp @@ -1,10 +1,15 @@ +/** + * This program is free software under the GNU General Public License v3. + * See for details. + */ + #ifndef SRC_DRIVERS_SHT3X_SHT3X_HPP_ #define SRC_DRIVERS_SHT3X_SHT3X_HPP_ #include +#include -#define SHT3X_I2C_DEVICE_ADDRESS_ADDR_PIN_LOW 0x44 -#define SHT3X_I2C_DEVICE_ADDRESS_ADDR_PIN_HIGH 0x45 +namespace Driver { enum class SHT3XCommand { SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH = 0x2c06, @@ -18,34 +23,37 @@ enum class SHT3XCommand { SHT3X_COMMAND_MEASURE_LOWREP_10HZ = 0x272a }; -struct SHT3XHandle { - /** - * The I2C device address. - * @see{PCA9865_I2C_DEVICE_ADDRESS_ADDR_PIN_LOW} and - * @see{SHT3X_I2C_DEVICE_ADDRESS_ADDR_PIN_HIGH} - */ - uint16_t device_address; -}; -class SHT3XPeriphery { - public: +class SHT3X { +public: + static constexpr uint8_t DEV_ADDR_PIN_LOW = 0x44; + static constexpr uint8_t DEV_ADDR_PIN_HIGH = 0x45; + + SHT3X(uint8_t dev_addr): device_address(dev_addr) {} + /** - * Execute a command defined in SHT3XCommand - * @param handle Handle to the SHT3x device. - * @param command SHT3XCommand + * @brief Takes a single temperature and humidity measurement. + * @param temperature Pointer to the storage location for the sampled temperature. + * @param humidity Pointer to the storage location for the sampled humidity. * @return True on success, false otherwise. */ - static bool sendCommand(const SHT3XHandle &handle, SHT3XCommand command); + bool read(float* temperature, float* humidity) const; + +private: /** - * Takes a single temperature and humidity measurement. + * @brief Execute a command defined in SHT3XCommand * @param handle Handle to the SHT3x device. - * @param temperature Pointer to the storage location for the sampled - * temperature. - * @param humidity Pointer to the storage location for the sampled humidity. + * @param command SHT3XCommand * @return True on success, false otherwise. */ - static bool readTemperatureHumidity(const SHT3XHandle &handle, float *temperature, - float *humidity); + static bool sendCommand(uint8_t device_address, SHT3XCommand command); + + static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb); + static uint8_t calculate_crc(const uint8_t* data, size_t length); + + uint8_t device_address; }; +} // namespace Driver + #endif // SRC_DRIVERS_SHT3X_SHT3X_HPP_ diff --git a/Src/drivers/sht3x/tests/CMakeLists.txt b/Src/drivers/sht3x/tests/CMakeLists.txt new file mode 100644 index 0000000..8276345 --- /dev/null +++ b/Src/drivers/sht3x/tests/CMakeLists.txt @@ -0,0 +1,28 @@ +# Copyright (C) 2024 Dmitry Ponomarev +# Distributed under the terms of the GPL v3 license, available in the file LICENSE. + +cmake_minimum_required(VERSION 3.10) +project(sht3x) + +add_library(sht3x + ../sht3x.cpp + ../../../Src/platform/ubuntu/i2c.cpp +) + +target_include_directories(sht3x PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +find_package(GTest REQUIRED) +include_directories( + ${GTEST_INCLUDE_DIRS} + ../../../../Src +) + +add_executable(test_sht3x + test_sht3x.cpp +) + +target_link_libraries(test_sht3x + sht3x + GTest::GTest + GTest::Main +) diff --git a/Src/drivers/sht3x/tests/test_sht3x.cpp b/Src/drivers/sht3x/tests/test_sht3x.cpp new file mode 100644 index 0000000..7251c5a --- /dev/null +++ b/Src/drivers/sht3x/tests/test_sht3x.cpp @@ -0,0 +1,29 @@ +/** + * This program is free software under the GNU General Public License v3. + * See for details. + * Author: Dmitry Ponomarev + */ + +#include +#include "drivers/sht3x/sht3x.hpp" + +TEST(Shx3xTest, HelloWorld) { + EXPECT_EQ(1, 1); +} + +TEST(Shx3xTest, init) { + Driver::SHT3X sht3x(Driver::SHT3X::DEV_ADDR_PIN_LOW); +} + +TEST(Shx3xTest, read) { + Driver::SHT3X sht3x(Driver::SHT3X::DEV_ADDR_PIN_LOW); + + float temperature; + float humidity; + sht3x.read(&temperature, &humidity); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file