Add:led control function.

This commit is contained in:
Fancy code 2024-06-06 21:35:36 +08:00
parent 31ac2138d5
commit a9b4eee4ea
27 changed files with 344 additions and 52 deletions

View File

@ -10,6 +10,7 @@ include_directories(
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
${MIDDLEWARE_SOURCE_PATH}/StateMachine/include
${MIDDLEWARE_SOURCE_PATH}/AppManager/include
${MIDDLEWARE_SOURCE_PATH}/MediaManager/include
@ -29,7 +30,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME MissionManager)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} McuAskBase StateMachine MediaManager StorageManager DeviceManager HuntingUpgrade KeyControl StatusCode Log)
target_link_libraries(${TARGET_NAME} McuAskBase StateMachine MediaManager StorageManager DeviceManager HuntingUpgrade KeyControl LedControl StatusCode Log)
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "LedsHandle.h"
#include "ILog.h"
void LedsHandle::ControlDeviceStatusLed(const DeviceStatus &status, const long int &keepAliveTime,
const unsigned int &blinkPeriod)
{
switch (status) {
case DeviceStatus::NORMAL:
mDeviceStatus = SetLedState::ControlLed("device_status", LedState::GREEN, keepAliveTime, blinkPeriod);
break;
default:
LogWarning("unknow device status.\n");
break;
}
}
void inline LedsHandle::DeleteDeviceStatusLed(void)
{
mDeviceStatus->DeleteState();
}
void LedsHandle::DeleteAllLeds(void)
{
DeleteDeviceStatusLed();
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LEDS_HANDLE_H
#define LEDS_HANDLE_H
#include "SetLedState.h"
enum class DeviceStatus
{
NORMAL = 0,
TAKING_PICTURE_OR_VIDEO,
END
};
class LedsHandle
{
public:
LedsHandle() = default;
virtual ~LedsHandle() = default;
protected:
/**
* @brief This function is designed as a virtual function so that when the board uses different LED circuits, this
* function can be overloaded to achieve polymorphic control of the light.
* @param status
* @param keepAliveTime
* @param blinkPeriod
*/
virtual void ControlDeviceStatusLed(const DeviceStatus &status, const long int &keepAliveTime = KEEP_ALIVE_FOREVER,
const unsigned int &blinkPeriod = LED_NOT_BLINK);
void DeleteDeviceStatusLed(void);
void DeleteAllLeds(void);
private:
std::shared_ptr<SetLedState> mDeviceStatus;
};
#endif

View File

@ -30,6 +30,7 @@ void MissionState::GoInState()
void MissionState::GoOutState()
{
LogInfo(" ========== MissionState::GoOutState.\n");
LedsHandle::DeleteAllLeds();
}
bool MissionState::ExecuteStateMsg(VStateMachineData *msg)
{

View File

@ -16,7 +16,11 @@
#define MISSION_STATE_H
#include "DataProcessing.h"
#include "IStateMachine.h"
class MissionState : public State, public DataProcessing, public std::enable_shared_from_this<MissionState>
#include "LedsHandle.h"
class MissionState : public State,
public DataProcessing,
public LedsHandle,
public std::enable_shared_from_this<MissionState>
{
public:
MissionState(const std::string &name);

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SetLedState.h"
SetLedState::SetLedState(const LedState &state, const unsigned int &keepAliveTime, const unsigned int &blinkPeriod)
: mState(state), mKeepAliveTime(keepAliveTime), mBlinkPeriod(blinkPeriod)
{
}
StatusCode SetLedState::GetLedState(LedState &state)
{
state = mState;
return CreateStatusCode(STATUS_CODE_OK);
}
unsigned int SetLedState::GetKeepAliveTimeMs(void)
{
return mKeepAliveTime;
}
unsigned int SetLedState::GetBlinkTimeMs(void)
{
return mBlinkPeriod;
}
void SetLedState::DeleteState(void)
{
mKeepAliveTime = DELETED_LED_STATE;
}
std::shared_ptr<SetLedState> SetLedState::ControlLed(const std::string &ledName, const LedState &state,
const long int &keepAliveTime, const unsigned int &blinkPeriod)
{
std::shared_ptr<SetLedState> ledState = std::make_shared<SetLedState>(state, keepAliveTime, blinkPeriod);
std::shared_ptr<LedControlContext> ledState2 = ledState;
IDeviceManager::GetInstance()->ControlLed(ledName, ledState2);
return ledState;
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SET_LED_STATE_H
#define SET_LED_STATE_H
#include "IDeviceManager.h"
#include "LedControl.h"
constexpr int LED_BLINKING_FAST_PERIOD = 500;
constexpr int LED_BLINKING_SLOW_PERIOD = 1000;
class SetLedState : public LedControlContext, public VSingleControl
{
public:
SetLedState(const LedState &state, const unsigned int &keepAliveTime = KEEP_ALIVE_FOREVER,
const unsigned int &blinkPeriod = LED_NOT_BLINK);
virtual ~SetLedState() = default;
StatusCode GetLedState(LedState &state) override;
unsigned int GetKeepAliveTimeMs(void) override;
unsigned int GetBlinkTimeMs(void) override;
void DeleteState(void);
public:
static std::shared_ptr<SetLedState> ControlLed(const std::string &ledName, const LedState &state,
const long int &keepAliveTime = KEEP_ALIVE_FOREVER,
const unsigned int &blinkPeriod = LED_NOT_BLINK);
private:
const LedState mState;
unsigned int mKeepAliveTime;
const unsigned int mBlinkPeriod;
};
#endif

View File

@ -34,6 +34,7 @@ void TestMissionState::GoInState()
std::shared_ptr<VAppMonitor> monitor =
std::dynamic_pointer_cast<TestMissionState>(MissionState::shared_from_this());
IAppManager::GetInstance()->SetAppMonitor(monitor);
ControlDeviceStatusLed(DeviceStatus::NORMAL);
}
void TestMissionState::GoOutState()
{

View File

@ -38,11 +38,11 @@ public:
virtual ~VKeyMonitor() = default;
virtual void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs);
};
class VirtualLedControl
class LedControlContext
{
public:
VirtualLedControl() = default;
virtual ~VirtualLedControl() = default;
LedControlContext() = default;
virtual ~LedControlContext() = default;
};
class IDeviceManager
{
@ -52,7 +52,7 @@ public:
static std::shared_ptr<IDeviceManager> &GetInstance(std::shared_ptr<IDeviceManager> *impl = nullptr);
virtual const StatusCode Init(void);
virtual const StatusCode UnInit(void);
virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control);
virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control);
virtual const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor);
};
#endif

