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

Yann/feature/imu/add event detection interrupt #1405

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions drivers/CoreIMU/include/CoreIMU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ 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 disableOnWakeUpInterrupt() final;

void setPowerMode(PowerMode mode) final;

void enableDeepSleep() final;
Expand Down Expand Up @@ -58,6 +62,8 @@ class CoreIMU : public interface::IMU, public interface::DeepSleepEnabled

static constexpr uint8_t kMaxBufferLength = 32;
std::array<uint8_t, kMaxBufferLength> _rx_buffer {};

std::function<void()> _on_wake_up_callback {};
};

} // namespace leka
51 changes: 51 additions & 0 deletions drivers/CoreIMU/source/CoreIMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,55 @@ void CoreIMU::disableDeepSleep()
setPowerMode(interface::IMU::PowerMode::Normal);
}

void CoreIMU::registerOnWakeUpCallback(std::function<void()> const &callback)
{
_on_wake_up_callback = callback;
}
Comment on lines +140 to +143
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

à mon sens le register il doit enable directement.

quel est le cas d'usage où on a besoin de faire les deux séparément ?

si on a un enable, je m'attends à avoir un disable aussi


void CoreIMU::enableOnWakeUpInterrupt()
{
// ? Set filter and disable user offset
lsm6dsox_xl_hp_path_internal_set(&_register_io_function, LSM6DSOX_USE_SLOPE);
lsm6dsox_xl_usr_offset_on_wkup_set(&_register_io_function, 0);

// ? Set Wakeup config
lsm6dsox_wkup_threshold_set(&_register_io_function, 2);
lsm6dsox_wkup_ths_weight_set(&_register_io_function, LSM6DSOX_LSb_FS_DIV_64);
lsm6dsox_wkup_dur_set(&_register_io_function, 0x02);

// ? Set Activity config
lsm6dsox_act_sleep_dur_set(&_register_io_function, 0x02);
lsm6dsox_act_mode_set(&_register_io_function, LSM6DSOX_XL_AND_GY_NOT_AFFECTED);

lsm6dsox_pin_int1_route_t lsm6dsox_int1 {
.sleep_change = PROPERTY_ENABLE,
};
lsm6dsox_pin_int1_route_set(&_register_io_function, lsm6dsox_int1);

auto on_wake_up_callback = [this] {
_event_queue.call([this] {
lsm6dsox_all_sources_t all_source;
lsm6dsox_all_sources_get(&_register_io_function, &all_source);

if (all_source.sleep_change && all_source.sleep_state == 0 && _on_wake_up_callback != nullptr) {
_on_wake_up_callback();
}
});
};

setDataReadyInterruptCallback(on_wake_up_callback);
}

void CoreIMU::disableOnWakeUpInterrupt()
{
lsm6dsox_pin_int1_route_t lsm6dsox_int1 {
.sleep_change = PROPERTY_DISABLE,
};
lsm6dsox_pin_int1_route_set(&_register_io_function, lsm6dsox_int1);

setDataReadyInterruptCallback({});
}

auto CoreIMU::read(uint8_t register_address, uint16_t number_bytes_to_read, uint8_t *p_buffer) -> int32_t
{
// Send component address, without STOP condition
Expand Down Expand Up @@ -177,6 +226,8 @@ void CoreIMU::setDataReadyInterruptCallback(std::function<void()> const &callbac
{
if (callback) {
_irq.onRise(callback);
} else {
_irq.onRise([] {});
}
}

Expand Down
88 changes: 88 additions & 0 deletions drivers/CoreIMU/tests/CoreIMU_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using namespace leka;

using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::MockFunction;

Expand Down Expand Up @@ -120,3 +121,90 @@ 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)
{
EXPECT_CALL(mocki2c, write).Times(AtLeast(1));
EXPECT_CALL(mocki2c, read).Times(AtLeast(1));

coreimu.enableOnWakeUpInterrupt();
}

TEST_F(CoreIMUTest, disableOnWakeUpInterrupt)
{
EXPECT_CALL(mocki2c, write).Times(AtLeast(1));
EXPECT_CALL(mocki2c, read).Times(AtLeast(1));

coreimu.disableOnWakeUpInterrupt();
}

TEST_F(CoreIMUTest, switchCallbacks)
{
auto mock_data_available_callback = MockFunction<void(const leka::interface::IMU::SensorData &data)> {};
auto mock_wake_up_callback = MockFunction<void()> {};
auto on_rise_callback = mbed::Callback<void()> {};

EXPECT_CALL(mocki2c, write).Times(AnyNumber());
EXPECT_CALL(mocki2c, read).Times(AnyNumber());

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

{
// Enable Data Available
EXPECT_CALL(mock_data_available_callback, Call).Times(1);
EXPECT_CALL(mock_wake_up_callback, Call).Times(0);
coreimu.enableOnDataReadyInterrupt();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

{
// Disable Data Available
EXPECT_CALL(mock_data_available_callback, Call).Times(0);
EXPECT_CALL(mock_wake_up_callback, Call).Times(0);
coreimu.disableOnDataReadyInterrupt();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

{
// Enable Wake Up
EXPECT_CALL(mock_data_available_callback, Call).Times(0);
EXPECT_CALL(mock_wake_up_callback, Call).Times(AnyNumber());
coreimu.enableOnWakeUpInterrupt();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

{
// Disable Wake Up
EXPECT_CALL(mock_data_available_callback, Call).Times(0);
EXPECT_CALL(mock_wake_up_callback, Call).Times(0);
coreimu.disableOnWakeUpInterrupt();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}
}
4 changes: 4 additions & 0 deletions include/interface/drivers/IMU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ 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 setPowerMode(PowerMode) = 0;
};
} // namespace leka::interface
6 changes: 6 additions & 0 deletions tests/unit/mocks/mocks/leka/IMU.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ 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));
MOCK_METHOD(void, disableOnWakeUpInterrupt, (), (override));

void call_data_ready_callback(const SensorData &data) { data_ready_callback(data); }
void call_wake_up_callback() { wake_up_callback(); }

private:
data_ready_callback_t data_ready_callback {};
std::function<void()> wake_up_callback {};
};

} // namespace leka::mock