-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge branch 'yann/feature/battery/add-on-data-update' into develop
- Loading branch information
Showing
9 changed files
with
262 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Leka - LekaOS | ||
# Copyright 2022 APF France handicap | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
add_library(BatteryKit STATIC) | ||
|
||
target_include_directories(BatteryKit | ||
PUBLIC | ||
include | ||
) | ||
|
||
target_sources(BatteryKit | ||
PRIVATE | ||
source/BatteryKit.cpp | ||
) | ||
|
||
target_link_libraries(BatteryKit | ||
mbed-os | ||
CoreEventQueue | ||
) | ||
|
||
if (${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests") | ||
|
||
leka_unit_tests_sources( | ||
tests/BatteryKit_test.cpp | ||
) | ||
|
||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "CoreEventQueue.h" | ||
#include "interface/drivers/Battery.h" | ||
|
||
namespace leka { | ||
|
||
class BatteryKit | ||
{ | ||
public: | ||
explicit BatteryKit(interface::Battery &battery) : _battery(battery) {}; | ||
|
||
void start(); | ||
|
||
auto level() -> uint8_t; | ||
auto isCharging() -> bool; | ||
void onChargeDidStart(mbed::Callback<void()> const &callback); | ||
void onChargeDidStop(mbed::Callback<void()> const &callback); | ||
|
||
void onDataUpdated(std::function<void(uint8_t)> const &callback); | ||
void onLowBattery(std::function<void()> const &callback); | ||
|
||
private: | ||
interface::Battery &_battery; | ||
|
||
CoreEventQueue _event_queue {}; | ||
|
||
std::function<void(uint8_t)> _on_data_updated {}; | ||
std::function<void()> _on_low_battery {}; | ||
}; | ||
|
||
} // namespace leka |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "BatteryKit.h" | ||
|
||
using namespace leka; | ||
using namespace std::chrono_literals; | ||
|
||
void BatteryKit::start() | ||
{ | ||
auto on_tick = [this] { | ||
if (_on_low_battery && level() == 0) { | ||
_on_low_battery(); | ||
} | ||
|
||
if (_on_data_updated) { | ||
_on_data_updated(level()); | ||
} | ||
}; | ||
|
||
on_tick(); // TODO (@john_doe): only for unit tests. Due to event_queue that does not make the call | ||
_event_queue.call_every(1s, on_tick); | ||
|
||
_event_queue.dispatch_forever(); | ||
} | ||
|
||
auto BatteryKit::level() -> uint8_t | ||
{ | ||
return _battery.level(); | ||
} | ||
|
||
auto BatteryKit::isCharging() -> bool | ||
{ | ||
return _battery.isCharging(); | ||
} | ||
|
||
void BatteryKit::onChargeDidStart(mbed::Callback<void()> const &callback) | ||
{ | ||
_battery.onChargeDidStart(callback); | ||
} | ||
|
||
void BatteryKit::onChargeDidStop(mbed::Callback<void()> const &callback) | ||
{ | ||
_battery.onChargeDidStop(callback); | ||
} | ||
|
||
void BatteryKit::onDataUpdated(std::function<void(uint8_t)> const &callback) | ||
{ | ||
_on_data_updated = callback; | ||
} | ||
|
||
void BatteryKit::onLowBattery(std::function<void()> const &callback) | ||
{ | ||
_on_low_battery = callback; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "BatteryKit.h" | ||
|
||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "mocks/leka/Battery.h" | ||
|
||
using namespace leka; | ||
|
||
using ::testing::MockFunction; | ||
using ::testing::Return; | ||
|
||
class BatteryKitTest : public ::testing::Test | ||
{ | ||
protected: | ||
BatteryKitTest() : batterykit(mock_battery) {} | ||
|
||
// void SetUp() override {} | ||
// void TearDown() override {} | ||
|
||
mock::Battery mock_battery; | ||
BatteryKit batterykit; | ||
}; | ||
|
||
TEST_F(BatteryKitTest, instantiation) | ||
{ | ||
EXPECT_NE(&batterykit, nullptr); | ||
} | ||
|
||
TEST_F(BatteryKitTest, level) | ||
{ | ||
auto expected_value = 0x2A; | ||
|
||
EXPECT_CALL(mock_battery, level).WillOnce(Return(expected_value)); | ||
|
||
auto actual_value = batterykit.level(); | ||
|
||
EXPECT_EQ(actual_value, expected_value); | ||
} | ||
|
||
TEST_F(BatteryKitTest, isCharging) | ||
{ | ||
auto expected_value = true; | ||
|
||
EXPECT_CALL(mock_battery, isCharging).WillOnce(Return(expected_value)); | ||
|
||
auto actual_value = batterykit.isCharging(); | ||
|
||
EXPECT_EQ(actual_value, expected_value); | ||
} | ||
|
||
TEST_F(BatteryKitTest, onChargeDidStart) | ||
{ | ||
mbed::Callback<void()> callback_dummy([] {}); | ||
|
||
EXPECT_CALL(mock_battery, onChargeDidStart(callback_dummy)).Times(1); | ||
|
||
batterykit.onChargeDidStart(callback_dummy); | ||
} | ||
|
||
TEST_F(BatteryKitTest, onChargeDidStop) | ||
{ | ||
mbed::Callback<void()> callback_dummy([] {}); | ||
|
||
EXPECT_CALL(mock_battery, onChargeDidStop(callback_dummy)).Times(1); | ||
|
||
batterykit.onChargeDidStop(callback_dummy); | ||
} | ||
|
||
TEST_F(BatteryKitTest, onDataUpdated) | ||
{ | ||
auto battery_level = 0x2A; | ||
MockFunction<void(uint8_t)> mock_on_data_updated_callback; | ||
|
||
batterykit.onDataUpdated(mock_on_data_updated_callback.AsStdFunction()); | ||
|
||
EXPECT_CALL(mock_battery, level).WillRepeatedly(Return(battery_level)); | ||
EXPECT_CALL(mock_on_data_updated_callback, Call(battery_level)).Times(1); | ||
|
||
batterykit.start(); | ||
} | ||
|
||
TEST_F(BatteryKitTest, onLowBattery) | ||
{ | ||
auto battery_level = 0x2A; | ||
MockFunction<void()> mock_on_low_battery_callback; | ||
|
||
batterykit.onLowBattery(mock_on_low_battery_callback.AsStdFunction()); | ||
|
||
EXPECT_CALL(mock_battery, level).WillRepeatedly(Return(battery_level)); | ||
EXPECT_CALL(mock_on_low_battery_callback, Call).Times(0); | ||
|
||
batterykit.start(); | ||
} | ||
|
||
TEST_F(BatteryKitTest, onLowBatteryLevelAtZero) | ||
{ | ||
auto battery_level = 0x00; | ||
MockFunction<void()> mock_on_low_battery_callback; | ||
|
||
batterykit.onLowBattery(mock_on_low_battery_callback.AsStdFunction()); | ||
|
||
EXPECT_CALL(mock_battery, level).WillRepeatedly(Return(battery_level)); | ||
EXPECT_CALL(mock_on_low_battery_callback, Call).Times(1); | ||
|
||
batterykit.start(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters