-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIRProtocols.hpp
180 lines (151 loc) · 5.12 KB
/
IRProtocols.hpp
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
173
174
175
176
177
178
179
180
#ifndef IRProtocols_hpp
#define IRProtocols_hpp
#include <Arduino.h>
#include "Iterator.hpp"
/**
* Encapsulates protocol timings.
*
* To add a new protocol, add a new entry on enum Id (and on Name member),
* and create it on IRProtocols below
*/
class IRProtocol
{
public:
/**
* Arbitrary ids (and names) for each protocol
*/
enum Id : char
{
Junco = 1,
Yawl,
Draftee,
Ampul,
Marl,
Pomander,
NEC
};
IRProtocol(Id i, uint16_t headerMark, uint16_t headerSpace,
uint16_t bitMark, uint16_t bitZeroSpace, uint16_t bitOneSpace,
uint16_t trailSpace, uint16_t repeatSpace)
{
m_id = i;
m_headerMark = headerMark;
m_headerSpace = headerSpace;
m_bitMark = bitMark;
m_bitZeroSpace = bitZeroSpace;
m_bitOneSpace = bitOneSpace;
m_trailSpace = trailSpace;
m_repeatSpace = repeatSpace;
}
IRProtocol(Id i, uint16_t headerMark, uint16_t headerSpace,
uint16_t bitMark, uint16_t bitZeroSpace, uint16_t bitOneSpace)
: IRProtocol(i, headerMark, headerSpace,
bitMark, bitZeroSpace, bitOneSpace, 0, 0) {};
Id GetId() { return m_id; };
uint16_t HeaderMark() { return m_headerMark; }
uint16_t HeaderSpace() { return m_headerSpace; }
uint16_t BitMark() { return m_bitMark; }
uint16_t BitZeroSpace() { return m_bitZeroSpace; }
uint16_t BitOneSpace() { return m_bitOneSpace; }
uint16_t TrailSpace() { return m_trailSpace; }
uint16_t RepeatSpace() { return m_repeatSpace; }
bool HasTrail() { return m_trailSpace > 0; }
bool IsRepeated() { return m_repeatSpace > 0; }
String Name()
{
switch(m_id)
{
case Junco: return String(F("Junco"));
case Yawl: return String(F("Yawl"));
case Draftee: return String(F("Draftee"));
case Ampul: return String(F("Ampul"));
case Marl: return String(F("Marl"));
case Pomander: return String(F("Pomander"));
case NEC: return String(F("NEC"));
default: return String(F("Unknown"));
}
}
private:
Id m_id;
uint16_t m_headerMark;
uint16_t m_headerSpace;
uint16_t m_bitMark;
uint16_t m_bitZeroSpace;
uint16_t m_bitOneSpace;
uint16_t m_trailSpace;
uint16_t m_repeatSpace;
};
/**
* Collection of protocols to encode and decode IRData.
*
* Add a new protocol on class constructor
*/
class IRProtocols : public Iterator<IRProtocol *>
{
private:
IRProtocol *m_protocols[10];
public:
IRProtocols()
{
m_count = 0; // from Iterator
m_protocols[m_count++] = new IRProtocol(
IRProtocol::Junco, 9000, 4500, 550, 550, 1650
);
m_protocols[m_count++] = new IRProtocol(
IRProtocol::Yawl, 3350, 1700, 350, 450, 1300
);
m_protocols[m_count++] = new IRProtocol(
IRProtocol::Draftee, 6000, 7400, 450, 650, 1700, 7400, 0
);
m_protocols[m_count++] = new IRProtocol(
IRProtocol::Ampul, 4350, 4450, 450, 600, 1700, 0, 5450
);
m_protocols[m_count++] = new IRProtocol(
IRProtocol::Marl, 3100, 8900, 450, 550, 1600
);
m_protocols[m_count++] = new IRProtocol(
IRProtocol::Pomander, 8850, 4500, 500, 700, 1650
);
m_protocols[m_count++] = new IRProtocol(
IRProtocol::NEC, 4400, 4400, 500, 600, 1700
);
}
void First() // from Iterator
{
if(m_count == 0) return;
m_current = m_protocols[0];
m_iteratorIndex = 0;
}
void Next() // from Iterator
{
if(IsDone()) return;
m_iteratorIndex++;
if(IsDone()) return;
m_current = m_protocols[m_iteratorIndex];
}
using Iterator<IRProtocol *>::Count;
using Iterator<IRProtocol *>::IsDone;
using Iterator<IRProtocol *>::Current;
/**
* Find protocol by its id.
*
* @param id to look for
*
* @return null if not found
*/
IRProtocol * GetProtocol(IRProtocol::Id id)
{
First();
while(!IsDone())
{
if(Current()->GetId() == id) return Current();
Next();
}
return NULL;
}
};
/**
* Global instance of IRProtocols
*/
IRProtocols g_irProtocols;
#endif