Skip to content

Commit

Permalink
🔀 Merge branch 'yann/feature/robot-controller/update-advertising-data…
Browse files Browse the repository at this point in the history
…' into develop
  • Loading branch information
ladislas committed May 23, 2022
2 parents b184e1e + 63d19b6 commit 97fdaff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
25 changes: 15 additions & 10 deletions libs/RobotKit/include/RobotController.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ class RobotController : public interface::RobotController
return is_charging;
}

void onStartChargingBehavior(uint8_t level)
void onChargingBehavior(uint8_t level)
{
_service_battery.setBatteryLevel(level);

if (level < 5) {
_behaviorkit.chargingZero();
} else if (level < 25) {
Expand All @@ -145,7 +143,6 @@ class RobotController : public interface::RobotController
using namespace std::chrono_literals;
using namespace system::robot::sm;

_battery_kit.onDataUpdated([this](uint8_t level) { onStartChargingBehavior(level); });
_lcd.turnOn();

auto on_charging_start_timeout = [this] {
Expand All @@ -157,11 +154,7 @@ class RobotController : public interface::RobotController
_timeout.start(1min);
}

void stopChargingBehavior() final
{
_timeout.stop();
_battery_kit.onDataUpdated([this](uint8_t level) { _service_battery.setBatteryLevel(level); });
}
void stopChargingBehavior() final { _timeout.stop(); }

void startConnectionBehavior() final
{
Expand Down Expand Up @@ -222,7 +215,19 @@ class RobotController : public interface::RobotController

// Setup callbacks for monitoring

_battery_kit.onDataUpdated([this](uint8_t level) { _service_battery.setBatteryLevel(level); });
_battery_kit.onDataUpdated([this](uint8_t level) {
auto is_charging = _battery.isCharging();

auto advertising_data = _ble.getAdvertisingData();
advertising_data.battery = level;
advertising_data.is_charging = static_cast<uint8_t>(is_charging);
_ble.setAdvertisingData(advertising_data);

_service_battery.setBatteryLevel(level);
if (is_charging) {
onChargingBehavior(level);
}
});

auto on_low_battery = [this] {
if (!_battery.isCharging()) {
Expand Down
2 changes: 2 additions & 0 deletions libs/RobotKit/tests/RobotController_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class RobotControllerTest : public testing::Test

Sequence on_data_updated_sequence;
EXPECT_CALL(battery, level).InSequence(on_data_updated_sequence);
EXPECT_CALL(battery, isCharging).InSequence(on_data_updated_sequence);
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).InSequence(on_data_updated_sequence);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).InSequence(on_data_updated_sequence);

EXPECT_CALL(timeout, onTimeout).WillOnce(GetCallback<interface::Timeout::callback_t>(&on_sleep_timeout));
Expand Down
37 changes: 16 additions & 21 deletions libs/RobotKit/tests/RobotController_test_registerEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsNotCharging)
EXPECT_CALL(battery, level).InSequence(on_low_battery_sequence);

Sequence on_data_updated_sequence;
EXPECT_CALL(battery, level).InSequence(on_data_updated_sequence);
EXPECT_CALL(battery, level).InSequence(on_data_updated_sequence).WillOnce(Return(false));
EXPECT_CALL(battery, isCharging).InSequence(on_data_updated_sequence);
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).InSequence(on_data_updated_sequence);
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).InSequence(on_data_updated_sequence);

Expand Down Expand Up @@ -67,7 +69,10 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsCharging)

Sequence on_data_updated_sequence;
EXPECT_CALL(battery, level).InSequence(on_data_updated_sequence);
EXPECT_CALL(battery, isCharging).InSequence(on_data_updated_sequence).WillOnce(Return(true));
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).InSequence(on_data_updated_sequence);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).InSequence(on_data_updated_sequence);
EXPECT_CALL(mock_videokit, displayImage).InSequence(on_data_updated_sequence);

EXPECT_CALL(timeout, onTimeout);

Expand Down Expand Up @@ -103,59 +108,49 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsCharging)
rc.registerEvents();
}

TEST_F(RobotControllerTest, onStartChargingBehaviorLevelBelow25)
TEST_F(RobotControllerTest, onChargingBehaviorLevelBelow25)
{
auto battery_level = 0;

EXPECT_CALL(mock_videokit, displayImage(std::filesystem::path {"/fs/images/low_battery.jpg"})).Times(1);
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _));

rc.onStartChargingBehavior(battery_level);
rc.onChargingBehavior(battery_level);
}

TEST_F(RobotControllerTest, onStartChargingBehaviorLevelAbove5Below25)
TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove5Below25)
{
auto battery_level = 22;

EXPECT_CALL(mock_videokit, displayImage(std::filesystem::path {"/fs/images/battery_red.jpg"})).Times(1);
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _));

rc.onStartChargingBehavior(battery_level);
rc.onChargingBehavior(battery_level);
}

TEST_F(RobotControllerTest, onStartChargingBehaviorLevelAbove25Below50)
TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove25Below50)
{
auto battery_level = 42;

EXPECT_CALL(mock_videokit, displayImage(std::filesystem::path {"/fs/images/battery_yellow_2.jpg"})).Times(1);
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _));

rc.onStartChargingBehavior(battery_level);
rc.onChargingBehavior(battery_level);
}

TEST_F(RobotControllerTest, onStartChargingBehaviorLevelAbove50Below75)
TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove50Below75)
{
auto battery_level = 66;

EXPECT_CALL(mock_videokit, displayImage(std::filesystem::path {"/fs/images/battery_green_3.jpg"})).Times(1);
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _));

rc.onStartChargingBehavior(battery_level);
rc.onChargingBehavior(battery_level);
}

TEST_F(RobotControllerTest, onStartChargingBehaviorLevelAbove75)
TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove75)
{
auto battery_level = 90;

EXPECT_CALL(mock_videokit, displayImage(std::filesystem::path {"/fs/images/battery_green_4.jpg"})).Times(1);
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _));

rc.onStartChargingBehavior(battery_level);
rc.onChargingBehavior(battery_level);
}

TEST_F(RobotControllerTest, onBleConnection)
Expand Down

0 comments on commit 97fdaff

Please sign in to comment.