/* * 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 "LedControl.h" #include "ILog.h" #include 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 &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 &state) { LogInfo("Add led state.\n"); DeleteUselessState(); NewLedStateStart(); LedState ledState = LedState::END; state->GetLedState(ledState); SetLedState(ledState); mCurrentState = ledState; if (DO_NOT_KEEP_ALIVE == state->GetKeepAliveTimeMs()) { return; } 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 &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; mStateAliveTime += period; mBlinkPeriod += period; if (TOP_STATE_SHOW >= 0) { // LogInfo("TOP_STATE_SHOW = %d.\n", TOP_STATE_SHOW); std::shared_ptr state = mStates[TOP_STATE_SHOW]; LedState ledState = LedState::END; if (state->GetKeepAliveTimeMs() == DELETED_LED_STATE || (state->GetKeepAliveTimeMs() != KEEP_ALIVE_FOREVER && state->GetKeepAliveTimeMs() <= mStateAliveTime)) { DeleteTopLedState(); return; } state->GetLedState(ledState); if (mCurrentState != ledState && LED_NOT_BLINK == state->GetBlinkTimeMs()) { SetLedState(ledState); mCurrentState = ledState; } else if (mCurrentState == ledState && state->GetBlinkTimeMs() != LED_NOT_BLINK) { BlinkOff(state); } else if (mCurrentState == LedState::OFF && state->GetBlinkTimeMs() != LED_NOT_BLINK) { BlinkOn(state, ledState); } } } void LedControl::NewLedStateStart(void) { mCurrentState = LedState::END; mStateAliveTime = NEW_TOP_LED_STATE; mBlinkPeriod = 0; } void LedControl::DeleteTopLedState(void) { const int TOP_STATE_SHOW = mStates.size() - 1; if (TOP_STATE_SHOW < 0) { return; } LogInfo("Delete led state.\n"); NewLedStateStart(); 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"); std::shared_ptr state = mStates[NEXT_LED_STATE]; LedState ledState = LedState::END; state->GetLedState(ledState); SetLedState(ledState); mCurrentState = ledState; } void LedControl::BlinkOff(std::shared_ptr &state) { if (state->GetBlinkTimeMs() < mBlinkPeriod) { // LogInfo("blink off.\n"); SetLedState(LedState::OFF); mCurrentState = LedState::OFF; mBlinkPeriod = 0; } } void LedControl::BlinkOn(std::shared_ptr &state, const LedState &onState) { if (state->GetBlinkTimeMs() < mBlinkPeriod) { // LogInfo("blink on.\n"); SetLedState(onState); mCurrentState = onState; mBlinkPeriod = 0; } } const char *PrintLedState(const LedState &state) { switch (state) { case LedState::OFF: { return "OFF"; } case LedState::ON: { return "ON"; } case LedState::GREEN: { return "GREEN"; } case LedState::RED: { return "RED"; } case LedState::YELLOW: { return "YELLOW"; } default: { return "Unknown LedState"; } } }