Skip to content

Commit

Permalink
♻️ (IMU): Register wake up callback on enable
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Aug 19, 2024
1 parent a104897 commit a284b3f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 38 deletions.
3 changes: 1 addition & 2 deletions drivers/CoreIMU/include/CoreIMU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class CoreIMU : public interface::IMU, public interface::DeepSleepEnabled
void enableOnDataReadyInterrupt() final;
void disableOnDataReadyInterrupt() final;

void registerOnWakeUpCallback(std::function<void()> const &callback) final;
void enableOnWakeUpInterrupt() final;
void enableOnWakeUpInterrupt(std::function<void()> const &callback) final;
void disableOnWakeUpInterrupt() final;

void setPowerMode(PowerMode mode) final;
Expand Down
9 changes: 3 additions & 6 deletions drivers/CoreIMU/source/CoreIMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,7 @@ void CoreIMU::disableDeepSleep()
setPowerMode(interface::IMU::PowerMode::Normal);
}

void CoreIMU::registerOnWakeUpCallback(std::function<void()> const &callback)
{
_on_wake_up_callback = callback;
}

void CoreIMU::enableOnWakeUpInterrupt()
void CoreIMU::enableOnWakeUpInterrupt(std::function<void()> const &callback)
{
// ? Set filter and disable user offset
lsm6dsox_xl_hp_path_internal_set(&_register_io_function, LSM6DSOX_USE_SLOPE);
Expand All @@ -162,6 +157,8 @@ void CoreIMU::enableOnWakeUpInterrupt()
};
lsm6dsox_pin_int1_route_set(&_register_io_function, lsm6dsox_int1);

_on_wake_up_callback = callback;

auto on_wake_up_callback = [this] {
_event_queue.call([this] {
lsm6dsox_all_sources_t all_source;
Expand Down
30 changes: 5 additions & 25 deletions drivers/CoreIMU/tests/CoreIMU_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,15 @@ TEST_F(CoreIMUTest, disableDeepSleep)
coreimu.disableDeepSleep();
}

TEST_F(CoreIMUTest, onWakeUpCallback)
{
MockFunction<void()> mock_callback;

EXPECT_CALL(mocki2c, write).Times(AtLeast(1));
EXPECT_CALL(mocki2c, read).Times(AtLeast(1));
EXPECT_CALL(mock_callback, Call).Times(AnyNumber());

coreimu.registerOnWakeUpCallback(mock_callback.AsStdFunction());

auto on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

TEST_F(CoreIMUTest, emptyOnWakeUpCallback)
{
coreimu.registerOnWakeUpCallback({});

auto on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

TEST_F(CoreIMUTest, enableOnWakeUpInterrupt)
{
auto mock_wake_up_callback = MockFunction<void()> {};

EXPECT_CALL(mocki2c, write).Times(AtLeast(1));
EXPECT_CALL(mocki2c, read).Times(AtLeast(1));
EXPECT_CALL(mock_wake_up_callback, Call).Times(AnyNumber());

coreimu.enableOnWakeUpInterrupt();
coreimu.enableOnWakeUpInterrupt(mock_wake_up_callback.AsStdFunction());
}

TEST_F(CoreIMUTest, disableOnWakeUpInterrupt)
Expand All @@ -170,7 +151,6 @@ TEST_F(CoreIMUTest, switchCallbacks)
EXPECT_CALL(mocki2c, read).Times(AnyNumber());

coreimu.registerOnDataReadyCallback(mock_data_available_callback.AsStdFunction());
coreimu.registerOnWakeUpCallback(mock_wake_up_callback.AsStdFunction());

{
// Enable Data Available
Expand All @@ -194,7 +174,7 @@ TEST_F(CoreIMUTest, switchCallbacks)
// Enable Wake Up
EXPECT_CALL(mock_data_available_callback, Call).Times(0);
EXPECT_CALL(mock_wake_up_callback, Call).Times(AnyNumber());
coreimu.enableOnWakeUpInterrupt();
coreimu.enableOnWakeUpInterrupt(mock_wake_up_callback.AsStdFunction());
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}
Expand Down
5 changes: 2 additions & 3 deletions include/interface/drivers/IMU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ class IMU
virtual void enableOnDataReadyInterrupt() = 0;
virtual void disableOnDataReadyInterrupt() = 0;

virtual void registerOnWakeUpCallback(std::function<void()> const &callback) = 0;
virtual void enableOnWakeUpInterrupt() = 0;
virtual void disableOnWakeUpInterrupt() = 0;
virtual void enableOnWakeUpInterrupt(std::function<void()> const &callback) = 0;
virtual void disableOnWakeUpInterrupt() = 0;

virtual void setPowerMode(PowerMode) = 0;
};
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/mocks/mocks/leka/IMU.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class IMU : public interface::IMU
MOCK_METHOD(void, enableOnDataReadyInterrupt, (), (override));
MOCK_METHOD(void, disableOnDataReadyInterrupt, (), (override));

void registerOnWakeUpCallback(std::function<void()> const &cb) override { wake_up_callback = cb; }
MOCK_METHOD(void, enableOnWakeUpInterrupt, (), (override));
void enableOnWakeUpInterrupt(std::function<void()> const &cb) override { wake_up_callback = cb; }
MOCK_METHOD(void, disableOnWakeUpInterrupt, (), (override));

void call_data_ready_callback(const SensorData &data) { data_ready_callback(data); }
Expand Down

0 comments on commit a284b3f

Please sign in to comment.