Skip to content

Commit

Permalink
fix: sensor template deduction
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Feb 8, 2024
1 parent 13f3b54 commit 809f14e
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 34 deletions.
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactglove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactosy2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactosyf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactosyh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactsuit_x16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactsuit_x40.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void setupMode()
new MultiplyFilter(3.3F), // Convert to raw pin voltage
new VoltageDividerFilter(27000.0F, 100000.0F), // Convert to voltage divider voltage
});
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask(
auto* batteryTask = new ::SenseShift::FreeRTOS::ComponentUpdateTask<SimpleSensorDecorator<float>>(
batteryVoltageSensor,
SENSESHIFT_BATTERY_SAMPLE_RATE,
{ "ADC Battery", 4096, SENSESHIFT_BATTERY_TASK_PRIORITY, tskNO_AFFINITY }
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/opengloves/opengloves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void setupMode()
#endif

#if GESTURE_TRIGGER_ENABLED
auto* trigger = new AnalogThresholdSensor(index_curl_sensor, GESTURE_TRIGGER_THRESHOLD);
auto* trigger = new TriggerGesture(index_curl_sensor, GESTURE_TRIGGER_THRESHOLD);
input_sensors.trigger.press = trigger;
#elif BUTTON_TRIGGER_ENABLED
auto trigger = new BUTTON_CLASS(PIN_BUTTON_TRIGGER, BUTTON_TRIGGER_INVERT);
Expand Down
51 changes: 31 additions & 20 deletions lib/io/senseshift/input/analog_threshold.hpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,66 @@
#pragma once

#include "senseshift/input/sensor.hpp"
#include <type_traits>

namespace SenseShift::Input {
template<typename Tp>
template<typename Tp = float>
class AnalogThresholdSensor : public BinarySensor, ITickable {
public:
public:
/// Analog threshold sensor with hysteresis.
///
/// \param source The source sensor.
/// \param threshold_upper Upper threshold, that needs to be crossed to transition from `low` to `high` states.
/// \param threshold_lower Lower threshold, that needs to be crossed to transition from `high` to `low` states.
explicit AnalogThresholdSensor(
::SenseShift::Input::Sensor<Tp>* source,
Tp threshold_upper,
float threshold_lower,
bool attach_callbacks = false
) : source_(source), threshold_upper_(threshold_upper), threshold_lower_(threshold_lower), attach_callbacks_(attach_callbacks) {}
::SenseShift::Input::Sensor<Tp>* source, Tp threshold_upper, Tp threshold_lower, bool attach_callbacks = false
) :
source_(source),
threshold_upper_(threshold_upper),
threshold_lower_(threshold_lower),
attach_callbacks_(attach_callbacks)
{
}

/// \param source The source sensor.
/// \param threshold Threshold, that will be used for both upper and lower thresholds.
template <typename U = Tp, std::enable_if_t<std::is_same_v<U, float>, int> = 0>
template<typename U = Tp, std::enable_if_t<std::is_same_v<U, float>, int> = 0>
explicit AnalogThresholdSensor(
::SenseShift::Input::Sensor<Tp>* source,
float threshold = 0.5f,
bool attach_callbacks = false
) : AnalogThresholdSensor(source, threshold, threshold, attach_callbacks) { }
::SenseShift::Input::Sensor<Tp>* source, float threshold = 0.5f, bool attach_callbacks = false
) :
AnalogThresholdSensor(source, threshold, threshold, attach_callbacks)
{
}

void init() override {
void init() override
{
SS_SUBSENSOR_INIT(this->source_, this->attach_callbacks_, [this](Tp /*value*/) {
this->recalculateState();
});
}

void tick() override {
void tick() override
{
if (this->attach_callbacks_) {
LOG_E("sensor.analog_threshold", "tick() called when attach_callbacks_ is true, infinite loop go wroom-wroom!");
LOG_E(
"sensor.analog_threshold",
"tick() called when attach_callbacks_ is true, infinite loop go wroom-wroom!"
);
}
this->recalculateState();
}

void recalculateState() {
const auto sensor_value = this->source_->getValue();
void recalculateState()
{
const auto sensor_value = this->source_->getValue();
this->publishState(sensor_value >= (this->getValue() ? this->threshold_lower_ : this->threshold_upper_));
}

private:
private:
::SenseShift::Input::Sensor<Tp>* source_;

float threshold_lower_, threshold_upper_;
Tp threshold_lower_, threshold_upper_;

bool attach_callbacks_ = false;
};
}
} // namespace SenseShift::Input
15 changes: 11 additions & 4 deletions test/test_body_gestures/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ void test_gesture_trigger(void)
float threshold = 0.5f;
auto* index = new FloatSensor();

auto* gesture = new TriggerGesture(index, threshold, true);
auto* gesture = new TriggerGesture(index, threshold);

index->publishState(0.4f);
gesture->tick();
TEST_ASSERT_FALSE(gesture->getValue());

index->publishState(0.6f);
gesture->tick();
TEST_ASSERT_TRUE(gesture->getValue());
}

Expand All @@ -35,22 +37,25 @@ void test_gesture_grab(void)
.ring = ring,
.pinky = pinky,
},
threshold,
true
threshold
);

TEST_ASSERT_FALSE(gesture->getValue());

index->publishState(0.6);
gesture->tick();
TEST_ASSERT_FALSE(gesture->getValue());

middle->publishState(0.6);
gesture->tick();
TEST_ASSERT_FALSE(gesture->getValue());

ring->publishState(0.6);
gesture->tick();
TEST_ASSERT_FALSE(gesture->getValue());

pinky->publishState(0.6);
gesture->tick();
TEST_ASSERT_TRUE(gesture->getValue());
}

Expand All @@ -61,14 +66,16 @@ void test_gesture_pinch(void)
auto* thumb = new FloatSensor();
auto* index = new FloatSensor();

auto* gesture = new PinchGesture({ .thumb = thumb, .index = index }, threshold, true);
auto* gesture = new PinchGesture({ .thumb = thumb, .index = index }, threshold);

TEST_ASSERT_FALSE(gesture->getValue());

thumb->publishState(0.6f);
gesture->tick();
TEST_ASSERT_FALSE(gesture->getValue());

index->publishState(0.6f);
gesture->tick();
TEST_ASSERT_TRUE(gesture->getValue());
}

Expand Down

0 comments on commit 809f14e

Please sign in to comment.