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

feat: add CD74HC4057 input #102

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor(Arduino): use Filter to invert analog sensors
  • Loading branch information
leon0399 committed Feb 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 2fb4da4c64ed3f44ad3a388e090680c07bddaa4b
21 changes: 5 additions & 16 deletions lib/arduino/senseshift/arduino/input/sensor/analog.hpp
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
#endif

namespace SenseShift::Arduino::Input {
template<bool Invert = false>
class AnalogSimpleSensor : public ::SenseShift::Input::IFloatSimpleSensor {
const std::uint8_t pin_;

@@ -29,20 +28,10 @@ namespace SenseShift::Arduino::Input {

void init() override { pinMode(this->pin_, INPUT); };

[[nodiscard]] auto getValue() -> float override;
[[nodiscard]] inline auto getValue() -> float override
{
const std::uint16_t raw = analogRead(this->pin_);
return static_cast<float>(raw) / ANALOG_MAX;
}
};

template<>
[[nodiscard]] inline auto AnalogSimpleSensor<false>::getValue() -> float
{
const std::uint16_t raw = analogRead(this->pin_);
return static_cast<float>(raw) / ANALOG_MAX;
}

template<>
[[nodiscard]] inline auto AnalogSimpleSensor<true>::getValue() -> float
{
const std::uint16_t raw = ANALOG_MAX - analogRead(this->pin_);
return static_cast<float>(raw) / ANALOG_MAX;
}
} // namespace SenseShift::Arduino::Input
6 changes: 6 additions & 0 deletions lib/io/senseshift/input/filter.hpp
Original file line number Diff line number Diff line change
@@ -300,4 +300,10 @@ namespace SenseShift::Input::Filter {
private:
Container const& lookup_table_;
};

/// Specialized filter for analog sensors (between 0.0 and 1.0).
class AnalogInvertFilter : public IFilter<float> {
public:
auto filter(ISimpleSensor<float>* /*sensor*/, float value) -> float override { return 1.0F - value; }
};
} // namespace SenseShift::Input::Filter
27 changes: 15 additions & 12 deletions lib/opengloves/senseshift/opengloves/autoconfig.hpp
Original file line number Diff line number Diff line change
@@ -83,11 +83,13 @@
#define FINGER_PINKY_ENABLED false
#endif

#define DEFINE_FINGER(NAME, CURL_PIN, CURL_INVERT, CURL_CALIB) \
auto* NAME##_sensor = new ::SenseShift::Input::SimpleSensorDecorator( \
new ::SenseShift::Arduino::Input::AnalogSimpleSensor<CURL_INVERT>(CURL_PIN) \
); \
NAME##_sensor->addFilters({ new ::SenseShift::Input::Filter::ExponentialMovingAverageFilter<float>(0.8F) }); \
#define DEFINE_FINGER(NAME, CURL_PIN, CURL_INVERT, CURL_CALIB) \
auto* NAME##_sensor = \
new ::SenseShift::Input::SimpleSensorDecorator(new ::SenseShift::Arduino::Input::AnalogSimpleSensor(CURL_PIN)); \
NAME##_sensor->addFilters({ new ::SenseShift::Input::Filter::ExponentialMovingAverageFilter<float>(0.8F) }); \
if (CURL_INVERT) { \
NAME##_sensor->addFilter({ new ::SenseShift::Input::Filter::AnalogInvertFilter() }); \
} \
NAME##_sensor->setCalibrator((CURL_CALIB));

#ifdef PIN_FINGER_THUMB_SPLAY
@@ -134,13 +136,14 @@
#define JOYSTICK_ENABLED false
#endif

#define DEFINE_JOYSTICK_AXIS(NAME, PIN, INVERT, DEADZONE) \
auto NAME##_sensor = \
new ::SenseShift::Input::SimpleSensorDecorator(new ::SenseShift::Arduino::Input::AnalogSimpleSensor<INVERT>(PIN) \
); \
NAME##_sensor->addFilters({ new ::SenseShift::Input::Filter::ExponentialMovingAverageFilter<float>(0.7F), \
new ::SenseShift::Input::Filter::CenterDeadzoneFilter(DEADZONE) });

#define DEFINE_JOYSTICK_AXIS(NAME, PIN, INVERT, DEADZONE) \
auto NAME##_sensor = \
new ::SenseShift::Input::SimpleSensorDecorator(new ::SenseShift::Arduino::Input::AnalogSimpleSensor(PIN)); \
NAME##_sensor->addFilters({ new ::SenseShift::Input::Filter::ExponentialMovingAverageFilter<float>(0.7F), \
new ::SenseShift::Input::Filter::CenterDeadzoneFilter(DEADZONE) }); \
if (INVERT) { \
NAME##_sensor->addFilter({ new ::SenseShift::Input::Filter::AnalogInvertFilter() }); \
}
#pragma endregion

#pragma region Buttons