Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
Nec1Decoder.cpp
Go to the documentation of this file.
1#include "Nec1Decoder.h"
2#include "IrReader.h"
3#include <string.h>
4#include <stdlib.h>
5
6//{38.4k,564}<1,-1|1,-3>(16,-8,D:8,S:8,F:8,~F:8,1,^108m,(16,-4,1,^108m)*) [D:0..255,S:0..255=255-D,F:0..255]
7
8int Nec1Decoder::decodeFlashGap(microseconds_t flash, microseconds_t gap) {
9 bool result = getDuration(flash, 1);
10 if (!result)
11 return invalid;
12
13 return getDuration(gap, 3) ? 1
14 : getDuration(gap, 1) ? 0
15 : invalid;
16}
17
18bool Nec1Decoder::tryDecode(const IrReader& irReader, Stream& stream) {
19 Nec1Decoder decoder(irReader);
20 return decoder.printDecode(stream);
21}
22
23int Nec1Decoder::decodeParameter(const IrReader& irReader, unsigned int index) {
24 unsigned int sum = 0;
25 for (int i = 7; i >= 0; i--) {
26 int result = decodeFlashGap(irReader.getDuration(2 * i + index), irReader.getDuration(2 * i + 1 + index));
27 if (result == invalid)
28 return invalid;
29 sum = (sum << 1) + result;
30 }
31 return sum;
32}
33
35 unsigned int index = 0;
36 bool success;
37 if (irReader.getDataLength() == 4U) {
38 success = getDuration(irReader.getDuration(index++), 16U);
39 if (!success)
40 return;
41 success = getDuration(irReader.getDuration(index++), 4U);
42 if (!success)
43 return;
44 success = getDuration(irReader.getDuration(index++), 1U);
45 if (!success)
46 return;
47 success = isEnding(irReader.getDuration(index));
48 if (!success)
49 return;
50 ditto = true;
51 setValid(true);
52 //strcpy_PF(decode, (uint_farptr_t) nec1DittoLiteral); // FIXME
53 strcpy(decode, nec1DittoLiteral); // FIXME
54 } else if (irReader.getDataLength() == 34U * 2U) {
55 success = getDuration(irReader.getDuration(index++), 16U);
56 if (!success)
57 return;
58 success = getDuration(irReader.getDuration(index++), 8U);
59 if (!success)
60 return;
61 D = decodeParameter(irReader, index);
62 if (D == invalid)
63 return;
64 index += 16;
65 S = decodeParameter(irReader, index);
66 if (S == invalid)
67 return;
68 index += 16;
69 F = decodeParameter(irReader, index);
70 if (F == invalid)
71 return;
72 index += 16;
73 int invF = decodeParameter(irReader, index);
74 if (invF < 0)
75 return;
76 if ((F ^ invF) != 0xFF)
77 return;
78 index += 16;
79
80 success = getDuration(irReader.getDuration(index++), 1U);
81 if (!success)
82 return;
83 success = isEnding(irReader.getDuration(index));
84 if (!success)
85 return;
86 ditto = false;
87 setValid(true);
88 //strncpy_PF(decode, pgm_read_byte(nec1DittoLiteral), 4);
89 //strcpy_PF(decode, (uint_farptr_t) F("NEC1"));
90 strcpy(decode, "NEC1");
91 char junk[5];
92 sprintf(junk, " %d", D);
93 strcat(decode, junk);
94 if (S != 255 - D) {
95 sprintf(junk, " %d", S);
96 strcat(decode, junk);
97 }
98 sprintf(junk, " %d", F);
99 strcat(decode, junk);
100 }
101}
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
Abstract base class for all decoder classes.
Definition: IrDecoder.h:8
void setValid(bool valid_)
Definition: IrDecoder.h:60
static constexpr int invalid
Definition: IrDecoder.h:59
static bool isEnding(microseconds_t duration)
Tests if the argument is large enough to be considered an ending of a decodable signal.
Definition: IrDecoder.h:69
bool printDecode(Stream &stream) const
If valid, prints the decode to the stream.
Definition: IrDecoder.h:48
Abstract base class for all IR readers, capturing or receiving.
Definition: IrReader.h:30
virtual size_t getDataLength() const =0
Returns the number of collected durations.
virtual microseconds_t getDuration(unsigned int index) const =0
Returns the index-th duration, if possible.
A decoder class for NEC1 signals.
Definition: Nec1Decoder.h:9
static bool tryDecode(const IrReader &irReader, Stream &stream)
Convenience function; constructs an Nec1Decoder and calls its printDecode.
Definition: Nec1Decoder.cpp:18