hunting/utils/LedControl/include/LedControl.h
2024-02-21 02:06:27 -08:00

83 lines
2.7 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,
YELLOW,
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) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual unsigned int GetKeepAliveTimeMs(void) { return KEEP_ALIVE_FOREVER; }
virtual unsigned int GetBlinkTimeMs(void) { return LED_NOT_BLINK; }
};
class VLedControl
{
public:
VLedControl() = default;
virtual ~VLedControl() = default;
virtual StatusCode SetLedState(const LedState &state) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual void AddLedState(std::shared_ptr<VSingleControl> &control) {}
virtual void CheckState(const unsigned int &period) {}
// virtual void SetHalLedState(const VirtualLedState &state) {}
virtual std::string GetLedName(void) { return "undefine"; }
};
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 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