-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdebounce.hpp
More file actions
104 lines (82 loc) · 2.89 KB
/
debounce.hpp
File metadata and controls
104 lines (82 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Dimitry Ishenko
// Contact: dimitry (dot) ishenko (at) (gee) mail (dot) com
//
// Distributed under the GNU GPL license. See the LICENSE.md file for details.
////////////////////////////////////////////////////////////////////////////////
#ifndef FIRMATA_DEBOUNCE_HPP
#define FIRMATA_DEBOUNCE_HPP
////////////////////////////////////////////////////////////////////////////////
#include "firmata/pin.hpp"
#include "firmata/types.hpp"
#include "asio_or_boost.hpp"
#include <chrono>
#include <map>
#include <memory>
#include <utility>
////////////////////////////////////////////////////////////////////////////////
namespace firmata
{
////////////////////////////////////////////////////////////////////////////////
// Pin debouncer
//
class debounce
{
public:
////////////////////
debounce() noexcept = default;
template<typename Rep, typename Period>
debounce(asio::io_service& io, const std::chrono::duration<Rep, Period>& time) :
debounce(io, std::chrono::duration_cast<msec>(time))
{ }
explicit debounce(asio::io_service& io, const msec& time = msec(5)) :
io_(&io), time_(time)
{ }
debounce(const debounce&) = delete;
debounce(debounce&& rhs) noexcept { swap(rhs); }
debounce& operator=(const debounce&) = delete;
debounce& operator=(debounce&& rhs) noexcept { swap(rhs); return *this; }
void swap(debounce& rhs) noexcept
{
using std::swap;
swap(io_ , rhs.io_ );
swap(time_ , rhs.time_ );
swap(chain_, rhs.chain_);
swap(id_ , rhs.id_ );
}
////////////////////
bool valid() const noexcept { return io_; }
explicit operator bool() const noexcept { return valid(); }
////////////////////
// install state changed/low/high callback
cid on_state_changed(pin&, pin::int_call);
cid on_state_low(pin&, pin::void_call);
cid on_state_high(pin&, pin::void_call);
// remove callback
bool remove_call(cid id) { return chain_.erase(id); }
private:
////////////////////
asio::io_service* io_ = nullptr;
msec time_;
struct bounce
{
////////////////////
bounce(asio::io_service&, const msec&, pin&, pin::int_call);
~bounce() noexcept;
private:
////////////////////
pin& pin_; int state_; cid id_;
msec time_;
asio::system_timer timer_;
pin::int_call fn_;
void pin_state_changed(int);
};
std::map<cid, std::unique_ptr<bounce>> chain_;
int id_ = 0;
};
////////////////////////////////////////////////////////////////////////////////
inline void swap(debounce& lhs, debounce& rhs) noexcept { lhs.swap(rhs); }
////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////
#endif