-
Notifications
You must be signed in to change notification settings - Fork 3
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 - Analogue Mux #98
Open
ishmis
wants to merge
6
commits into
master
Choose a base branch
from
sns-adc-mux
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#pragma once | ||
|
||
#include "mux_sensors.hpp" | ||
|
||
#include <array> | ||
#include <bitset> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <optional> | ||
|
||
#include <core/logger.hpp> | ||
#include <core/types.hpp> | ||
#include <io/gpio.hpp> | ||
|
||
namespace hyped::sensors { | ||
|
||
constexpr std::uint8_t kNumSelectorPins = 4; | ||
|
||
/** | ||
* @brief Class for analogue mux where channel selection is done by GPIO pins | ||
* @details 16 channels are available, selection is done by 4 GPIO pins (think 4x16 decoder) | ||
*/ | ||
template<class T, std::uint8_t N> | ||
class AnalogueMux { | ||
public: | ||
/** | ||
* @brief Creates an AnalogueMux object with specific selector pins and disable input pin | ||
* @details The disable input pin is just the active low enable input pin (from datasheet) | ||
* @param selector_pin_writers Array of 4 GPIO pin writers in the order s0, s1, s2 and s3 | ||
* @param disable_input_writer GPIO pin corresponding to E bar (active low enable input pin) | ||
*/ | ||
static std::optional<std::shared_ptr<AnalogueMux<T, N>>> create( | ||
core::ILogger &logger, | ||
std::array<std::shared_ptr<io::IGpioWriter>, kNumSelectorPins> selector_pin_writers, | ||
std::shared_ptr<io::IGpioWriter> disable_input_writer, | ||
std::array<std::unique_ptr<IMuxSensor<T>>, N> &sensors); | ||
AnalogueMux(core::ILogger &logger, | ||
std::array<std::shared_ptr<io::IGpioWriter>, kNumSelectorPins> selector_pins, | ||
std::shared_ptr<io::IGpioWriter> disable_input_writer, | ||
std::array<std::unique_ptr<IMuxSensor<T>>, N> &sensors); | ||
~AnalogueMux(); | ||
|
||
std::optional<std::array<T, N>> readAllChannels(); | ||
|
||
private: | ||
core::Result selectChannel(const std::uint8_t channel); | ||
core::Result closeAllChannels(); | ||
|
||
private: | ||
core::ILogger &logger_; | ||
const std::array<std::shared_ptr<io::IGpioWriter>, kNumSelectorPins> selector_pin_writers_; | ||
const std::shared_ptr<io::IGpioWriter> disable_input_writer_; | ||
const std::array<std::unique_ptr<IMuxSensor<T>>, N> sensors_; | ||
}; | ||
|
||
template<typename T, std::uint8_t N> | ||
std::optional<std::shared_ptr<AnalogueMux<T, N>>> AnalogueMux<T, N>::create( | ||
core::ILogger &logger, | ||
std::array<std::shared_ptr<io::IGpioWriter>, kNumSelectorPins> selector_pin_writers, | ||
std::shared_ptr<io::IGpioWriter> disable_input_writer, | ||
std::array<std::unique_ptr<IMuxSensor<T>>, N> &sensors) | ||
{ | ||
if (N > 16) { | ||
logger.log(core::LogLevel::kFatal, | ||
"Failed to create AnalogueMux instance, maximum 16 channels only"); | ||
return std::nullopt; | ||
} | ||
return std::make_shared<AnalogueMux<T, N>>( | ||
logger, selector_pin_writers, disable_input_writer, sensors); | ||
} | ||
|
||
template<typename T, std::uint8_t N> | ||
AnalogueMux<T, N>::AnalogueMux( | ||
core::ILogger &logger, | ||
std::array<std::shared_ptr<io::IGpioWriter>, kNumSelectorPins> selector_pin_writers, | ||
std::shared_ptr<io::IGpioWriter> disable_input_writer, | ||
std::array<std::unique_ptr<IMuxSensor<T>>, N> &sensors) | ||
: logger_(logger), | ||
selector_pin_writers_(std::move(selector_pin_writers)), | ||
sensors_(std::move(sensors)) | ||
{ | ||
} | ||
|
||
template<typename T, std::uint8_t N> | ||
AnalogueMux<T, N>::~AnalogueMux() | ||
{ | ||
} | ||
|
||
template<typename T, std::uint8_t N> | ||
std::optional<std::array<T, N>> AnalogueMux<T, N>::readAllChannels() | ||
{ | ||
// zero-initialize the array | ||
std::array<T, N> values{}; | ||
for (std::size_t i = 0; i < N; ++i) { | ||
const auto &sensor = sensors_.at(i).get(); | ||
std::uint8_t channel = sensor->getChannel(); | ||
// ensuring correct channel is selected | ||
core::Result channel_select_result = selectChannel(channel); | ||
if (channel_select_result == core::Result::kFailure) { | ||
logger_.log(core::LogLevel::kFatal, "Failed to select channel %d for analogue mux", i); | ||
return std::nullopt; | ||
} | ||
// then read sensor data | ||
const auto sensor_data = sensor->read(); | ||
if (!sensor_data) { | ||
logger_.log( | ||
core::LogLevel::kFatal, "Failed to read from sensor on channel %d on analogue mux", i); | ||
return std::nullopt; | ||
} | ||
// finally store the data | ||
values.at(i) = *sensor_data; | ||
} | ||
return values; | ||
} | ||
|
||
template<typename T, std::uint8_t N> | ||
core::Result AnalogueMux<T, N>::selectChannel(const std::uint8_t channel) | ||
{ | ||
std::bitset binary_selector = std::bitset<4>(channel); | ||
for (std::size_t i = 0; i < kNumSelectorPins; ++i) { | ||
core::Result write_result = selector_pin_writers_[i]->write( | ||
binary_selector.test(i) ? core::DigitalSignal::kHigh : core::DigitalSignal::kLow); | ||
if (write_result == core::Result::kFailure) { | ||
logger_.log( | ||
core::LogLevel::kFatal, "Failed to write to selector GPIO pin %d for analogue mux", i); | ||
return core::Result::kFailure; | ||
} | ||
} | ||
return core::Result::kSuccess; | ||
} | ||
|
||
template<typename T, std::uint8_t N> | ||
core::Result AnalogueMux<T, N>::closeAllChannels() | ||
{ | ||
core::Result write_result = disable_input_writer_->write(core::DigitalSignal::kHigh); | ||
if (write_result == core::Result::kFailure) { | ||
logger_.log(core::LogLevel::kFatal, "Failed to disable analogue mux"); | ||
return core::Result::kFailure; | ||
} | ||
return core::Result::kSuccess; | ||
} | ||
|
||
} // namespace hyped::sensors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "dummy_adc_mux_sensor.hpp" | ||
|
||
namespace hyped::utils { | ||
|
||
DummyAdcMuxSensor::DummyAdcMuxSensor() | ||
{ | ||
} | ||
|
||
core::Result DummyAdcMuxSensor::configure() | ||
{ | ||
return core::Result::kSuccess; | ||
} | ||
|
||
std::optional<core::Float> DummyAdcMuxSensor::read() | ||
{ | ||
return 0; | ||
} | ||
|
||
std::uint8_t DummyAdcMuxSensor::getChannel() const | ||
{ | ||
return 0; | ||
} | ||
|
||
} // namespace hyped::utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <sensors/mux_sensors.hpp> | ||
|
||
namespace hyped::utils { | ||
|
||
class DummyAdcMuxSensor : public sensors::IMuxSensor<core::Float> { | ||
public: | ||
DummyAdcMuxSensor(); | ||
virtual core::Result configure(); | ||
virtual std::optional<core::Float> read(); | ||
virtual std::uint8_t getChannel() const; | ||
}; | ||
|
||
} // namespace hyped::utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment or constexpr explaining the 16