From b5d596cb56bcb11debd1304d0ddf95c68b69631e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Treffenst=C3=A4dt?= Date: Sat, 16 Apr 2022 11:48:15 +0200 Subject: [PATCH 1/5] Added skeleton of LM75 class. --- cmake/targets/detector.cmake | 2 ++ include/muonpi/serial/i2cdevices/lm75.h | 25 +++++++++++++++++++++++++ src/serial/i2cdevices/lm75.cpp | 0 3 files changed, 27 insertions(+) create mode 100644 include/muonpi/serial/i2cdevices/lm75.h create mode 100644 src/serial/i2cdevices/lm75.cpp diff --git a/cmake/targets/detector.cmake b/cmake/targets/detector.cmake index fec9942..20f2b91 100644 --- a/cmake/targets/detector.cmake +++ b/cmake/targets/detector.cmake @@ -5,6 +5,7 @@ set(DETECTOR_SOURCE_FILES "${PROJECT_SRC_DIR}/serial/i2cdevice.cpp" "${PROJECT_SRC_DIR}/serial/i2cbus.cpp" "${PROJECT_SRC_DIR}/serial/i2cdevices/generalcall.cpp" + "${PROJECT_SRC_DIR}/serial/i2cdevices/lm75.cpp" ) set(DETECTOR_HEADER_FILES @@ -16,6 +17,7 @@ set(DETECTOR_HEADER_FILES "${PROJECT_HEADER_DIR}/muonpi/serial/i2cbus.h" "${PROJECT_HEADER_DIR}/muonpi/serial/i2ceeprom.h" "${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/generalcall.h" + "${PROJECT_HEADER_DIR}/muonpi/serial/i2cdevices/lm75.h" ) if (NOT LIBMUONPI_FORMAT_ONLY) if (LIBMUONPI_BUILD_DETECTOR) # libraries specific to the Detector library diff --git a/include/muonpi/serial/i2cdevices/lm75.h b/include/muonpi/serial/i2cdevices/lm75.h new file mode 100644 index 0000000..0b5f01a --- /dev/null +++ b/include/muonpi/serial/i2cdevices/lm75.h @@ -0,0 +1,25 @@ +#ifndef MUONPI_SERIAL_I2CDEVICES_LM75_H +#define MUONPI_SERIAL_I2CDEVICES_LM75_H + +#include "muonpi/serial/i2cdevice.h" +#include "muonpi/addressrange.h" + +namespace muonpi::serial::devices { + +/** + * @brief The lm75 class. Interaction for the LM75 Temperature Sensor. + * Closely follows the datasheet: + * https://datasheets.maximintegrated.com/en/ds/LM75.pdf + */ +class lm75 : public i2c_device { +public: + constexpr static address_range addresses {0b01001000, {0b111}}; + + struct temperature_r : public simple_register<> { + + } +}; + +} // namespace muonpi::serial::devices + +#endif diff --git a/src/serial/i2cdevices/lm75.cpp b/src/serial/i2cdevices/lm75.cpp new file mode 100644 index 0000000..e69de29 From f582736bd589568639c292a23f3c7097bd4aef53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Treffenst=C3=A4dt?= Date: Sat, 16 Apr 2022 12:47:32 +0200 Subject: [PATCH 2/5] Added implementation of lm75 device --- include/muonpi/serial/i2cdevices/lm75.h | 83 ++++++++++++++++++++++++- src/serial/i2cdevices/lm75.cpp | 52 ++++++++++++++++ 2 files changed, 133 insertions(+), 2 deletions(-) diff --git a/include/muonpi/serial/i2cdevices/lm75.h b/include/muonpi/serial/i2cdevices/lm75.h index 0b5f01a..931cfd3 100644 --- a/include/muonpi/serial/i2cdevices/lm75.h +++ b/include/muonpi/serial/i2cdevices/lm75.h @@ -15,9 +15,88 @@ class lm75 : public i2c_device { public: constexpr static address_range addresses {0b01001000, {0b111}}; - struct temperature_r : public simple_register<> { + lm75(traffic_t& bus_traffic, const std::string& path, i2c_device::address_type address); + lm75(traffic_t& bus_traffic, const std::string& path); - } + + [[nodiscard]] auto identify() -> bool override; + + template + struct basic_temperature_r : public simple_register { + using value_type = typename simple_register::value_type; + using address_type = typename simple_register::address_type; + + value_type temperature : 9 {}; + const value_type reserved : 7 {}; + + [[nodiscard]] constexpr auto value() const noexcept -> double { + return static_cast(temperature) / 2.0; + } + + [[nodiscard]] constexpr auto get() const noexcept -> value_type override { + return static_cast(temperature << 7); + } + + constexpr explicit basic_temperature_r(value_type v) noexcept + : temperature { static_cast(v >> 7) } + , reserved {static_cast(v & 0b1111111) } {} + + constexpr explicit basic_temperature_r() noexcept = default; + }; + + struct temperature_r : public basic_temperature_r<0x00> { + constexpr static tag_type register_tag { i2c_register_tag::read }; + + constexpr explicit temperature_r(value_type v) noexcept + : basic_temperature_r { v } {} + + constexpr explicit temperature_r() noexcept = default; + }; + + struct t_hyst_r : public basic_temperature_r<0x01> { + constexpr static tag_type register_tag { i2c_register_tag::read_write }; + + constexpr explicit t_hyst_r(value_type v) noexcept + : basic_temperature_r { v } {} + + constexpr explicit t_hyst_r() noexcept + : basic_temperature_r { 0b0100101100000000 } {} + }; + + struct t_os_r : public basic_temperature_r<0x02> { + constexpr static tag_type register_tag { i2c_register_tag::read_write }; + + constexpr explicit t_os_r(value_type v) noexcept + : basic_temperature_r { v } {} + + constexpr explicit t_os_r() noexcept + : basic_temperature_r { 0b0101000000000000 } {} + }; + + struct configuration_r : public simple_register { + constexpr static tag_type register_tag { i2c_register_tag::read_write }; + + const value_type reserved : 3 {}; + value_type fault_queue : 2 {}; + value_type os_polarity : 1 {}; + value_type comparator : 1 {}; + value_type shutdown : 1 {}; + + [[nodiscard]] constexpr auto get() const noexcept -> value_type override { + return static_cast((fault_queue << 3) | (os_polarity << 2) | (comparator << 1) | shutdown); + } + + constexpr explicit configuration_r(value_type v) noexcept + : reserved { static_cast((v & 0b11100000) >> 5) } + , fault_queue { static_cast((v & 0b11000) >> 3) } + , os_polarity { static_cast((v & 0b100) >> 2) } + , comparator { static_cast((v & 0b10) >> 1) } + , shutdown { static_cast(v & 0b1 ) } + {} + + constexpr explicit configuration_r() noexcept = default; + + }; }; } // namespace muonpi::serial::devices diff --git a/src/serial/i2cdevices/lm75.cpp b/src/serial/i2cdevices/lm75.cpp index e69de29..5de8880 100644 --- a/src/serial/i2cdevices/lm75.cpp +++ b/src/serial/i2cdevices/lm75.cpp @@ -0,0 +1,52 @@ +#include "muonpi/serial/i2cdevices/lm75.h" + +namespace muonpi::serial::devices { + +lm75::lm75(traffic_t& bus_traffic, const std::string& path, i2c_device::address_type address) + : i2c_device { bus_traffic, path, address} {} + +lm75::lm75(traffic_t& bus_traffic, const std::string& path) + : i2c_device { bus_traffic, path, addresses} {} + + + +auto lm75::identify() -> bool { + if (flag_set(Flags::Error)) { + return false; + } + if (!present()) { + return false; + } + + const auto config { read() }; + if (!config.has_value()) { + return false; + } + if (config.value().reserved != 0) { + return false; + } + const auto temp { read() }; + if (!temp.has_value()) { + return false; + } + if (temp.value().reserved != 0) { + return false; + } + const auto thyst { read() }; + if (!thyst.has_value()) { + return false; + } + if (thyst.value().reserved != 0) { + return false; + } + const auto tos { read() }; + if (!tos.has_value()) { + return false; + } + if (tos.value().reserved != 0) { + return false; + } + + return true; +} +} // namespace muonpi::serial::devices From 9f1c2e4666e87af0fe95e0bf171dae20c239f940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Treffenst=C3=A4dt?= Date: Sat, 16 Apr 2022 12:52:28 +0200 Subject: [PATCH 3/5] Added documentation to lm75 class --- include/muonpi/serial/i2cdevices/lm75.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/muonpi/serial/i2cdevices/lm75.h b/include/muonpi/serial/i2cdevices/lm75.h index 931cfd3..747adb8 100644 --- a/include/muonpi/serial/i2cdevices/lm75.h +++ b/include/muonpi/serial/i2cdevices/lm75.h @@ -15,10 +15,26 @@ class lm75 : public i2c_device { public: constexpr static address_range addresses {0b01001000, {0b111}}; + /** + * @brief lm75 + * @param bus_traffic The bus traffic object from the i2c_bus. + * @param path The path of the i2c bus file descriptor + * @param address The address to use + */ lm75(traffic_t& bus_traffic, const std::string& path, i2c_device::address_type address); + + /** + * @brief lm75 Attempts to automatically setup the device connection. + * @param bus_traffic The bus traffic object from the i2c_bus. + * @param path The path of the i2c bus file descriptor + */ lm75(traffic_t& bus_traffic, const std::string& path); + /** + * @brief identify Attempts to positively identify the device. + * @return True if identification was successful. + */ [[nodiscard]] auto identify() -> bool override; template From bf242d3c745be1f589db2a332b4e2d15ce19e6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Treffenst=C3=A4dt?= Date: Sat, 16 Apr 2022 12:52:45 +0200 Subject: [PATCH 4/5] Applied clang-format --- include/muonpi/serial/i2cdefinitions.h | 2 +- include/muonpi/serial/i2cdevices/lm75.h | 67 ++++++++++++------------- src/serial/i2cdevices/lm75.cpp | 14 +++--- 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/include/muonpi/serial/i2cdefinitions.h b/include/muonpi/serial/i2cdefinitions.h index f1a86fd..96f572c 100644 --- a/include/muonpi/serial/i2cdefinitions.h +++ b/include/muonpi/serial/i2cdefinitions.h @@ -112,7 +112,7 @@ requires is_value_type&& is_value_type [[nodiscard]] constexpr virtual auto get() const noexcept -> std::array = 0; }; -using tag_type = std::uint8_t; +using tag_type = std::uint8_t; namespace i2c_register_tag { constexpr static tag_type read {0b01}; diff --git a/include/muonpi/serial/i2cdevices/lm75.h b/include/muonpi/serial/i2cdevices/lm75.h index 747adb8..e81c680 100644 --- a/include/muonpi/serial/i2cdevices/lm75.h +++ b/include/muonpi/serial/i2cdevices/lm75.h @@ -1,8 +1,8 @@ #ifndef MUONPI_SERIAL_I2CDEVICES_LM75_H #define MUONPI_SERIAL_I2CDEVICES_LM75_H -#include "muonpi/serial/i2cdevice.h" #include "muonpi/addressrange.h" +#include "muonpi/serial/i2cdevice.h" namespace muonpi::serial::devices { @@ -30,7 +30,6 @@ class lm75 : public i2c_device { */ lm75(traffic_t& bus_traffic, const std::string& path); - /** * @brief identify Attempts to positively identify the device. * @return True if identification was successful. @@ -40,10 +39,11 @@ class lm75 : public i2c_device { template struct basic_temperature_r : public simple_register { using value_type = typename simple_register::value_type; - using address_type = typename simple_register::address_type; + using address_type = + typename simple_register::address_type; - value_type temperature : 9 {}; - const value_type reserved : 7 {}; + value_type temperature : 9 {}; + const value_type reserved : 7 {}; [[nodiscard]] constexpr auto value() const noexcept -> double { return static_cast(temperature) / 2.0; @@ -54,64 +54,63 @@ class lm75 : public i2c_device { } constexpr explicit basic_temperature_r(value_type v) noexcept - : temperature { static_cast(v >> 7) } - , reserved {static_cast(v & 0b1111111) } {} + : temperature {static_cast(v >> 7)} + , reserved {static_cast(v & 0b1111111)} {} - constexpr explicit basic_temperature_r() noexcept = default; + constexpr explicit basic_temperature_r() noexcept = default; }; struct temperature_r : public basic_temperature_r<0x00> { - constexpr static tag_type register_tag { i2c_register_tag::read }; + constexpr static tag_type register_tag {i2c_register_tag::read}; constexpr explicit temperature_r(value_type v) noexcept - : basic_temperature_r { v } {} + : basic_temperature_r {v} {} - constexpr explicit temperature_r() noexcept = default; + constexpr explicit temperature_r() noexcept = default; }; struct t_hyst_r : public basic_temperature_r<0x01> { - constexpr static tag_type register_tag { i2c_register_tag::read_write }; + constexpr static tag_type register_tag {i2c_register_tag::read_write}; constexpr explicit t_hyst_r(value_type v) noexcept - : basic_temperature_r { v } {} + : basic_temperature_r {v} {} - constexpr explicit t_hyst_r() noexcept - : basic_temperature_r { 0b0100101100000000 } {} + constexpr explicit t_hyst_r() noexcept + : basic_temperature_r {0b0100101100000000} {} }; struct t_os_r : public basic_temperature_r<0x02> { - constexpr static tag_type register_tag { i2c_register_tag::read_write }; + constexpr static tag_type register_tag {i2c_register_tag::read_write}; constexpr explicit t_os_r(value_type v) noexcept - : basic_temperature_r { v } {} + : basic_temperature_r {v} {} - constexpr explicit t_os_r() noexcept - : basic_temperature_r { 0b0101000000000000 } {} + constexpr explicit t_os_r() noexcept + : basic_temperature_r {0b0101000000000000} {} }; struct configuration_r : public simple_register { - constexpr static tag_type register_tag { i2c_register_tag::read_write }; + constexpr static tag_type register_tag {i2c_register_tag::read_write}; - const value_type reserved : 3 {}; - value_type fault_queue : 2 {}; - value_type os_polarity : 1 {}; - value_type comparator : 1 {}; - value_type shutdown : 1 {}; + const value_type reserved : 3 {}; + value_type fault_queue : 2 {}; + value_type os_polarity : 1 {}; + value_type comparator : 1 {}; + value_type shutdown : 1 {}; [[nodiscard]] constexpr auto get() const noexcept -> value_type override { - return static_cast((fault_queue << 3) | (os_polarity << 2) | (comparator << 1) | shutdown); + return static_cast((fault_queue << 3) | (os_polarity << 2) + | (comparator << 1) | shutdown); } constexpr explicit configuration_r(value_type v) noexcept - : reserved { static_cast((v & 0b11100000) >> 5) } - , fault_queue { static_cast((v & 0b11000) >> 3) } - , os_polarity { static_cast((v & 0b100) >> 2) } - , comparator { static_cast((v & 0b10) >> 1) } - , shutdown { static_cast(v & 0b1 ) } - {} - - constexpr explicit configuration_r() noexcept = default; + : reserved {static_cast((v & 0b11100000) >> 5)} + , fault_queue {static_cast((v & 0b11000) >> 3)} + , os_polarity {static_cast((v & 0b100) >> 2)} + , comparator {static_cast((v & 0b10) >> 1)} + , shutdown {static_cast(v & 0b1)} {} + constexpr explicit configuration_r() noexcept = default; }; }; diff --git a/src/serial/i2cdevices/lm75.cpp b/src/serial/i2cdevices/lm75.cpp index 5de8880..48c61a1 100644 --- a/src/serial/i2cdevices/lm75.cpp +++ b/src/serial/i2cdevices/lm75.cpp @@ -3,12 +3,10 @@ namespace muonpi::serial::devices { lm75::lm75(traffic_t& bus_traffic, const std::string& path, i2c_device::address_type address) - : i2c_device { bus_traffic, path, address} {} + : i2c_device {bus_traffic, path, address} {} lm75::lm75(traffic_t& bus_traffic, const std::string& path) - : i2c_device { bus_traffic, path, addresses} {} - - + : i2c_device {bus_traffic, path, addresses} {} auto lm75::identify() -> bool { if (flag_set(Flags::Error)) { @@ -18,28 +16,28 @@ auto lm75::identify() -> bool { return false; } - const auto config { read() }; + const auto config {read()}; if (!config.has_value()) { return false; } if (config.value().reserved != 0) { return false; } - const auto temp { read() }; + const auto temp {read()}; if (!temp.has_value()) { return false; } if (temp.value().reserved != 0) { return false; } - const auto thyst { read() }; + const auto thyst {read()}; if (!thyst.has_value()) { return false; } if (thyst.value().reserved != 0) { return false; } - const auto tos { read() }; + const auto tos {read()}; if (!tos.has_value()) { return false; } From 49e779308fb48c3c4f7f0d33973d3a996e3fbc3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Treffenst=C3=A4dt?= Date: Sun, 31 Jul 2022 13:59:46 +0200 Subject: [PATCH 5/5] Updated use of i2c_tag_type alias --- include/muonpi/serial/i2cdevices/lm75.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/muonpi/serial/i2cdevices/lm75.h b/include/muonpi/serial/i2cdevices/lm75.h index e81c680..d1a6902 100644 --- a/include/muonpi/serial/i2cdevices/lm75.h +++ b/include/muonpi/serial/i2cdevices/lm75.h @@ -61,7 +61,7 @@ class lm75 : public i2c_device { }; struct temperature_r : public basic_temperature_r<0x00> { - constexpr static tag_type register_tag {i2c_register_tag::read}; + constexpr static i2c_tag_type register_tag {i2c_register_tag::read}; constexpr explicit temperature_r(value_type v) noexcept : basic_temperature_r {v} {} @@ -70,7 +70,7 @@ class lm75 : public i2c_device { }; struct t_hyst_r : public basic_temperature_r<0x01> { - constexpr static tag_type register_tag {i2c_register_tag::read_write}; + constexpr static i2c_tag_type register_tag {i2c_register_tag::read_write}; constexpr explicit t_hyst_r(value_type v) noexcept : basic_temperature_r {v} {} @@ -80,7 +80,7 @@ class lm75 : public i2c_device { }; struct t_os_r : public basic_temperature_r<0x02> { - constexpr static tag_type register_tag {i2c_register_tag::read_write}; + constexpr static i2c_tag_type register_tag {i2c_register_tag::read_write}; constexpr explicit t_os_r(value_type v) noexcept : basic_temperature_r {v} {} @@ -90,7 +90,7 @@ class lm75 : public i2c_device { }; struct configuration_r : public simple_register { - constexpr static tag_type register_tag {i2c_register_tag::read_write}; + constexpr static i2c_tag_type register_tag {i2c_register_tag::read_write}; const value_type reserved : 3 {}; value_type fault_queue : 2 {};