forked from crankyoldgit/IRremoteESP8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRsend_test.h
155 lines (135 loc) · 3.97 KB
/
IRsend_test.h
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
// Copyright 2017 David Conran
#ifndef TEST_IRSEND_TEST_H_
#define TEST_IRSEND_TEST_H_
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <iostream>
#include <sstream>
#include <string>
#include "IRrecv.h"
#include "IRsend.h"
#include "IRtimer.h"
#define OUTPUT_BUF 10000U
#define RAW_BUF 10000U
#ifdef UNIT_TEST
// Used to help simulate elapsed time in unit tests.
extern uint32_t _IRtimer_unittest_now;
#endif // UNIT_TEST
class IRsendTest : public IRsend {
public:
uint32_t output[OUTPUT_BUF];
uint32_t freq[OUTPUT_BUF];
uint8_t duty[OUTPUT_BUF];
uint16_t last;
uint16_t rawbuf[RAW_BUF];
decode_results capture;
explicit IRsendTest(uint16_t x, bool i = false, bool j = true)
: IRsend(x, i, j) {
reset();
}
void reset() {
last = 0;
for (uint16_t i = 0; i < OUTPUT_BUF; i++) output[i] = 0;
for (uint16_t i = 0; i < RAW_BUF; i++) rawbuf[i] = 0;
}
std::string outputStr() {
std::stringstream result;
uint8_t lastduty = UINT8_MAX; // An impossible duty cycle value.
uint32_t lastfreq = 0; // An impossible frequency value.
if (last == 0 && output[0] == 0) return "";
for (uint16_t i = 0; i <= last; i++) {
// Display the frequency only if it changes.
if (freq[i] != lastfreq) {
result << "f";
result << freq[i];
lastfreq = freq[i];
}
// Display the duty cycle only if it changes.
if (duty[i] != lastduty) {
result << "d";
result << static_cast<uint16_t>(duty[i]);
lastduty = duty[i];
}
if ((i & 1) != outputOff) // Odd XOR outputOff
result << "s";
else
result << "m";
result << output[i];
}
reset();
return result.str();
}
void makeDecodeResult(uint16_t offset = 0) {
capture.decode_type = UNKNOWN;
capture.bits = 0;
capture.rawlen = last + 2 - offset;
capture.overflow = (last - offset >= (int16_t)RAW_BUF);
capture.repeat = false;
capture.address = 0;
capture.command = 0;
capture.value = 0;
capture.rawbuf = rawbuf;
for (uint16_t i = 0; (i < RAW_BUF - 1) && (offset < OUTPUT_BUF);
i++, offset++)
if (output[offset] / kRawTick > UINT16_MAX)
rawbuf[i + 1] = UINT16_MAX;
else
rawbuf[i + 1] = output[offset] / kRawTick;
}
void dumpRawResult() {
std::cout << std::dec;
if (capture.rawlen == 0) return;
std::cout << "uint16_t rawbuf[" << capture.rawlen - 1 << "] = {";
for (uint16_t i = 1; i < capture.rawlen; i++) {
if (i % 8 == 1) std::cout << std::endl << " ";
std::cout << (capture.rawbuf[i] * kRawTick);
// std::cout << "(" << capture.rawbuf[i] << ")";
if (i < capture.rawlen - 1) std::cout << ", ";
}
std::cout << "};" << std::endl;
}
void addGap(uint32_t usecs) { space(usecs); }
uint16_t mark(uint16_t usec) {
IRtimer::add(usec);
if (last >= OUTPUT_BUF) return 0;
if (last & 1) // Is odd? (i.e. last call was a space())
output[++last] = usec;
else
output[last] += usec;
duty[last] = _dutycycle;
freq[last] = _freq_unittest;
return 0;
}
void space(uint32_t time) {
IRtimer::add(time);
if (last >= OUTPUT_BUF) return;
if (last & 1) { // Is odd? (i.e. last call was a space())
output[last] += time;
} else {
output[++last] = time;
}
duty[last] = _dutycycle;
freq[last] = _freq_unittest;
}
};
#ifdef UNIT_TEST
class IRsendLowLevelTest : public IRsend {
public:
std::string low_level_sequence;
explicit IRsendLowLevelTest(uint16_t x, bool i = false, bool j = true)
: IRsend(x, i, j) {
reset();
}
void reset() { low_level_sequence = ""; }
protected:
void _delayMicroseconds(uint32_t usec) {
_IRtimer_unittest_now += usec;
std::ostringstream Convert;
Convert << usec;
low_level_sequence += Convert.str() + "usecs";
}
void ledOff() { low_level_sequence += "[Off]"; }
void ledOn() { low_level_sequence += "[On]"; }
};
#endif // UNIT_TEST
#endif // TEST_IRSEND_TEST_H_