View File

@ -36,7 +36,7 @@ const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &
LogInfo("DeviceManager::SetAllKeysMonitor\n");
return KeyManager::GetInstance()->SetKeyMonitor(monitor);
}
const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control)
const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control)
{
LedManager::GetInstance()->ControlLed(ledName, control);
return CreateStatusCode(STATUS_CODE_OK);

View File

@ -26,7 +26,7 @@ public:
const StatusCode Init(void) override;
const StatusCode UnInit(void) override;
const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor) override;
const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control) override;
const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control) override;
private:
// std::vector<std::shared_ptr<LedManager>> mLedManagers;

View File

@ -39,7 +39,7 @@ const StatusCode IDeviceManager::UnInit(void)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control)
const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -37,6 +37,11 @@ void LedManager::Init(void)
void LedManager::UnInit(void)
{
StopTimer();
mMutex.lock();
for (auto &iter : mAllLedHal) {
iter.second->DeleteAllState();
}
mMutex.unlock();
mAllLedHal.clear();
}
void LedManager::Timer(void)
@ -53,7 +58,7 @@ void LedManager::Timer(void)
std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS));
}
}
void LedManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control)
void LedManager::ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control)
{
std::lock_guard<std::mutex> locker(mMutex);
std::shared_ptr<VSingleControl> singleControl = std::dynamic_pointer_cast<VSingleControl>(control);
@ -61,6 +66,11 @@ void LedManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualL
LogError("led can't be controled.\n");
return;
}
auto iter = mAllLedHal.find(ledName);
if (iter == mAllLedHal.end()) {
LogError("Can't find led [%s].\n", ledName.c_str());
return;
}
mAllLedHal[ledName]->AddLedState(singleControl);
}
void LedManager::StartTimer(void)

