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.
This commit is contained in:
Russell Gilbert 2026-02-24 16:41:01 +00:00
parent 9159967b83
commit 2125e936dc
5 changed files with 27 additions and 13 deletions

View file

@ -30,7 +30,8 @@ add_executable(dixon
dixon_brain.h dixon_brain.h
cardio/heart.cpp cardio/heart.cpp
cardio/heart.h cardio/heart.h
logging/dixon_logger.h) logging/dixon_logger.h
constants.h)
# Create the Dependency # Create the Dependency
# This tells CMake: "Before you build Dixon, you MUST run BumpVersion." # 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 # Consolidated Include Paths
target_include_directories(dixon PRIVATE 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 # Link the gpiod & spdlog libraries
target_link_libraries(dixon PRIVATE target_link_libraries(dixon PRIVATE
gpiodcxx gpiodcxx

View file

@ -1,15 +1,16 @@
#include "heart.h" #include "../constants.h"
#include "heart.h"
#include <gpiod.hpp> #include <gpiod.hpp>
#include "../dixon_node_state.h" #include "../dixon_node_state.h"
namespace cardio namespace cardio
{ {
Heart::Heart(const char* chipName, const unsigned int lineOffset) Heart::Heart()
: _lastBeat(std::chrono::steady_clock::now()), : _lastBeat(std::chrono::steady_clock::now()),
chip_(std::string("/dev/") + chipName), chip_(std::string("/dev/") + dixon::GPIO_CHIP_NAME),
isOn_(false), isOn_(false),
request_(get_line_request(chip_, lineOffset)), request_(get_line_request(chip_, dixon::GPIO_HEART_PIN)),
logger_("Heart") logger_("Heart")
{ {
} }
@ -26,7 +27,7 @@ namespace cardio
{ {
isOn_ = !isOn_; isOn_ = !isOn_;
const auto val = isOn_ ? gpiod::line::value::ACTIVE : gpiod::line::value::INACTIVE; 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; _lastBeat = now;
if (isOn_) { if (isOn_) {
@ -39,7 +40,7 @@ namespace cardio
{ {
logger_.info("Stopping heart."); logger_.info("Stopping heart.");
isOn_ = false; 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."); logger_.info("Stopped heart.");
} }

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "constants.h"
#include "../logging/dixon_logger.h" #include "../logging/dixon_logger.h"
#include <gpiod.hpp> #include <gpiod.hpp>
#include <chrono> #include <chrono>
@ -9,15 +10,15 @@ namespace cardio
{ {
public: 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 beat();
void stop(); void stop();
private: private:
static gpiod::line_request get_line_request(gpiod::chip&, unsigned int lineOffset); 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; std::chrono::steady_clock::time_point _lastBeat;
gpiod::chip chip_; gpiod::chip chip_;
bool isOn_; bool isOn_;

View file

@ -0,0 +1,7 @@
#pragma once
namespace dixon
{
constexpr auto GPIO_CHIP_NAME = "gpiochip0";
constexpr unsigned int GPIO_HEART_PIN = 17;
}

View file

@ -1 +1 @@
1.0.59 1.0.67