-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProntoHex.cpp
299 lines (261 loc) · 9.03 KB
/
ProntoHex.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "ProntoHex.h"
ProntoHex::ProntoHex(/* args */)
{
}
ProntoHex::ProntoHex(std::string filename)
{
m_filename = filename;
std::ifstream t(filename);
if(!t.is_open())
{
// error! maybe the file doesn't exist.
//printf("%s !is_open()\n", filename.c_str());
m_isCreated = false;
return;
}
std::stringstream buffer;
buffer << t.rdbuf();
m_original_file_contents = buffer.str();
m_cleaned_file_contents = buffer.str();
// Remove all whitespace from the original file contents
m_cleaned_file_contents.erase(std::remove_if(m_cleaned_file_contents.begin(),
m_cleaned_file_contents.end(),
[](unsigned char x){return std::isspace(x);}),
m_cleaned_file_contents.end());
raw_bytes = explode(m_cleaned_file_contents);
m_isCreated = true;
};
ProntoHex::~ProntoHex()
{
}
bool ProntoHex::IsCreated(){
return m_isCreated;
}
std::string ProntoHex::GetFilename(){
return m_filename;
}
std::string ProntoHex::GetOriginalData(){
return m_original_file_contents;
}
std::string ProntoHex::GetCleanedData(){
return m_cleaned_file_contents;
}
std::string ProntoHex::GetPreamble(){
if(raw_bytes.size() < 8){
throw runtime_error("Preamble Invalid, size less than 8");
}
if(raw_bytes[0] != 0 || raw_bytes[1] != 0){
throw runtime_error("Preamble Invalid, first 2 bytes not 0000.");
}
char hexstr[20];
int i;
for (i=0; i<2; i++) {
sprintf(hexstr+i*5, "%02X%02X ", raw_bytes[i*2], raw_bytes[i*2+1]);
}
hexstr[19] = 0;
return string(hexstr);
}
vector<unsigned char> ProntoHex::explode(std::string input)
{
//Expects already filtered and no whitespace string
vector<unsigned char> bytes;
size_t position = 0;
string token;
while (input.size() >= 2 ) {
token = input.substr(0, 2);
unsigned char byte = (unsigned char)strtol(token.c_str(), NULL, 16);
bytes.push_back(byte);
input.erase(0, 2);
}
// unsigned char byte = (unsigned char)strtol(str.c_str(), NULL, 16);
// bytes.push_back(byte);
return bytes;
}
void ProntoHex::PrintHex() {
printf("Raw Hex: \r\n");
for (uint16_t i = 0; i < raw_bytes.size(); i++) {
printf("%02X", raw_bytes[i]);
if ((i+1) % 16 == 0 && i > 0) {
printf("\r\n");
}
else if (i < raw_bytes.size()) {
if(i%2 == 1){
printf(" ");
}
}
}
printf("\n");
}
double ProntoHex::GetIRFrequency(){
if(raw_bytes.size() < 8){
throw runtime_error("Preamble Invalid, size less than 8");
}
uint16_t freq = (raw_bytes[2]<<8) + raw_bytes[3];
if(freq == 0){
throw runtime_error("Preamble Invalid, frequency = 0");
}
return (1000000/(freq * .241246));
}
double ProntoHex::GetIRPulseDuration(){
return (1000000 / GetIRFrequency());
}
bool ProntoHex::GetAssumeSony(){
return m_assumeSony;
}
void ProntoHex::SetAssumeSony(bool assume){
m_assumeSony = assume;
}
int ProntoHex::GetSonyScore(){
uint16_t burst1[] = {0x30,0x18};
uint16_t burst0[] = {0x18,0x18};
uint16_t leadin[] = {0x60,0x18};
uint16_t leadout1[] = {0x18,0x03f6};
uint16_t leadout2[] = {0x30,0x03f6};
int score = 0;
for(int i = 0; i < raw_bytes.size()/4; i++){
uint16_t burstref[2];
burstref[0] = (raw_bytes[i*4]<<8) + raw_bytes[i*4+1];
burstref[1] = (raw_bytes[i*4+2]<<8) + raw_bytes[i*4+3];
// printf("%04X %04X", burstref[0], burstref[1]);
if(burstref[0] == burst1[0] && burstref[1] == burst1[1]){
// printf(" = 1\n");
score += 1;
}else if(burstref[0] == burst0[0] && burstref[1] == burst0[1]){
// printf(" = 0\n");
score += 1;
}else if(burstref[0] == leadin[0] && burstref[1] == leadin[1]){
// printf(" = Lead In\n");
score += 1;
}else if(burstref[0] == leadout1[0] && burstref[1] == leadout1[1]){
// printf(" = Lead Out 1\n");
score += 1;
}else if(burstref[0] == leadout2[0] && burstref[1] == leadout2[1]){
// printf(" = Lead Out 2\n");
score += 1;
}else{
// printf(" ?\n");
}
}
return score;
}
int ProntoHex::GetNECScore(){
uint16_t burst1[] = {0x16,0x60};
uint16_t burst0[] = {0x16,0x16};
uint16_t leadin[] = {0x156,0xab};
uint16_t leadout[] = {0x16,0x0593};
int score = 0;
for(int i = 0; i < raw_bytes.size()/4; i++){
uint16_t burstref[2];
burstref[0] = (raw_bytes[i*4]<<8) + raw_bytes[i*4+1];
burstref[1] = (raw_bytes[i*4+2]<<8) + raw_bytes[i*4+3];
// printf("%04X %04X", burstref[0], burstref[1]);
if(burstref[0] == burst1[0] && burstref[1] == burst1[1]){
// printf(" = 1\n");
score += 1;
}else if(burstref[0] == burst0[0] && burstref[1] == burst0[1]){
// printf(" = 0\n");
score += 1;
}else if(burstref[0] == leadin[0] && burstref[1] == leadin[1]){
// printf(" = Lead In\n");
score += 1;
}else if(burstref[0] == leadout[0] && burstref[1] == leadout[1]){
// printf(" = Lead Out 1\n");
score += 1;
}else{
// printf(" ?\n");
}
}
return score;
}
bool ProntoHex::IsSequencePresent(int sequence){
if(raw_bytes.size() < 8){
throw runtime_error("Preamble Invalid, size less than 8");
}
if(sequence != 1 && sequence != 2){
throw runtime_error("Cannot request sequence " + to_string(sequence));
}
return raw_bytes[2+sequence*2] > 0 || raw_bytes[3+sequence*2] > 0;
}
int ProntoHex::GetSequenceLength(int sequence){
if(raw_bytes.size() < 8){
throw runtime_error("Preamble Invalid, size less than 8");
}
if(sequence != 1 && sequence != 2){
throw runtime_error("Cannot request sequence " + to_string(sequence));
}
uint16_t size = (raw_bytes[2+sequence*2]<<8) + raw_bytes[3+sequence*2];
return size;
}
int ProntoHex::GetSequenceStartIndex(int sequence){
if(raw_bytes.size() < 8){
throw runtime_error("Preamble Invalid, size less than 8");
}
if(sequence != 1 && sequence != 2){
throw runtime_error("Cannot request sequence " + to_string(sequence));
}
if(!IsSequencePresent(sequence)){
throw runtime_error("Sequence " + to_string(sequence) + " is not found in the pronto hex data");
}
int start_index = 8;
if(IsSequencePresent(1) && sequence == 2){
start_index += GetSequenceLength(1)*4;
}
return start_index;
}
vector<unsigned char> ProntoHex::GetSequenceBytes(int sequence){
if(sequence != 1 && sequence != 2){
throw runtime_error("Cannot request sequence " + to_string(sequence));
}
if(!IsSequencePresent(sequence)){
throw runtime_error("Sequence " + to_string(sequence) + " is not found in the pronto hex data");
}
int index = GetSequenceStartIndex(sequence);
int length = GetSequenceLength(sequence);
vector<unsigned char> results;
for(int i = 0; i < length; i++){
results.push_back(raw_bytes[index + i*4]);
results.push_back(raw_bytes[index + i*4 + 1]);
results.push_back(raw_bytes[index + i*4 + 2]);
results.push_back(raw_bytes[index + i*4 + 3]);
}
return results;
}
vector<int> ProntoHex::GetSequenceTiming(int sequence, bool withsigns){
if(sequence != 1 && sequence != 2){
throw runtime_error("Cannot request sequence " + to_string(sequence));
}
if(!IsSequencePresent(sequence)){
throw runtime_error("Sequence " + to_string(sequence) + " is not found in the pronto hex data");
}
vector<int> results;
vector<BurstPair> sequencePairs = GetSequenceBurstPairs(sequence);
for(int j = 0; j < sequencePairs.size(); j++){
// printf("Burst pair %d: %d, %d\n",j, sequencePairs[j].TimeOn, sequencePairs[j].TimeOff);
results.push_back(sequencePairs[j].TimeOn);
results.push_back((withsigns ? -1 : 1) * sequencePairs[j].TimeOff);
}
return results;
}
vector<BurstPair> ProntoHex::GetSequenceBurstPairs(int sequence, bool verboseOutput, int verboseLevel){
if(sequence != 1 && sequence != 2){
throw runtime_error("Cannot request sequence " + to_string(sequence));
}
if(!IsSequencePresent(sequence)){
throw runtime_error("Sequence " + to_string(sequence) + " is not found in the pronto hex data");
}
int index = GetSequenceStartIndex(sequence);
vector<unsigned char> bytes = GetSequenceBytes(sequence);
vector<BurstPair> results;
for(int i = 0; i < bytes.size(); i+= 4){
int onpulses = (bytes[i]<<8) + bytes[i+1];
int onvalue = round(onpulses * GetIRPulseDuration());
int offpulses = (bytes[i+2]<<8) + bytes[i+3];
int offvalue = round(offpulses * GetIRPulseDuration());
if(verboseOutput && verboseLevel < 2){
printf("%02X%02X %02X%02X = %d, %d\n", bytes[i], bytes[i+1], bytes[i+2], bytes[i+3], onpulses, offpulses);
}
BurstPair pair(onvalue, offvalue);
results.push_back(pair);
}
return results;
}