Skip to content

Commit

Permalink
♻️ (imu): Break setDataReadyInterrupt in multiple methods
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Jul 19, 2024
1 parent eb48c4f commit 25dfefe
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
5 changes: 4 additions & 1 deletion drivers/CoreIMU/include/CoreIMU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class CoreIMU : public interface::IMU, public interface::DeepSleepEnabled
void init() final;

void registerOnDataReadyCallback(data_ready_callback_t const &callback) final;
void enableOnDataReadyInterrupt() final;
void disableOnDataReadyInterrupt() final;

void setPowerMode(PowerMode mode) final;

Expand All @@ -39,7 +41,8 @@ class CoreIMU : public interface::IMU, public interface::DeepSleepEnabled
uint16_t number_bytes_to_read) -> int32_t;

void onDataReadyHandler(auto timestamp);
void setDataReadyInterrupt();

void setDataReadyInterruptCallback(std::function<void()> const &callback);

interface::I2C &_i2c;
CoreEventQueue _event_queue {};
Expand Down
46 changes: 32 additions & 14 deletions drivers/CoreIMU/source/CoreIMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void CoreIMU::init()
lsm6dsox_dataready_pulsed_t data_ready_pulsed {LSM6DSOX_DRDY_PULSED};
lsm6dsox_data_ready_mode_set(&_register_io_function, data_ready_pulsed);

setDataReadyInterrupt();
enableOnDataReadyInterrupt();
}

void CoreIMU::setPowerMode(PowerMode mode)
Expand Down Expand Up @@ -100,6 +100,33 @@ void CoreIMU::onDataReadyHandler(auto timestamp)
}
}

void CoreIMU::enableOnDataReadyInterrupt()
{
lsm6dsox_pin_int1_route_t lsm6dsox_int1 {
.drdy_xl = PROPERTY_ENABLE,
.den_flag = PROPERTY_ENABLE,
};
lsm6dsox_pin_int1_route_set(&_register_io_function, lsm6dsox_int1);

auto on_data_ready_callback = [this] {
auto timestamp = rtos::Kernel::Clock::now();
_event_queue.call([this, timestamp] { onDataReadyHandler(timestamp); });
};

setDataReadyInterruptCallback(on_data_ready_callback);
}

void CoreIMU::disableOnDataReadyInterrupt()
{
lsm6dsox_pin_int1_route_t lsm6dsox_int1 {
.drdy_xl = PROPERTY_DISABLE,
.den_flag = PROPERTY_DISABLE,
};
lsm6dsox_pin_int1_route_set(&_register_io_function, lsm6dsox_int1);

setDataReadyInterruptCallback({});
}

void CoreIMU::enableDeepSleep()
{
setPowerMode(interface::IMU::PowerMode::Off);
Expand Down Expand Up @@ -146,20 +173,11 @@ auto CoreIMU::ptr_io_read(CoreIMU *handle, uint8_t read_address, uint8_t *p_buff
return handle->read(read_address, number_bytes_to_read, p_buffer);
}

void CoreIMU::setDataReadyInterrupt()
void CoreIMU::setDataReadyInterruptCallback(std::function<void()> const &callback)
{
lsm6dsox_pin_int1_route_t lsm6dsox_int1 {
.drdy_xl = PROPERTY_ENABLE,
.den_flag = PROPERTY_ENABLE,
};
lsm6dsox_pin_int1_route_set(&_register_io_function, lsm6dsox_int1);

auto data_ready_callback = [this] {
auto timestamp = rtos::Kernel::Clock::now();
_event_queue.call([this, timestamp] { onDataReadyHandler(timestamp); });
};

_irq.onRise(data_ready_callback);
if (callback) {
_irq.onRise(callback);
}
}

} // namespace leka
16 changes: 16 additions & 0 deletions drivers/CoreIMU/tests/CoreIMU_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ TEST_F(CoreIMUTest, emptyOnDataReadyCallback)
on_rise_callback();
}

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

coreimu.enableOnDataReadyInterrupt();
}

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

coreimu.disableOnDataReadyInterrupt();
}

TEST_F(CoreIMUTest, enableDeepSleep)
{
EXPECT_CALL(mocki2c, write).Times(AtLeast(1));
Expand Down
9 changes: 7 additions & 2 deletions include/interface/drivers/IMU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ class IMU

using data_ready_callback_t = std::function<void(const SensorData)>;

virtual void init() = 0;
virtual void init() = 0;

virtual void registerOnDataReadyCallback(data_ready_callback_t const &callback) = 0;
virtual void setPowerMode(PowerMode) = 0;

virtual void enableOnDataReadyInterrupt() = 0;
virtual void disableOnDataReadyInterrupt() = 0;

virtual void setPowerMode(PowerMode) = 0;
};
} // namespace leka::interface
2 changes: 2 additions & 0 deletions tests/unit/mocks/mocks/leka/IMU.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class IMU : public interface::IMU
MOCK_METHOD(void, setPowerMode, (PowerMode), (override));

void registerOnDataReadyCallback(data_ready_callback_t const &cb) override { data_ready_callback = cb; }
MOCK_METHOD(void, enableOnDataReadyInterrupt, (), (override));
MOCK_METHOD(void, disableOnDataReadyInterrupt, (), (override));

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

Expand Down

0 comments on commit 25dfefe

Please sign in to comment.