From 2125e936dc2c1dffc7b5740236db370f1b39e6c7 Mon Sep 17 00:00:00 2001 From: Russell Gilbert Date: Tue, 24 Feb 2026 16:41:01 +0000 Subject: [PATCH] Centralizes GPIO pin definitions Moves GPIO pin and chip definitions to a central constants file for easier management and consistency. Refactors the heart component to utilize these centralized constants, enhancing code maintainability and readability. --- src/RobotNode/CMakeLists.txt | 9 +++++++-- src/RobotNode/cardio/heart.cpp | 13 +++++++------ src/RobotNode/cardio/heart.h | 9 +++++---- src/RobotNode/constants.h | 7 +++++++ src/RobotNode/version.txt | 2 +- 5 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/RobotNode/constants.h diff --git a/src/RobotNode/CMakeLists.txt b/src/RobotNode/CMakeLists.txt index 34740f8..b24e722 100644 --- a/src/RobotNode/CMakeLists.txt +++ b/src/RobotNode/CMakeLists.txt @@ -30,7 +30,8 @@ add_executable(dixon dixon_brain.h cardio/heart.cpp cardio/heart.h - logging/dixon_logger.h) + logging/dixon_logger.h + constants.h) # Create the Dependency # This tells CMake: "Before you build Dixon, you MUST run BumpVersion." @@ -41,9 +42,13 @@ configure_file(version.h.in "${CMAKE_CURRENT_BINARY_DIR}/Version.h") # Consolidated Include Paths target_include_directories(dixon PRIVATE - "${CMAKE_CURRENT_BINARY_DIR}" # For generated Version.h + "${CMAKE_CURRENT_SOURCE_DIR}}" # project root (constants.h) + "${CMAKE_CURRENT_BINARY_DIR}" # For generated Version.h ) + +target_include_directories(dixon PRIVATE ${CMAKE_SOURCE_DIR}) + # Link the gpiod & spdlog libraries target_link_libraries(dixon PRIVATE gpiodcxx diff --git a/src/RobotNode/cardio/heart.cpp b/src/RobotNode/cardio/heart.cpp index d2abbbe..7e7efb1 100644 --- a/src/RobotNode/cardio/heart.cpp +++ b/src/RobotNode/cardio/heart.cpp @@ -1,15 +1,16 @@ -#include "heart.h" +#include "../constants.h" +#include "heart.h" #include #include "../dixon_node_state.h" namespace cardio { - Heart::Heart(const char* chipName, const unsigned int lineOffset) + Heart::Heart() : _lastBeat(std::chrono::steady_clock::now()), - chip_(std::string("/dev/") + chipName), + chip_(std::string("/dev/") + dixon::GPIO_CHIP_NAME), isOn_(false), - request_(get_line_request(chip_, lineOffset)), + request_(get_line_request(chip_, dixon::GPIO_HEART_PIN)), logger_("Heart") { } @@ -26,7 +27,7 @@ namespace cardio { isOn_ = !isOn_; const auto val = isOn_ ? gpiod::line::value::ACTIVE : gpiod::line::value::INACTIVE; - request_.set_value(DEFAULT_HEART_PIN, val); + request_.set_value(dixon::GPIO_HEART_PIN, val); _lastBeat = now; if (isOn_) { @@ -39,7 +40,7 @@ namespace cardio { logger_.info("Stopping heart."); isOn_ = false; - request_.set_value(DEFAULT_HEART_PIN, gpiod::line::value::INACTIVE); + request_.set_value(dixon::GPIO_HEART_PIN, gpiod::line::value::INACTIVE); logger_.info("Stopped heart."); } diff --git a/src/RobotNode/cardio/heart.h b/src/RobotNode/cardio/heart.h index 784a033..0c2290d 100644 --- a/src/RobotNode/cardio/heart.h +++ b/src/RobotNode/cardio/heart.h @@ -1,4 +1,5 @@ #pragma once +#include "constants.h" #include "../logging/dixon_logger.h" #include #include @@ -9,15 +10,15 @@ namespace cardio { public: - explicit Heart(const char* chipName = GPIO_CHIP_NAME, unsigned int lineOffset = DEFAULT_HEART_PIN); + Heart(); + Heart(const Heart&) = delete; + Heart& operator=(const Heart&) = delete; + void beat(); void stop(); private: static gpiod::line_request get_line_request(gpiod::chip&, unsigned int lineOffset); - static constexpr auto GPIO_CHIP_NAME = "gpiochip0"; - static constexpr unsigned int DEFAULT_HEART_PIN = 17; - static constexpr unsigned int HEART_LINE_INDEX = 0; std::chrono::steady_clock::time_point _lastBeat; gpiod::chip chip_; bool isOn_; diff --git a/src/RobotNode/constants.h b/src/RobotNode/constants.h new file mode 100644 index 0000000..f555fc8 --- /dev/null +++ b/src/RobotNode/constants.h @@ -0,0 +1,7 @@ +#pragma once + +namespace dixon +{ + constexpr auto GPIO_CHIP_NAME = "gpiochip0"; + constexpr unsigned int GPIO_HEART_PIN = 17; +} \ No newline at end of file diff --git a/src/RobotNode/version.txt b/src/RobotNode/version.txt index 1b5deea..ed3c0fb 100644 --- a/src/RobotNode/version.txt +++ b/src/RobotNode/version.txt @@ -1 +1 @@ -1.0.59 \ No newline at end of file +1.0.67 \ No newline at end of file