Key.hpp
1 //
2 // Created by xiong on 2022/7/5.
3 //
4 
5 #ifndef EMX_Key_HPP
6 #define EMX_Key_HPP
7 
8 #include "EmxCore.hpp"
9 #include "Bsp.hpp"
10 
11 namespace Emx {
16  class Key {
17  public:
18  static const int DetectInterval = 50; //50ms
19  enum class EventE : uint8_t {
20  Click,
21  LongPress,
22  };
23 
24  using OnKeyEvent = std::function<void(const char *name, EventE e)>;
25 
26  Key(const char *name, EuvLoop &loop, Bsp::GpioLike *key, int longPressTimeMs) :
27  m_name(name), m_loop(loop), m_key(key), m_longPressTime(longPressTimeMs),
28  m_pressTime(0) {}
29 
30  virtual ~Key() {}
31 
32  void Create(OnKeyEvent e);
33 
34  void Destroy();
35 
36  private:
37  void OnTimer();
38 
39  private:
40  std::string m_name;
41  EuvLoop m_loop;
42  Bsp::GpioLike *m_key;
43  int m_longPressTime;
44  OnKeyEvent m_cb;
45  EuvTimer m_timer;
46  int m_pressTime;
47  };
48 
50 }
51 
52 #endif //EMX_Key_HPP
Definition: Bsp.hpp:21
实现多路复用循环的主体,所有基于EuvLoop的事件都应绑定到一个EuvLoop上
Definition: EuvLoop.hpp:18
基于EuvLoop的定时器,很有用的一个东西
Definition: EuvTimer.hpp:16
Definition: Key.hpp:16
void Create(OnKeyEvent e)
static const int DetectInterval
Definition: Key.hpp:18
virtual ~Key()
Definition: Key.hpp:30
std::function< void(const char *name, EventE e)> OnKeyEvent
Definition: Key.hpp:24
EventE
Definition: Key.hpp:19
Key(const char *name, EuvLoop &loop, Bsp::GpioLike *key, int longPressTimeMs)
Definition: Key.hpp:26
void Destroy()
Definition: EmxGpio.hpp:10