[zhoulongyu]: 1.LedTimer的代码 2. 增加Led_Test的代码

This commit is contained in:
jas 2024-02-08 23:35:19 +08:00
parent 92a62a1db6
commit 69b327bfdc
13 changed files with 270 additions and 16 deletions

View File

@ -22,7 +22,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME DeviceManager)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} StatusCode Log)
target_link_libraries(${TARGET_NAME} Hal StatusCode Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(

View File

@ -14,6 +14,7 @@
*/
#ifndef IDEVICEMANAGER_H
#define IDEVICEMANAGER_H
#include "ILog.h"
#include "StatusCode.h"
#include <iostream>
#include <memory>

View File

@ -14,6 +14,7 @@
*/
#include "DeviceManager.h"
#include "LedTimer.h"
#include <vector>
const StatusCode DeviceManager::Init(void)
@ -26,7 +27,10 @@ const StatusCode DeviceManager::Init(void)
std::shared_ptr<LedManager> ledOut =
std::make_shared<LedManager>(ledHal, NEW_LED_STATE, DEFAULT_KEEP_ALIVE_TIME, LED_NOT_BLINK);
mLedManagers.push_back(ledOut);
LedTimer::GetInstance()->AddTimerLedManager(ledOut);
}
LedTimer::GetInstance()->Init();
return CreateStatusCode(STATUS_CODE_OK);
}
@ -35,7 +39,7 @@ const StatusCode DeviceManager::UnInit(void)
if (!mLedManagers.empty()) {
mLedManagers.clear();
}
LedTimer::GetInstance()->UnInit();
return CreateStatusCode(STATUS_CODE_OK);
}
@ -44,6 +48,10 @@ const IpcMission DeviceManager::GetIpcMission(void) { return IpcMission::TEST; }
const StatusCode DeviceManager::ISetLedState(std::string ledName, LedState &CurrentState,
const unsigned int &KeepAliveTime, const unsigned int &BlinkPeriod)
{
if (!ledName.empty() || *ledName.c_str() == '\0') {
LogDebug("\n\n\n\n %s %d\n\n\n", __func__, __LINE__);
}
for (auto it = mLedManagers.begin(); it != mLedManagers.end(); ++it) {
std::shared_ptr<LedManager> ledOut = *it;
if (ledOut->GetLedHal()->GetLedName() == ledName) {
@ -51,5 +59,6 @@ const StatusCode DeviceManager::ISetLedState(std::string ledName, LedState &Curr
return CreateStatusCode(STATUS_CODE_OK);
}
}
LogWarning("ledName(%s) not found", ledName.c_str());
return CreateStatusCode(STATUS_CODE_NOT_OK);
}

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "DeviceManagerMakePtr.h"
#include "DeviceManager.h"
#include "ILog.h"
bool CreateDeviceManagerModule(void)
{
@ -35,7 +36,7 @@ std::shared_ptr<DeviceManagerMakePtr> &DeviceManagerMakePtr::GetInstance(std::sh
}
const StatusCode DeviceManagerMakePtr::CreateDeviceManager(std::shared_ptr<IDeviceManager> &impl)
{
auto tmp = std::make_shared<IDeviceManager>();
auto tmp = std::make_shared<DeviceManager>();
impl = tmp;
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -52,14 +52,14 @@ StatusCode LedManager::GetLedState(LedState &CurrentState)
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode LedManager::BlinkOn(LedState CurrentState, unsigned int KeepAliveTime)
{
mKeepAliveTime = KeepAliveTime;
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);
}
// StatusCode LedManager::BlinkOff(void)
// {
// mCurrentState = LedState::LED_STATE_OFF;
// return CreateStatusCode(STATUS_CODE_OK);
// }

View File

@ -44,8 +44,8 @@ public:
StatusCode SetLedState(LedState &CurrentState, const unsigned int &KeepAliveTime = DEFAULT_KEEP_ALIVE_TIME,
const unsigned int &BlinkPeriod = LED_NOT_BLINK);
StatusCode GetLedState(LedState &CurrentState);
StatusCode BlinkOn(LedState CurrentState, unsigned int KeepAliveTime);
StatusCode BlinkOff(void);
// StatusCode BlinkOn(LedState CurrentState, unsigned int KeepAliveTime);
// StatusCode BlinkOff(void);
private:
std::shared_ptr<VLedHal> mLedHal;

View File

@ -0,0 +1,76 @@
/*
* 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 "LedTimer.h"
// #include "Log.h"
LedTimer::LedTimer() { mTimerRuning = false; }
std::shared_ptr<LedTimer> &LedTimer::GetInstance(std::shared_ptr<LedTimer> *impl)
{
static auto instance = std::make_shared<LedTimer>();
// if (impl)
// {
// instance = *impl;
// }
return instance;
}
// void LedTimer::Init(std::vector<std::shared_ptr<LedManager>> &LedManagers)
void LedTimer::Init(void)
{
// TimerLedManagers = LedManagers;
StartTimer();
}
void LedTimer::UnInit(void)
{
StopTimer();
TimerLedManagers.clear();
}
void LedTimer::StartTimer(void)
{
auto timerThread = [](std::shared_ptr<LedTimer> timer) { timer->Timer(); };
mTimer = std::thread(timerThread, shared_from_this());
}
void LedTimer::StopTimer(void)
{
mTimerRuning = false;
if (mTimer.joinable()) {
mTimer.join();
}
}
void LedTimer::Timer(void)
{
mTimerRuning = true;
while (mTimerRuning) {
mMutex.lock();
CheckState();
mMutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS));
}
}
const StatusCode LedTimer::AddTimerLedManager(std::shared_ptr<LedManager> &LedManager)
{
TimerLedManagers.push_back(LedManager);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode LedTimer::CheckState(void)
{
int count = 0;
for (auto it = TimerLedManagers.begin(); it != TimerLedManagers.end(); ++it) {
std::shared_ptr<LedManager> LedManager = *it;
LogInfo("%s %d count = %d\n\n", __func__, __LINE__, count++);
}
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -0,0 +1,50 @@
/*
* 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 LEDTIMER_H
#define LEDTIMER_H
#include "LedManager.h"
#include <map>
#include <memory>
#include <mutex>
#include <thread>
constexpr int LED_STATE_CHECK_PERIOD_MS = 100;
class LedTimer : public std::enable_shared_from_this<LedTimer>
{
public:
LedTimer();
~LedTimer() = default;
static std::shared_ptr<LedTimer> &GetInstance(std::shared_ptr<LedTimer> *impl = nullptr);
// void Init(std::vector<std::shared_ptr<LedManager>> &LedManagers);
void Init(void);
void UnInit(void);
void Timer(void);
const StatusCode CheckState(void);
const StatusCode AddTimerLedManager(std::shared_ptr<LedManager> &LedManager);
private:
void StartTimer(void);
void StopTimer(void);
private:
std::vector<std::shared_ptr<LedManager>> TimerLedManagers;
std::mutex mMutex;
bool mTimerRuning;
std::thread mTimer;
};
#endif

View File

@ -2,4 +2,5 @@
# cmake_minimum_required(VERSION 2.8.0)
add_subdirectory(IpcConfig)
add_subdirectory(McuManager)
add_subdirectory(McuAskBase)
add_subdirectory(McuAskBase)
add_subdirectory(LedTest)

View File

@ -0,0 +1,84 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
#
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
#
include_directories(
.
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
# ${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
#
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# , SRC_FILES_MAIN
aux_source_directory(. SRC_FILES_MAIN)
# ./src, SRC_FILES
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME Led_Test)
#
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} DeviceManager gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
Led_Test_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/LedTest
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make Led_Test_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
Led_Test_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/LedTest
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make Led_Test_code_check
COMMAND make Led_Test_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,21 @@
#include "IDeviceManager.h"
#include "ILog.h"
#include <gtest/gtest.h>
namespace Led_Test
{
// ../output_files/test/bin/Led_Test --gtest_filter=Led_Test.Demo
TEST(Led_Test, Demo)
{
// 初始化Log
CreateLogModule();
CreateDeviceManagerModule();
IDeviceManager::GetInstance()->Init();
LedState _Led_State = LED_STATE_RED;
IDeviceManager::GetInstance()->ISetLedState("Led_testing", _Led_State, 10, 0);
IDeviceManager::GetInstance()->UnInit();
}
} /*namespace Led_Test*/

View File

@ -26,11 +26,13 @@ TEST(UartDeviceTest, UNIT_UartDevice_AUTO_IllegalObject)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
IUartOpen(nullptr);
IUartSend(nullptr, nullptr, 0);
IUartRecv(nullptr, nullptr, 0, 0);
IUartTcflush(nullptr);
IUartDeviceFree(nullptr);
char illegalObject[100] = {0};
void *object = &illegalObject[10];
IUartOpen(object);