From 532598fbaa68d71529f24dbd00822e27b3fa60d6 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Sun, 7 Jan 2024 21:39:55 +0800 Subject: [PATCH] =?UTF-8?q?[zhoulongyu]:=20=E5=AE=8C=E5=96=84Led=20?= =?UTF-8?q?=E9=80=82=E9=85=8D=E5=B1=82=E5=92=8C=E4=B8=AD=E9=97=B4=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hal/include/IHalCpp.h | 56 +++++++++++++++- hal/src/HalCpp.cpp | 5 ++ hal/src/HalCpp.h | 5 ++ .../DeviceManager/include/IDeviceManager.h | 12 ++++ .../DeviceManager/src/DeviceManager.cpp | 49 ++++++++++++++ middleware/DeviceManager/src/DeviceManager.h | 39 +++++++++++ middleware/DeviceManager/src/LedManager.cpp | 67 +++++++++++++++++++ middleware/DeviceManager/src/LedManager.h | 57 ++++++++++++++++ 8 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 middleware/DeviceManager/src/DeviceManager.cpp create mode 100644 middleware/DeviceManager/src/DeviceManager.h create mode 100644 middleware/DeviceManager/src/LedManager.cpp create mode 100644 middleware/DeviceManager/src/LedManager.h diff --git a/hal/include/IHalCpp.h b/hal/include/IHalCpp.h index 66a49936..e9c9891e 100644 --- a/hal/include/IHalCpp.h +++ b/hal/include/IHalCpp.h @@ -16,6 +16,7 @@ #define IHALCPP_H #include "StatusCode.h" #include +#include #include constexpr int INVALID_PERIOD = -1; constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100; @@ -44,6 +45,55 @@ public: virtual void SetKeyHalOwner(std::shared_ptr owner) {} virtual const std::string GetKeyName(void) { return "undefine"; } }; + + +enum class LedState +{ + LED_STATE_OFF = 0, + LED_STATE_ON, + LED_STATE_GREEN, + LED_STATE_RED, + LED_STATE_YELLOW, + LED_STATE_LEVEL_0, + LED_STATE_LEVEL_1, + LED_STATE_LEVEL_2, + LED_STATE_LEVEL_3, + LED_STATE_LEVEL_4, + LED_STATE_LEVEL_END, + LED_STATE_END +}; + + +class VLedHal +{ +public: + VLedHal() = default; + ~VLedHal() = default; + virtual StatusCode SetLedState(const LedState &state) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } + virtual StatusCode GetLedName(std::string &LedName) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } +}; + + +#if 0 +class WifiLed : public VLedHalOwner, public VLedHal +{ +public: + WifiLed() = default; + ~WifiLed() = default; + + // StatusCode SetLedHalOwner(std::shared_ptr owner) override; + StatusCode SetLedState(const LedState &state) override; + StatusCode GetLedName(std::string &LedName) override; + +private: + // VLedHalOwner m_LedHalOwner; + std::string m_LedName; + SF_LED_GPIO_IDX_E mPinRed; + SF_LED_GPIO_IDX_E mPinGreen; +} +#endif + + class IHalCpp { public: @@ -52,6 +102,10 @@ public: static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } virtual StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } + virtual StatusCode GetLedHal(std::vector> &ledHals) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } }; void CreateHalCppModule(void); -#endif \ No newline at end of file + + +#endif + diff --git a/hal/src/HalCpp.cpp b/hal/src/HalCpp.cpp index ba77430d..095c22b9 100644 --- a/hal/src/HalCpp.cpp +++ b/hal/src/HalCpp.cpp @@ -24,3 +24,8 @@ StatusCode HalCpp::UnInit(void) LogInfo("HalCpp::UnInit\n"); return CreateStatusCode(STATUS_CODE_OK); } +StatusCode HalCpp::GetLedHal(std::vector> &ledHals) +{ + ledHals = mLedHals; + return CreateStatusCode(STATUS_CODE_OK); +} diff --git a/hal/src/HalCpp.h b/hal/src/HalCpp.h index 1017cc2b..ead32f5b 100644 --- a/hal/src/HalCpp.h +++ b/hal/src/HalCpp.h @@ -22,5 +22,10 @@ public: virtual ~HalCpp() = default; StatusCode Init(void) override; StatusCode UnInit(void) override; + StatusCode GetLedHal(std::vector> &ledHals) override; + +private: + std::vector> mLedHals; + }; #endif \ No newline at end of file diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index f83e1e98..74fa4940 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -16,6 +16,7 @@ #define IDEVICEMANAGER_H #include "StatusCode.h" #include +#include "IHalCpp.h" enum class IpcMission { TEST = 0, @@ -29,6 +30,15 @@ enum class KeyAction HOLD_UP, END }; + +class VLedManager +{ +public: + VLedManager() = default; + ~VLedManager() = default; + virtual StatusCode SetLedState(LedState CurrentState, unsigned int KeepAliveTime) {return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } +}; + class IDeviceManager { public: @@ -38,6 +48,8 @@ public: virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const IpcMission GetIpcMissiony(void) { return IpcMission::END; } + virtual const StatusCode GetLedManager(std::vector> &ledManagers) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } + }; bool CreateDeviceManagerModule(void); #endif \ No newline at end of file diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp new file mode 100644 index 00000000..7dc23035 --- /dev/null +++ b/middleware/DeviceManager/src/DeviceManager.cpp @@ -0,0 +1,49 @@ +/* + * 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 "DeviceManager.h" + +const StatusCode DeviceManager::Init(void) +{ + std::vector> ledHals; + IHalCpp::GetInstance()->GetLedHal(ledHals); + + for (auto it = ledHals.begin(); it != ledHals.end(); ++it) + { + std::shared_ptr ledHal = *it; + std::shared_ptr ledOut = std::make_shared(ledHal, NEW_LED_STATE, DEFAULT_KEEP_ALIVE_TIME, BLINKING_FAST_MS); + mLedManagers.push_back(ledOut); + } + return CreateStatusCode(STATUS_CODE_OK); +} +const StatusCode DeviceManager::UnInit(void) +{ + if (!mLedManagers.empty()) + { + mLedManagers.clear(); + } + + return CreateStatusCode(STATUS_CODE_OK); +} +const IpcMission DeviceManager::GetIpcMissiony(void) +{ + return IpcMission::TEST; +} +const StatusCode DeviceManager::GetLedManager(std::vector> &ledManagers) +{ + ledManagers = mLedManagers; + return CreateStatusCode(STATUS_CODE_OK); +} + diff --git a/middleware/DeviceManager/src/DeviceManager.h b/middleware/DeviceManager/src/DeviceManager.h new file mode 100644 index 00000000..d431bd5c --- /dev/null +++ b/middleware/DeviceManager/src/DeviceManager.h @@ -0,0 +1,39 @@ +/* + * 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 DEVICEMANAGER_H +#define DEVICEMANAGER_H +#include "LedManager.h" +#include "IDeviceManager.h" + + +class DeviceManager : public IDeviceManager +{ +public: + DeviceManager() = default; + virtual ~DeviceManager() = default; + + const StatusCode Init(void) override; + const StatusCode UnInit(void) override; + const IpcMission GetIpcMissiony(void) override; + const StatusCode GetLedManager(std::vector> &ledManagers) override; +private: + std::vector> mLedManagers; +}; + + + + +#endif + diff --git a/middleware/DeviceManager/src/LedManager.cpp b/middleware/DeviceManager/src/LedManager.cpp new file mode 100644 index 00000000..c32e4442 --- /dev/null +++ b/middleware/DeviceManager/src/LedManager.cpp @@ -0,0 +1,67 @@ +/* + * 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 "LedManager.h" +#include "ILog.h" + + + +LedManager::LedManager() +{ + mLedHal = nullptr; + mCurrentState = NEW_LED_STATE; + mBlinkPeriod = BLINKING_FAST_MS; + mKeepAliveTime = DEFAULT_KEEP_ALIVE_TIME; + mStateAliveTime = 0; +} +LedManager::LedManager(std::shared_ptr &LedHal, const LedState &CurrentState, + const unsigned int &KeepAliveTime, const unsigned int &BlinkPeriod) + : mLedHal(LedHal), mCurrentState(CurrentState), mKeepAliveTime(KeepAliveTime), mBlinkPeriod(BlinkPeriod) +{ + mStateAliveTime = 0; +} + + +StatusCode LedManager::SetLedState(LedState CurrentState, unsigned int KeepAliveTime) +{ + mCurrentState = CurrentState; + mKeepAliveTime = KeepAliveTime; + return CreateStatusCode(STATUS_CODE_OK); +} + + +StatusCode LedManager::GetLedState(LedState &CurrentState) +{ + CurrentState = mCurrentState; + return CreateStatusCode(STATUS_CODE_OK); +} + +StatusCode LedManager::BlinkOn(LedState CurrentState, unsigned int KeepAliveTime) +{ + mKeepAliveTime = KeepAliveTime; + return CreateStatusCode(STATUS_CODE_OK); +} + +StatusCode LedManager::BlinkOff(void) +{ + mCurrentState = LedState::LED_STATE_OFF; + return CreateStatusCode(STATUS_CODE_OK); +} + + + + + + diff --git a/middleware/DeviceManager/src/LedManager.h b/middleware/DeviceManager/src/LedManager.h new file mode 100644 index 00000000..e258e2da --- /dev/null +++ b/middleware/DeviceManager/src/LedManager.h @@ -0,0 +1,57 @@ +/* + * 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 LED_MANAGER_H +#define LED_MANAGER_H + +#include "IDeviceManager.h" +#include +#include + +constexpr LedState NEW_LED_STATE = LedState::LED_STATE_OFF; +constexpr unsigned int LED_NOT_BLINK = 0; +constexpr unsigned int BLINKING_FAST_MS = 500; +constexpr unsigned int BLINKING_SLOW_MS = 1000; +constexpr long int DEFAULT_KEEP_ALIVE_TIME = 1500; +constexpr unsigned int DELETED_LED_STATE = -1; +constexpr unsigned int DO_NOT_KEEP_ALIVE = -2; + +class LedManager : public VLedManager +{ +public: + LedManager(); + LedManager(std::shared_ptr &LedHal, const LedState &CurrentState, + const unsigned int &KeepAliveTime = DEFAULT_KEEP_ALIVE_TIME, + const unsigned int &BlinkPeriod = BLINKING_FAST_MS + ); + ~LedManager() = default; + StatusCode Init(void) {return CreateStatusCode(STATUS_CODE_NOT_OK); } + StatusCode Unit(void) {return CreateStatusCode(STATUS_CODE_NOT_OK); } + StatusCode SetLedState(LedState CurrentState, unsigned int KeepAliveTime) override; + +public: + StatusCode GetLedState(LedState &CurrentState) {return CreateStatusCode(STATUS_CODE_NOT_OK); } + StatusCode BlinkOn(LedState CurrentState, unsigned int KeepAliveTime) {return CreateStatusCode(STATUS_CODE_NOT_OK); } + StatusCode BlinkOff(void) {return CreateStatusCode(STATUS_CODE_NOT_OK); } + +private: + std::shared_ptr mLedHal; + LedState mCurrentState; // 当前状态 + unsigned int mBlinkPeriod; // 闪烁频率 + unsigned int mKeepAliveTime; // 保持存活时间 + unsigned int mStateAliveTime; // 状态保持时间 +}; + +#endif +