-
Notifications
You must be signed in to change notification settings - Fork 10
/
IRelectra.cpp
172 lines (147 loc) · 3.74 KB
/
IRelectra.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* IRelectra
* Copyrights 2016 Barak Weiss
*
* Many thanks to Chris from AnalysIR
*/
#include "IRelectra.h"
#include <stdint.h>
#include <vector>
using std::vector;
#define UNIT 992
#define NUM_BITS 34
IRelectra::IRelectra(IRsend* remote) : _remote(remote)
{}
// Sends the specified configuration to the IR led
bool IRelectra::sendElectra(bool power, IRElectraMode mode, IRElectraFan fan, int temperature, bool swing, bool sleep)
{
// get the data representing the configuration
uint64_t code = encodeElectra(power, mode, fan, temperature, swing, sleep);
// get the raw data itself with headers, repetition, etc.
std::vector<unsigned int> data = generateSignal(code);
// send using HW.
_remote->sendRaw(data.data(), data.size(), 33);
return true;
}
std::vector<unsigned int> IRelectra::generateSignal(uint64_t code)
{
MarkSpaceArray markspace(UNIT);
// The whole packet looks this:
// 3 Times:
// 3000 usec MARK
// 3000 used SPACE
// Maxchester encoding of the data, clock is ~1000usec
// 4000 usec MARK
for (int k =0; k<3; k++)
{
markspace.addMark(3); //mark
markspace.addSpace(3); //space
markspace.addNumberWithManchesterCode(code, NUM_BITS);
}
markspace.addMark(4);
return markspace.data();
}
#pragma pack(1)
// That configuration has a total of 34 bits
// 33: Power bit, if this bit is ON, the A/C will toggle it's power.
// 32-30: Mode - Cool, heat etc.
// 29-28: Fan - Low, medium etc.
// 27-26: Zeros
// 25: Swing On/Off
// 24-23: Zeros
// 22-19: Temperature, where 15 is 0000, 30 is 1111
// 18: Sleep mode On/Off
// 17- 2: Zeros
// 1: One
// 0: Zero
typedef union ElectraCode {
uint64_t num;
struct {
uint8_t zeros1 : 1;
uint8_t ones1 : 1;
uint16_t zeros2 : 16;
uint8_t sleep : 1;
uint8_t temperature : 4;
uint8_t zeros3 : 2;
uint8_t swing : 1;
uint8_t zeros4 : 2;
uint8_t fan : 2;
uint8_t mode : 3;
uint8_t power : 1;
};
} ElectraUnion;
#pragma pack()
uint64_t IRelectra::encodeElectra(bool power, IRElectraMode mode, IRElectraFan fan, int temperature, bool swing, bool sleep)
{
temperature -= 15;
ElectraCode code = { 0 };
code.ones1 = 1;
code.sleep = sleep ? 1 : 0;
code.temperature = temperature;
code.swing = swing ? 1 : 0;
code.fan = fan;
code.mode = mode;
code.power = power ? 1 : 0;
return code.num;
}
///
/// Mark Space Array
///
MarkSpaceArray::MarkSpaceArray(uint16_t unitLengthInUsec) : _unitLength(unitLengthInUsec)
{ }
void MarkSpaceArray::MarkSpaceArray::addMark(uint16_t units)
{
if (currentState())
{
addUnitsToCurrentState(units);
}
else
{
addUnitsToNextState(units);
}
}
void MarkSpaceArray::addSpace(uint16_t units)
{
if (!currentState())
{
addUnitsToCurrentState(units);
}
else
{
addUnitsToNextState(units);
}}
void MarkSpaceArray::addBitWithManchesterCode(uint8_t bit)
{
if (currentState() == (bit & 1))
{
addUnitsToNextState(1);
}
else
{
addUnitsToCurrentState(1);
}
addUnitsToNextState(1);
}
void MarkSpaceArray::addNumberWithManchesterCode(uint64_t code, uint8_t numberOfBits)
{
for (int j = numberOfBits - 1; j>=0; j--)
{
addBitWithManchesterCode((code >> j) & 1);
}
}
void MarkSpaceArray::addUnitsToCurrentState(uint16_t units)
{
_data.back() += _unitLength * units;
}
void MarkSpaceArray::addUnitsToNextState(uint16_t units)
{
_data.emplace_back(_unitLength * units);
}
const std::vector<unsigned int> MarkSpaceArray::data()
{
return _data;
}
uint8_t MarkSpaceArray::currentState()
{
return _data.size() % 2;
}