diff --git a/libs/RobotKit/include/RobotController.h b/libs/RobotKit/include/RobotController.h index e4406bf3e4..e45844460b 100644 --- a/libs/RobotKit/include/RobotController.h +++ b/libs/RobotKit/include/RobotController.h @@ -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) { @@ -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] { @@ -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 { @@ -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(is_charging); + _ble.setAdvertisingData(advertising_data); + + _service_battery.setBatteryLevel(level); + if (is_charging) { + onChargingBehavior(level); + } + }); auto on_low_battery = [this] { if (!_battery.isCharging()) { diff --git a/libs/RobotKit/tests/RobotController_test.h b/libs/RobotKit/tests/RobotController_test.h index defdf511b3..596f6a692c 100644 --- a/libs/RobotKit/tests/RobotController_test.h +++ b/libs/RobotKit/tests/RobotController_test.h @@ -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(&on_sleep_timeout)); diff --git a/libs/RobotKit/tests/RobotController_test_registerEvents.cpp b/libs/RobotKit/tests/RobotController_test_registerEvents.cpp index b79f0ca440..a2addb3e6b 100644 --- a/libs/RobotKit/tests/RobotController_test_registerEvents.cpp +++ b/libs/RobotKit/tests/RobotController_test_registerEvents.cpp @@ -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); @@ -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); @@ -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)