hunting/middleware/DeviceManager/src/LedTimer.cpp

77 lines
2.2 KiB
C++

/*
* 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);
}