Infrared4Arduino
Rc5Renderer.cpp
Go to the documentation of this file.
1 #include "Rc5Renderer.h"
2 
3 // NOTE: writing intro[i++] = ... produces wrong result, compiler bug?
4 // (Adding a print statement immediately after, and it works :-~)
5 // So let's write intro[i] = ...; i++ at least for now.
6 
7 uint8_t Rc5Renderer::T = 1;
8 
9 const IrSignal *Rc5Renderer::newIrSignal(unsigned int D, unsigned int F) {
10  T = ! T;
11  return newIrSignal(D, F, T);
12 }
13 
15 
16 const IrSignal *Rc5Renderer::newIrSignal(unsigned int D, unsigned int F, unsigned int T) {
17  unsigned int index = 0U;
18  int pending = 0;
19  microseconds_t *repeat = new microseconds_t[28];
20  emit(1U, index, pending, repeat);
21  emit(((~F) & 0x40U) >> 6U, index, pending, repeat);
22  emit(T & 1U, index, pending, repeat);
23  emitMsb(D, 5U, index, pending, repeat);
24  emitMsb(F, 6U, index, pending, repeat);
25  emitEnd(index, pending, repeat);
26  IrSequence *repeatSequence = new IrSequence(repeat, index, true);
27  return new IrSignal(emptyIrSequence, *repeatSequence, emptyIrSequence, frequency);
28 }
29 
30 void Rc5Renderer::emitMsb(unsigned int x, unsigned int length,
31  unsigned int& index, int& pending, microseconds_t *repeat) {
32  unsigned int mask = 1U << (length - 1U);
33  while (mask != 0U) {
34  emit((x & mask) != 0, index, pending, repeat);
35  mask >>= 1U;
36  }
37 }
38 
39 void Rc5Renderer::emit(unsigned int x, unsigned int& index, int& pending,
40  microseconds_t *repeat) {
41  if (pending == 0) {
42  // First, do nothing, just stuff in pending.
43  } else if ((pending > 0) == ((x & 1) != 0)) {
44  repeat[index] = timebase;
45  index++;
46  repeat[index] = timebase;
47  index++;
48  } else {
49  repeat[index] = 2 * timebase;
50  index++;
51  }
52  pending = (x & 1U) ? 1 : -1;
53 }
54 
55 void Rc5Renderer::emitEnd(unsigned int& index, int& pending, microseconds_t *repeat) {
56  if (pending > 0) {
57  repeat[index] = timebase; index++;
58  }
59  repeat[index] = 0xFFFF; index++;
60 }
static const IrSignal * newIrSignal(unsigned int D, unsigned int F, unsigned int T)
Generates an RC5 signal from the RC5 parameters.
Definition: Rc5Renderer.cpp:16
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:16
static const IrSequence emptyIrSequence
Definition: Rc5Renderer.cpp:14
This class models an IR signal with intro-, repeat-, and ending sequences.
Definition: IrSignal.h:11
This class consists of a vector of durations.
Definition: IrSequence.h:12