Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
Rc5Decoder.cpp
Go to the documentation of this file.
1#include "Rc5Decoder.h"
2#include <string.h>
3
4Rc5Decoder::Length Rc5Decoder::decodeDuration(microseconds_t t) {
5 Length len = (t < timebaseLower) ? invalid
6 : (t <= timebaseUpper) ? half
7 : (t >= 2*timebaseLower && t <= 2*timebaseUpper) ? full
8 : invalid;
9 return len;
10}
11
12unsigned int Rc5Decoder::decodeFlashGap(microseconds_t flash, microseconds_t gap) {
13 bool result = getDuration(flash, 1);
14 if (!result)
15 return invalid;
16
17 return getDuration(gap, 3) ? 1
18 : getDuration(gap, 1) ? 0
19 : invalid;
20}
21
22bool Rc5Decoder::tryDecode(const IrReader& irCapturer, Stream& stream) {
23 Rc5Decoder decoder(irCapturer);
24 return decoder.printDecode(stream);
25}
26
27Rc5Decoder::Rc5Decoder(const IrReader& irCapturer) {
28 unsigned int index = 0U;
29 unsigned int sum = 0U;
30 int doublet = -1;
31 decode[0] = '\0';
32
33 while (doublet < 25) {
34 Length length = decodeDuration(irCapturer.getDuration(index++));
35 if (length == invalid)
36 return;
37 doublet += (int) length;
38 if (doublet % 2 == 1)
39 sum = (sum << 1U) + (index & 1U);
40 }
41 sum = ~sum & 0x1FFFU;
42
43 bool success = isEnding(irCapturer.getDuration(irCapturer.getDataLength()-1));
44 if (!success)
45 return;
46
47 F = (sum & 0x3FU) | ((~sum & 0x1000U) >> 6U);
48 D = (sum & 0x7C0U) >> 6U;
49 T = (sum & 0x0800U) >> 11U;
50
51 setValid(true);
52 sprintf(decode, format, D, F, T);
53}
54
55const char *Rc5Decoder::getDecode() const {
56 return decode;
57}
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
void setValid(bool valid_)
Definition: IrDecoder.h:60
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 RC5 signals.
Definition: Rc5Decoder.h:9
static bool tryDecode(const IrReader &irReader, Stream &stream)
Convenience function; constructs an Rc5Decoder and calls its printDecode.
Definition: Rc5Decoder.cpp:22
Rc5Decoder(const IrReader &irReader)
Constructs a Rc5Decoder from an IrReader, containing data.
Definition: Rc5Decoder.cpp:27
const char * getDecode() const
Returns a textual description the decode for human consumption.
Definition: Rc5Decoder.cpp:55