83 lines
2.6 KiB
C++
83 lines
2.6 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 KEY_CONTROL_H
|
|
#define KEY_CONTROL_H
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
constexpr long int KEY_DO_NOT_HOLD_PRESSING = -1;
|
|
constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100;
|
|
constexpr int KEY_ACTION_LONG_CLICK = 1000 * 5;
|
|
constexpr int KEY_ACTION_SHORT_CLICK = 200;
|
|
constexpr int KEY_ACTION_HOLD_DWON = 500;
|
|
constexpr long int KEY_NOT_PRESSING = -1;
|
|
enum class KeyHalEvent
|
|
{
|
|
PRESSING = 0,
|
|
NOT_PRESSING,
|
|
END
|
|
};
|
|
enum class KeyEvent
|
|
{
|
|
SHORT_CLICK = 0,
|
|
HOLD_DOWN,
|
|
HOLD_UP,
|
|
END
|
|
};
|
|
class VKeyHal
|
|
{
|
|
public:
|
|
VKeyHal() = default;
|
|
virtual ~VKeyHal() = default;
|
|
virtual void KeyEventTrigger(const KeyHalEvent &event) {}
|
|
virtual void TimerKeyEventTrigger(const KeyHalEvent &event) {}
|
|
virtual long int GetHoldPressingTimeMs(void) { return KEY_DO_NOT_HOLD_PRESSING; }
|
|
};
|
|
class VKeyControl
|
|
{
|
|
public:
|
|
VKeyControl() = default;
|
|
virtual ~VKeyControl() = default;
|
|
// virtual void SetKeyHalOwner(std::shared_ptr<VKeyHal> owner) {}
|
|
virtual const std::string GetKeyName(void) { return "undefine"; }
|
|
};
|
|
using KeyActionReport = std::function<void(const std::string &, const KeyEvent &, const unsigned int &)>;
|
|
class KeyControl : public VKeyControl, public VKeyHal, public std::enable_shared_from_this<KeyControl>
|
|
{
|
|
public:
|
|
KeyControl();
|
|
KeyControl(std::shared_ptr<VKeyControl> &keyHal, const KeyActionReport &keyAction,
|
|
const long int &longClickTime = KEY_ACTION_LONG_CLICK);
|
|
~KeyControl();
|
|
void KeyEventTrigger(const KeyHalEvent &event) override;
|
|
void TimerKeyEventTrigger(const KeyHalEvent &event) override;
|
|
long int GetHoldPressingTimeMs(void) override;
|
|
void Init(void);
|
|
void UnInit(void);
|
|
void ActionReport(const std::string &key, const KeyHalEvent &keyEvent);
|
|
|
|
private:
|
|
void KeyPressingTrigger(const std::string &key);
|
|
void KeyNotPressingTrigger(const std::string &key);
|
|
bool IsKeyPressing(void);
|
|
|
|
private:
|
|
std::mutex mMutex;
|
|
// std::shared_ptr<VKeyControl> mKeyHal;
|
|
KeyActionReport mKeyActionReport;
|
|
long int mPressingTime;
|
|
long int mLongClickTime;
|
|
};
|
|
#endif |