hunting/utils/LedControl/include/LedControl.h
2024-06-15 08:36:44 +08:00

94 lines
2.8 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.
*/
#ifndef LED_CONTROL_H
#define LED_CONTROL_H
#include "StatusCode.h"
#include <memory>
#include <vector>
constexpr unsigned int NEW_TOP_LED_STATE = 0;
constexpr unsigned int LED_NOT_BLINK = 0;
constexpr unsigned int BLINKING_FAST_MS = 500;
constexpr unsigned int BLINKING_SLOW_MS = 1000;
constexpr long int KEEP_ALIVE_FOREVER = 0;
constexpr unsigned int DELETED_LED_STATE = -1;
constexpr unsigned int DO_NOT_KEEP_ALIVE = -2;
enum class LedState
{
OFF = 0,
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,
LEVEL_3,
LEVEL_4,
LEVEL_END,
END
};
class VSingleControl
{
public:
VSingleControl() = default;
virtual ~VSingleControl() = default;
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);
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
{
public:
LedControl() = default;
virtual ~LedControl() = default;
void CheckState(const unsigned int &period) override;
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);
void BlinkOn(std::shared_ptr<VSingleControl> &state, const LedState &onState);
private:
std::vector<std::shared_ptr<VSingleControl>> mStates;
LedState mCurrentState;
unsigned int mStateAliveTime;
unsigned int mBlinkPeriod;
};
const char *PrintLedState(const LedState &state);
#endif