-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlircProntoCodes.cpp
More file actions
137 lines (117 loc) · 3.56 KB
/
FlircProntoCodes.cpp
File metadata and controls
137 lines (117 loc) · 3.56 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
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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <iomanip>
using namespace std;
long const RawDataCode = 0x0000;
long const CarrierFrequency = 0x006d;
long const Burst1PairCount = 0x0000;
void PrintWord(long word) { cout << hex << setfill('0') << setw(4) << word << " "; }
void PushBits(long n, vector<char> & bits)
{
bits.push_back((n & 0x8) >> 3);
bits.push_back((n & 0x4) >> 2);
bits.push_back((n & 0x2) >> 1);
bits.push_back((n & 0x1));
}
int main(int argc, const char * argv[])
{
int commandCodeRadix = 16;
if (argc < 3 || argc > 4)
{
cerr
<< "Usage: FlircProntoCodes <DeviceCode> <CommandCode>\n"
<< " DeviceCode: Hexidecmal code for the Flirc device\n"
<< " CommandCode: Hexidecmal code for the Flirc command\n\n"
<< " Generates the pronto hex codes to send an IR command to the\n"
<< " Flirc TV USB receiver.\n";
return 1;
}
if (argc == 4 && string(argv[3]) == "dec")
{
commandCodeRadix = 10;
}
else
{
cerr
<< "Invalid radix argument: " << argv[3] << "\n";
return 2;
}
long deviceCode = strtol(argv[1], nullptr, 16);
long commandCode = strtol(argv[2], nullptr, commandCodeRadix);
if (deviceCode < 0 || deviceCode > 0xf)
{
cerr << "Invalid device code: " << argv[2];
return 3;
}
if (commandCode < 0 || commandCode > 0xff)
{
cerr << "Invalid command code: " << argv[1];
return 4;
}
// Positive values indicate number of frames LED is on, negative values
// indicate the number of frames the LED is off.
// Adding the fixed header for each Flirc IR packet.
vector<long> edges = {
+0xba, -0xba,
+0x20, -0x40,
+0x20, -0x40,
+0x20, -0x40,
+0x20, -0x20,
+0x20, -0x40,
+0x20, -0x40,
+0x20, -0x40,
+0x20, -0x40,
-0xba,
};
// convert the deviceCode and commandCode to a stream of nibbles
vector<long> nibbles;
nibbles.push_back(deviceCode);
nibbles.push_back(commandCode >> 4);
nibbles.push_back(commandCode & 0x0f);
// convert our nibbles to a stream of bits
vector<char> bits;
for (long n : nibbles) PushBits(n, bits);
// add bits to the edge data
for (char bit : bits)
{
edges.push_back(bit == 1 ? -0x20 : +0x20);
edges.push_back(bit == 1 ? +0x20 : -0x20);
}
// convert to pairs of +/- values
vector<long> edgePairs;
for (long e : edges)
{
if (edgePairs.empty())
{
edgePairs.push_back(e);
}
else
{
if (edgePairs.back() > 0 && e > 0)
edgePairs.back() += e;
else if (edgePairs.back() < 0 && e < 0)
edgePairs.back() += e;
else
edgePairs.push_back(e);
}
}
// If last edge is high we will need to add an low edge.
if (edgePairs.back() > 0) edgePairs.push_back(-1);
// convert paired edge data to Pronto stye On/Off burst pairs.
vector<long> burst2Pairs;
for (long e : edgePairs)
{
burst2Pairs.push_back( e < 0 ? -e : e);
}
cout
<< "[0x" << hex << setfill('0') << setw(2) << deviceCode << ", "
<< "0x" << hex << setfill('0') << setw(2) << commandCode << "] ";
PrintWord(RawDataCode);
PrintWord(CarrierFrequency);
PrintWord(Burst1PairCount);
PrintWord(burst2Pairs.size() / 2);
for (long w : burst2Pairs) PrintWord(w);
cout << "\n";
return 0;
}