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 #define MIN(a, b) ((a) < (b) ? (a) : (b))
8 
9 uint8_t Rc5Renderer::T = 1U;
10 
11 const IrSignal *Rc5Renderer::newIrSignal(unsigned int D, unsigned int F) {
12  T = ! T;
13  return newIrSignal(D, F, T);
14 }
15 
17 
18 const IrSignal *Rc5Renderer::newIrSignal(unsigned int D, unsigned int F, unsigned int T) {
19  unsigned int index = 0U;
20  int pending = 0;
21  microseconds_t *repeat = new microseconds_t[28];
22  emit(1U, index, pending, repeat);
23  emit(((~F) & 0x40U) >> 6U, index, pending, repeat);
24  emit(T & 1U, index, pending, repeat);
25  emitMsb(D, 5U, index, pending, repeat);
26  emitMsb(F, 6U, index, pending, repeat);
27  emitEnd(index, pending, repeat);
28  IrSequence *repeatSequence = new IrSequence(repeat, index, true);
29  return new IrSignal(emptyIrSequence, *repeatSequence, emptyIrSequence, frequency);
30 }
31 
32 void Rc5Renderer::emitMsb(unsigned int x, unsigned int length,
33  unsigned int& index, int& pending, microseconds_t *repeat) {
34  unsigned int mask = 1U << (length - 1U);
35  while (mask != 0U) {
36  emit((x & mask) != 0, index, pending, repeat);
37  mask >>= 1U;
38  }
39 }
40 
41 void Rc5Renderer::emit(unsigned int x, unsigned int& index, int& pending,
42  microseconds_t *repeat) {
43  if (pending == 0) {
44  // First, do nothing, just stuff in pending.
45  } else if ((pending > 0) == ((x & 1U) != 0U)) {
46  repeat[index] = timebase;
47  index++;
48  repeat[index] = timebase;
49  index++;
50  } else {
51  repeat[index] = 2U * timebase;
52  index++;
53  }
54  pending = (x & 1U) ? 1 : -1;
55 }
56 
57 void Rc5Renderer::emitEnd(unsigned int& index, int& pending, microseconds_t *repeat) {
58  if (pending > 0) {
59  repeat[index] = timebase; index++;
60  }
61  repeat[index] = MIN(90000U, MICROSECONDS_T_MAX); index++;
62 }
static const IrSignal * newIrSignal(unsigned int D, unsigned int F, unsigned int T)
Generates an RC5 signal from the RC5 parameters.
Definition: Rc5Renderer.cpp:18
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
static const IrSequence emptyIrSequence
Definition: Rc5Renderer.cpp:16
#define MICROSECONDS_T_MAX
Largest microseconds_t number possible.
Definition: InfraredTypes.h:18
This class models an IR signal with intro-, repeat-, and ending sequences.
Definition: IrSignal.h:10
This class consists of a vector of durations.
Definition: IrSequence.h:11
#define MIN(a, b)
Definition: Rc5Renderer.cpp:7