-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cpp
More file actions
95 lines (82 loc) · 2.42 KB
/
Command.cpp
File metadata and controls
95 lines (82 loc) · 2.42 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
#include "Command.hpp"
#include "PT22x2.hpp"
#include <exception>
using std::runtime_error;
Command::Command(PT22x2::Codeword codeword) :
system_code(0), channel(0), state(State::Off)
{
for (int i=0; i<5; i++) {
if (codeword[i] == PT22x2::CodeBit::Zero) {
system_code |= 1 << i;
}
else if (codeword[i] == PT22x2::CodeBit::One) {
throw runtime_error("malformed command");
}
}
for (int i=0; i<4; i++) {
if (codeword[i+5] == PT22x2::CodeBit::Zero) {
channel |= 1 << i;
}
else if (codeword[i+5] == PT22x2::CodeBit::One) {
throw runtime_error("malformed command");
}
}
if (channel == 0) {
throw runtime_error("malformed command");
}
if (codeword[10] == PT22x2::CodeBit::Floating && codeword[11] == PT22x2::CodeBit::Zero) {
state = State::Off;
}
else if (codeword[10] == PT22x2::CodeBit::Zero && codeword[11] == PT22x2::CodeBit::Floating) {
state = State::On;
}
else {
throw runtime_error("malformed command");
}
}
Command::Command(unsigned int system_code, unsigned int channel, State state) :
system_code(system_code), channel(channel), state(state)
{}
PT22x2::Codeword Command::getCodeword() {
PT22x2::Codeword codeword;
for (int i=0; i<5; i++) {
if ((system_code >> i) & 1) {
codeword[i] = PT22x2::CodeBit::Zero;
}
else {
codeword[i] = PT22x2::CodeBit::Floating;
}
}
for (int i=0; i<4; i++) {
if ((channel >> i) & 1) {
codeword[i+5] = PT22x2::CodeBit::Zero;
}
else {
codeword[i+5] = PT22x2::CodeBit::Floating;
}
}
codeword[9] = PT22x2::CodeBit::Floating;
if (state == State::Off) {
codeword[10] = PT22x2::CodeBit::Floating;
codeword[11] = PT22x2::CodeBit::Zero;
}
else {
codeword[10] = PT22x2::CodeBit::Zero;
codeword[11] = PT22x2::CodeBit::Floating;
}
return codeword;
}
std::ostream& operator<<(std::ostream &strm, const Command &cmd) {
strm << "<Command: system_code=" << cmd.system_code << ", "
<< "channel=" << cmd.channel << ", state=";
switch (cmd.state) {
case Command::State::Off:
strm << "off";
break;
case Command::State::On:
strm << "on";
break;
}
strm << ">";
return strm;
}