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