View File

@ -31,7 +31,7 @@ public:
void UnInit(void);
void StartTimer(void);
void StopTimer(void);
void ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control);
void ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control);
private:
void Timer(void);

View File

@ -65,4 +65,18 @@ TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlClick)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl)
{
SetAllLedsResult(mAllLedsMock);
MockOtherSideIpcMissionReply(IpcMission::TEST);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
} // namespace DeviceManager_Mock_Test

View File

@ -18,6 +18,7 @@
#include "MainThread.h"
#include <thread>
const char *KEY_RESET = "reset";
const char *LED_DEVICE_STATUS = "device_status";
void MainThreadTest::CustomizationInit(void)
{
// Do nothing here to make sure test tool work.
@ -44,6 +45,7 @@ void HuntingCameraTest::SetUp()
MainThread::GetInstance(&mainThread);
HalTestTool::Init();
CreateAllKeysMcok();
CreateAllLedsMcok();
AppManagerTestTool::Init();
MissionManagerTestTool::Init();
mLinuxTest = LinuxTest::CreateLinuxTest();
@ -72,6 +74,7 @@ void HuntingCameraTest::TearDown()
DeviceManagerTestTool::UnInit();
DestroyAllCamerasMock();
DestroyAllKeysMock();
DestroyAllLedsMock();
}
void HuntingCameraTest::CreateAllCamerasMcok(void)
{
@ -86,13 +89,19 @@ void HuntingCameraTest::CreateAllKeysMcok(void)
{
std::shared_ptr<VKeyHal> key = HalTestTool::MakeKeyHalTest(KEY_RESET);
mAllKeysMock[KEY_RESET] = key;
// std::shared_ptr<VLedHal> led = HalTestTool::MakeLedHalTest(LED_TEST);
// mAllLedsMock[LED_TEST] = led;
HalTestTool::SetAllKeysResult(mAllKeysMock);
}
void HuntingCameraTest::DestroyAllKeysMock(void)
{
mAllKeysMock.clear();
// mAllLedsMock.clear();
}
void HuntingCameraTest::CreateAllLedsMcok(void)
{
std::shared_ptr<VLedHal> led = HalTestTool::MakeLedHalTest(LED_DEVICE_STATUS);
mAllLedsMock[LED_DEVICE_STATUS] = led;
}
void HuntingCameraTest::DestroyAllLedsMock(void)
{
mAllLedsMock.clear();
}

View File

@ -52,11 +52,14 @@ private:
void DestroyAllCamerasMock(void);
void CreateAllKeysMcok(void);
void DestroyAllKeysMock(void);
void CreateAllLedsMcok(void);
void DestroyAllLedsMock(void);
protected:
std::shared_ptr<LinuxTest> mLinuxTest;
std::map<CameraType, std::shared_ptr<VCameraHal>> mAllCamerasMock;
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeysMock;
std::map<std::string, std::shared_ptr<VLedHal>> mAllLedsMock;
};
#endif

View File

@ -22,6 +22,7 @@ include_directories(
${UTILS_SOURCE_PATH}/LinuxApi/include
${UTILS_SOURCE_PATH}/UpgradeTool/include
${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/middleware/AppManager/tool/include
${TEST_SOURCE_PATH}/middleware/AppManager/tool/src
@ -37,7 +38,7 @@ include_directories(
aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET MissionManagerTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
target_link_libraries(${TEST_TOOL_TARGET} MissionManager AppManagerTestTool MediaManagerTestTool KeyControl UpgradeTool StatusCode Log)
target_link_libraries(${TEST_TOOL_TARGET} MissionManager AppManagerTestTool MediaManagerTestTool KeyControl LedControl UpgradeTool StatusCode Log)
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(

View File

@ -272,8 +272,10 @@ void HalTestTool::InitLedsMock(std::shared_ptr<VLedHal> &vMock)
LogError("vMock error.\n");
return;
}
constexpr int LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT = 0;
EXPECT_CALL(*mock.get(), SetLedStateTrace(_)).Times(LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT);
// constexpr int LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT = 0;
EXPECT_CALL(*mock.get(), SetLedStateTrace(_))
.Times(AnyNumber())
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_OK))));
}
void HalTestTool::SetAllCamerasResult(std::shared_ptr<IHalCpp> &vMock,
std::map<CameraType, std::shared_ptr<VCameraHal>> &allCameras)

