Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNS - Brakes sensor #72

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ build
.cache
tags
.DS_Store
.idea
.cmake-build-debug

# Telemetry
.env
38 changes: 38 additions & 0 deletions lib/sensors/brakes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "brakes.hpp"

namespace hyped::sensors {

std::optional<Brakes> Brakes::create(core::ILogger &logger,
std::shared_ptr<io::IGpio> gpio,
const std::uint8_t pin)
{
const auto reader = gpio->getReader(pin, io::Edge::kNone);
if (!reader) {
logger.log(core::LogLevel::kFatal, "Failed to create Brakes instance");
return std::nullopt;
}
logger.log(core::LogLevel::kDebug, "Successfully created Brakes instance");
return Brakes(logger, *reader);
}

Brakes::Brakes(core::ILogger &logger, std::shared_ptr<io::IGpioReader> gpio_reader)
: gpio_reader_(gpio_reader),
logger_(logger),
pin_()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this here? Either pass the pin into the constructor if it may need to be accessible in the future (i.e. if something else might want to know which pin it's using) or remove it from here and the header file

{
}

Brakes::~Brakes()
{
}

std::optional<bool> Brakes::isClamped()
{
const auto optional_reading = gpio_reader_->read();
if (!optional_reading) { return std::nullopt; }
const auto reading = *optional_reading;

if (reading == core::DigitalSignal::kHigh) { return false; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need an if statement here - you can just return the condition :)

return true;
licornes-fluos marked this conversation as resolved.
Show resolved Hide resolved
}
} // namespace hyped::sensors
29 changes: 29 additions & 0 deletions lib/sensors/brakes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <cstdint>
#include <memory>
#include <optional>

#include <core/logger.hpp>
#include <io/gpio.hpp>

namespace hyped::sensors {
class Brakes {
public:
static std::optional<Brakes> create(core::ILogger &logger,
std::shared_ptr<io::IGpio> gpio,
const std::uint8_t new_pin);

~Brakes();

std::optional<bool> isClamped();

private:
Brakes(core::ILogger &logger, std::shared_ptr<io::IGpioReader> gpio_reader);

private:
const std::uint8_t pin_;
std::shared_ptr<io::IGpioReader> gpio_reader_;
core::ILogger &logger_;
};
} // namespace hyped::sensors
Loading