-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitcher.cpp
More file actions
108 lines (93 loc) · 3.48 KB
/
Copy pathswitcher.cpp
File metadata and controls
108 lines (93 loc) · 3.48 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
105
106
107
108
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2021 Dimitry Ishenko
// Contact: dimitry (dot) ishenko (at) (gee) mail (dot) com
//
// Distributed under the GNU GPL license. See the LICENSE.md file for details.
////////////////////////////////////////////////////////////////////////////////
#include "switcher.hpp"
#include <utility> // std::swap
////////////////////////////////////////////////////////////////////////////////
namespace pbus
{
////////////////////////////////////////////////////////////////////////////////
switcher::switcher(serial_port& port) :
port_{ &port }
{
ci_ = port_->add_recv_callback([&](std::string data)
{
if(data.size()) switch(data[0])
{
case pbus::query: // query response
if(qcb_ && data.size() -1 <= 1 + max_data_len)
{
data.erase(0, 1);
qcb_(data);
}
break;
case pbus::write: // write response
if(wcb_ && data.size() >= 6 && data.size() <= 6 + max_data_len)
{
auto id = to_num(data.substr(1, 2));
auto reg = to_num(data.substr(3, 3));
data.erase(0, 6);
if(id <= max_id && reg <= max_reg) wcb_(id, reg, data);
}
break;
}
});
}
////////////////////////////////////////////////////////////////////////////////
switcher::~switcher()
{
if(port_) port_->remove_recv_callback(ci_);
}
////////////////////////////////////////////////////////////////////////////////
switcher& switcher::operator=(switcher&& rhs)
{
std::swap(port_, rhs.port_);
std::swap(ci_ , rhs.ci_ );
std::swap(qcb_ , rhs.qcb_ );
std::swap(wcb_ , rhs.wcb_ );
return *this;
}
////////////////////////////////////////////////////////////////////////////////
void switcher::learn(const pbus::map& map, num reg)
{
throw_if_reg_out_of_range(reg, "switcher::learn()");
port_->send(pbus::learn + to_hex(map) + to_hex(reg, 3));
}
////////////////////////////////////////////////////////////////////////////////
void switcher::recall(const pbus::map& map, num reg)
{
throw_if_reg_out_of_range(reg, "switcher::recall()");
port_->send(pbus::recall + to_hex(map) + to_hex(reg, 3));
}
////////////////////////////////////////////////////////////////////////////////
void switcher::trigger(const pbus::map& map, num fn)
{
throw_if_fn_out_of_range(fn, "switcher::trigger()");
port_->send(pbus::trigger + to_hex(map) + to_hex(fn, 1));
}
////////////////////////////////////////////////////////////////////////////////
void switcher::query(num id)
{
throw_if_id_out_of_range(id, "switcher::query()");
port_->send(pbus::query + to_hex(id, 2));
}
////////////////////////////////////////////////////////////////////////////////
void switcher::read(num id, num reg)
{
throw_if_id_out_of_range(id, "switcher::read()");
throw_if_reg_out_of_range(reg, "switcher::read()");
port_->send(pbus::read + to_hex(id, 2) + to_hex(reg, 3));
}
////////////////////////////////////////////////////////////////////////////////
void switcher::write(num id, num reg, const std::string& data)
{
throw_if_id_out_of_range(id, "switcher::write()");
throw_if_reg_out_of_range(reg, "switcher::write()");
throw_if_data_out_of_range(data, "switcher::write()");
port_->send(pbus::write + to_hex(id, 2) + to_hex(reg, 3) + data);
}
////////////////////////////////////////////////////////////////////////////////
}