View File

@ -16,11 +16,9 @@
#include "ILog.h"
LedControlTest::LedControlTest(const std::string &ledName) : mLedName(ledName)
{
//
}
LedControlMock::LedControlMock(const std::string &ledName) : LedControlTest(ledName)
{
//
}
StatusCode LedControlTest::SetLedState(const LedState &state)
{

View File

@ -196,4 +196,20 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4)
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
std::shared_ptr<LedControlContext> ledControl =
DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
DeviceManagerTestTool::DestoryLedControl(ledControl);
DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest

View File

@ -54,8 +54,9 @@ public:
void UnInit(void);
public:
std::shared_ptr<VirtualLedControl> ControlLed(const std::string &ledName, const LedState &state,
std::shared_ptr<LedControlContext> ControlLed(const std::string &ledName, const LedState &state,
const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs);
void DestoryLedControl(std::shared_ptr<LedControlContext> &ledControl);
protected:
void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override;

View File

@ -38,16 +38,26 @@ void DeviceManagerTestTool::UnInit(void)
mKeyMonitorMock.reset();
CancelOverrideDeviceMakePtrObject();
}
std::shared_ptr<VirtualLedControl> DeviceManagerTestTool::ControlLed(const std::string &ledName, const LedState &state,
std::shared_ptr<LedControlContext> DeviceManagerTestTool::ControlLed(const std::string &ledName, const LedState &state,
const unsigned int &aliveTimeMs,
const unsigned int &blinkTimeMs)
{
HalTestTool::SetLedStateExpectations(ledName, state, aliveTimeMs, blinkTimeMs);
std::shared_ptr<VirtualLedControl> ledControl =
std::shared_ptr<LedControlContext> ledControl =
std::make_shared<SingleControlMock>(state, aliveTimeMs, blinkTimeMs);
IDeviceManager::GetInstance()->ControlLed(ledName, ledControl);
return ledControl;
}
void DeviceManagerTestTool::DestoryLedControl(std::shared_ptr<LedControlContext> &ledControl)
{
std::shared_ptr<SingleControlMock> ledControlMock = std::dynamic_pointer_cast<SingleControlMock>(ledControl);
if (ledControlMock) {
ledControlMock->DeleteState();
}
else {
LogWarning("DestoryLedCtx failed.\n");
}
}
void DeviceManagerTestTool::DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs)
{
LogInfo("DeviceManagerTestTool::DeviceManagerNotice\n");

View File

@ -24,6 +24,10 @@ StatusCode SingleControlTool::GetLedState(LedState &state)
state = mState;
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
void SingleControlTool::DeleteState(void)
{
mAliveTimeMs = DELETED_LED_STATE;
}
SingleControlMock::SingleControlMock(const LedState &state, const unsigned int aliveTimeMs,
const unsigned int &blinkTimeMs)
: SingleControlTool(state, aliveTimeMs, blinkTimeMs)

View File

@ -16,7 +16,7 @@
#define LED_CONTROL_MOCK_H
#include "IDeviceManager.h"
#include "LedControl.h"
class SingleControlTool : public VSingleControl, public VirtualLedControl
class SingleControlTool : public VSingleControl, public LedControlContext
{
public:
SingleControlTool(const LedState &state, const unsigned int aliveTimeMs, const unsigned int &blinkTimeMs);
@ -30,11 +30,12 @@ public:
{
return mBlinkTimeMs;
}
void DeleteState(void);
private:
const LedState mState;
const unsigned int mAliveTimeMs;
const unsigned int mBlinkTimeMs;
unsigned int mAliveTimeMs;
unsigned int mBlinkTimeMs;
};
class SingleControlMock : public SingleControlTool
{

View File

@ -30,7 +30,12 @@ enum class LedState
ON,
GREEN,
RED,
BLUE,
YELLOW,
GREEN_RED_MEANS_YELLOW,
GREEN_BLUE_MEANS_CYAN,
RED_BLUE_MEANS_PURPLE,
GREEN_RED_BLUE_MEANS_WHITE,
LEVEL_0,
LEVEL_1,
LEVEL_2,
@ -44,39 +49,20 @@ class VSingleControl
public:
VSingleControl() = default;
virtual ~VSingleControl() = default;
virtual StatusCode GetLedState(LedState &state)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual unsigned int GetKeepAliveTimeMs(void)
{
return KEEP_ALIVE_FOREVER;
}
virtual unsigned int GetBlinkTimeMs(void)
{
return LED_NOT_BLINK;
}
virtual StatusCode GetLedState(LedState &state);
virtual unsigned int GetKeepAliveTimeMs(void);
virtual unsigned int GetBlinkTimeMs(void);
};
class VLedControl
{
public:
VLedControl() = default;
virtual ~VLedControl() = default;
virtual StatusCode SetLedState(const LedState &state)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual void AddLedState(std::shared_ptr<VSingleControl> &control)
{
}
virtual void CheckState(const unsigned int &period)
{
}
// virtual void SetHalLedState(const VirtualLedState &state) {}
virtual std::string GetLedName(void)
{
return "undefine";
}
virtual StatusCode SetLedState(const LedState &state);
virtual void AddLedState(std::shared_ptr<VSingleControl> &control);
virtual void CheckState(const unsigned int &period);
virtual std::string GetLedName(void);
virtual void DeleteAllState(void);
};
class LedControl : virtual public VLedControl
{
@ -87,6 +73,12 @@ public:
void AddLedState(std::shared_ptr<VSingleControl> &state) override;
private:
void DeleteAllState(void) override;
/**
* @brief Each time you control a light, check for invalid data to avoid wasting memory resources.
*
*/
void DeleteUselessState(void);
void NewLedStateStart(void);
void DeleteTopLedState(void);
void BlinkOff(std::shared_ptr<VSingleControl> &state);

View File

@ -14,9 +14,45 @@
*/
#include "LedControl.h"
#include "ILog.h"
#include <algorithm>
StatusCode VSingleControl::GetLedState(LedState &state)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
unsigned int VSingleControl::GetKeepAliveTimeMs(void)
{
return KEEP_ALIVE_FOREVER;
}
unsigned int VSingleControl::GetBlinkTimeMs(void)
{
return LED_NOT_BLINK;
}
StatusCode VLedControl::SetLedState(const LedState &state)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
void VLedControl::AddLedState(std::shared_ptr<VSingleControl> &control)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
}
void VLedControl::CheckState(const unsigned int &period)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
}
std::string VLedControl::GetLedName(void)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return "undefine";
}
void VLedControl::DeleteAllState(void)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
}
void LedControl::AddLedState(std::shared_ptr<VSingleControl> &state)
{
LogInfo("Add led state.\n");
DeleteUselessState();
NewLedStateStart();
LedState ledState = LedState::END;
state->GetLedState(ledState);
@ -27,6 +63,24 @@ void LedControl::AddLedState(std::shared_ptr<VSingleControl> &state)
}
mStates.push_back(state);
}
void LedControl::DeleteAllState(void)
{
mStates.clear();
SetLedState(LedState::OFF);
}
void LedControl::DeleteUselessState(void)
{
constexpr bool DELETE_STATE = true;
constexpr bool KEEP_STATE = false;
auto is_to_remove = [](const std::shared_ptr<VSingleControl> &state) {
if (DELETED_LED_STATE == state->GetKeepAliveTimeMs()) {
LogInfo(" Delete useless led state.\n");
return DELETE_STATE;
}
return KEEP_STATE;
};
mStates.erase(std::remove_if(mStates.begin(), mStates.end(), is_to_remove), mStates.end());
}
void LedControl::CheckState(const unsigned int &period)
{
const int TOP_STATE_SHOW = mStates.size() - 1;
@ -71,6 +125,7 @@ void LedControl::DeleteTopLedState(void)
mStates.erase(mStates.begin() + TOP_STATE_SHOW);
const int NEXT_LED_STATE = mStates.size() - 1;
if (NEXT_LED_STATE < 0) {
SetLedState(LedState::OFF);
return;
}
LogInfo("Top next led state.\n");