-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make multiplexers universal with templates
- Loading branch information
Showing
2 changed files
with
87 additions
and
118 deletions.
There are no files selected for viewing
118 changes: 0 additions & 118 deletions
118
lib/arduino/senseshift/arduino/input/sensor/cd74hc4067.hpp
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
lib/arduino/senseshift/arduino/input/sensor/multiplexer.hpp
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,87 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <cassert> | ||
#include <cstddef> | ||
#include <cstdint> | ||
|
||
#include <Arduino.h> | ||
|
||
#include "senseshift/arduino/input/sensor/analog.hpp" | ||
|
||
#include <senseshift/core/component.hpp> | ||
#include <senseshift/input/sensor.hpp> | ||
|
||
namespace SenseShift::Arduino::Input { | ||
template<size_t N> | ||
class Multiplexer : public IInitializable { | ||
public: | ||
/// \param pins The pins to use for the multiplexer. | ||
explicit Multiplexer(const std::array<std::uint8_t, N>& pins, const size_t switch_delay_us = 2) : | ||
pins_(pins), switch_delay_us_(switch_delay_us) | ||
{ | ||
} | ||
|
||
void init() override | ||
{ | ||
for (const auto pin : this->pins_) { | ||
pinMode(pin, OUTPUT); | ||
} | ||
} | ||
|
||
/// Select the channel. | ||
/// \param channel The channel to select. | ||
void selectChannel(const std::uint8_t channel) | ||
{ | ||
if (channel >= (1 << N) || this->active_channel_ == channel) { | ||
return; | ||
} | ||
|
||
for (size_t i = 0; i < N; ++i) { | ||
digitalWrite(this->pins_[i], (channel >> i) & 0b0001); | ||
} | ||
|
||
delayMicroseconds(this->switch_delay_us_); | ||
|
||
this->active_channel_ = channel; | ||
} | ||
|
||
private: | ||
const std::array<std::uint8_t, N> pins_; | ||
std::uint8_t active_channel_ = 0; | ||
const size_t switch_delay_us_; | ||
}; | ||
|
||
using MUX_CD74HC4057Component = Multiplexer<4>; | ||
using MUX_74HC4051Component = Multiplexer<3>; | ||
|
||
template<size_t N> | ||
class MultiplexerSensor : public AnalogSimpleSensor { | ||
public: | ||
/// \param component The CD74HC4057 Component to use. | ||
/// \param pin The SIG pin of the sensor. | ||
/// \param channel The channel to read from. | ||
MultiplexerSensor(Multiplexer<N>* component, const std::uint8_t pin_sig, const std::uint8_t channel) : | ||
component_(component), AnalogSimpleSensor(pin_sig), channel_(channel) | ||
{ | ||
assert(channel < (1 << N) && "Channel out of range"); | ||
} | ||
|
||
void init() override | ||
{ | ||
this->component_->init(); | ||
AnalogSimpleSensor::init(); | ||
} | ||
|
||
[[nodiscard]] auto getValue() -> float override | ||
{ | ||
this->component_->selectChannel(this->channel_); | ||
|
||
return AnalogSimpleSensor::getValue(); | ||
} | ||
|
||
private: | ||
Multiplexer<N>* component_; | ||
const std::uint8_t channel_; | ||
}; | ||
} // namespace SenseShift::Arduino::Input |