Infrared4Arduino
IrSender.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2015 Bengt Martensson.
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at
7 your option) any later version.
8 
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see http://www.gnu.org/licenses/.
16 */
17 
18 #include "IrSender.h"
19 #include "IrSignal.h"
20 
21 // From IRLib.cpp, renamed from My_delay_uSecs.
22 
23 //The Arduino built in function delayMicroseconds has limits we wish to exceed
24 //Therefore we have created this alternative
26  if (T) {
27  if (T > 16000) {
28  delayMicroseconds(T % 1000);
29  delay(T / 1000);
30  } else
31  delayMicroseconds(T);
32  };
33 }
34 
36  outputPin = invalidPin;
37 }
38 
40  outputPin = pin;
41  pinMode(pin, OUTPUT);
42  digitalWrite(pin, LOW);
43 }
44 
46  mute();
47 }
48 
50  digitalWrite(outputPin, LOW);
51 }
52 
53 void IrSender::sendIrSignal(const IrSignal& irSignal, unsigned int noSends) {
54  if (!irSignal.getIntro().isEmpty())
55  send(irSignal.getIntro(), irSignal.getFrequency());
56  for (unsigned int i = 0; i < irSignal.noRepetitions(noSends); i++)
57  send(irSignal.getRepeat(), irSignal.getFrequency());
58  if (!irSignal.getEnding().isEmpty())
59  send(irSignal.getEnding(), irSignal.getFrequency());
60 }
61 
62 void IrSender::sendWhile(const IrSignal& irSignal, bool(*trigger)()) {
63  if (trigger()) {
64  // "Button is pressed", send
65 
66  // Send the intro sequence,...
67  sendIrSignal(irSignal, 1);
68 
69  // ... then, for as long as the button is held,
70  // send the repeat sequence
71  while (trigger()) {
72  send(irSignal.getRepeat());
73  }
74 
75  // finally, the ending sequence (normally empty)
76  send(irSignal.getEnding());
77  } else {
78  // Button is not pressed, do nothing.
79  }
80 }
void sendWhile(const IrSignal &irSignal, bool(*trigger)())
Send an IrSignal, when and as long as trigger() returns true.
Definition: IrSender.cpp:62
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
uint8_t pin_t
Type for GPIO pin, compatible with Arduino libs.
Definition: InfraredTypes.h:37
frequency_t getFrequency() const
Definition: IrSignal.h:51
const IrSequence & getIntro() const
Definition: IrSignal.h:63
This class models an IR signal with intro-, repeat-, and ending sequences.
Definition: IrSignal.h:10
const IrSequence & getRepeat() const
Definition: IrSignal.h:59
void delayUSecs(microseconds_t T)
Definition: IrSender.cpp:25
virtual void send(const IrSequence &irSequence, frequency_t frequency=IrSignal::defaultFrequency)=0
Sends an IrSequence with the prescribed frequency.
const IrSequence & getEnding() const
Definition: IrSignal.h:55
const pin_t invalidPin
Symbolic name for an invalid pin number.
Definition: InfraredTypes.h:39
unsigned int noRepetitions(unsigned int noSends) const
Implementation of the count semantics, i.e., how many repetitions should be sent if the signal is sen...
Definition: IrSignal.h:103
virtual void mute()
Send an IrSignal, when and as long as buttonPin is LOW.
Definition: IrSender.cpp:49
virtual ~IrSender()
Definition: IrSender.cpp:45
IrSender()
Definition: IrSender.cpp:35
bool isEmpty() const
Definition: IrSequence.h:52
void sendIrSignal(const IrSignal &irSignal, unsigned int noSends=1)
Sends the IrSignal given as argument the prescribed number of times.
Definition: IrSender.cpp:53