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
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_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

View file

@ -1,15 +1,16 @@
#include "heart.h"
#include "../constants.h"
#include "heart.h"
#include <gpiod.hpp>
#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.");
}

View file

@ -1,4 +1,5 @@
#pragma once
#include "constants.h"
#include "../logging/dixon_logger.h"
#include <gpiod.hpp>
#include <chrono>
@ -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_;

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