Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
IrSender.cpp
Go to the documentation of this file.
1/*
2Copyright (C) 2015 Bengt Martensson.
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or (at
7your option) any later version.
8
9This program is distributed in the hope that it will be useful, but
10WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see http://www.gnu.org/licenses/.
16*/
17
18#include "IrSender.h"
19#include "IrSignal.h"
20
21IrSender::IrSender(pin_t pin) : sendPin(pin) {
22 Board::getInstance()->setPinMode(pin, OUTPUT);
24}
25
27 mute();
28}
29
30void IrSender::sendIrSignal(const IrSignal& irSignal, unsigned int noSends) {
31 send(irSignal.getIntro(), irSignal.getFrequency());
32 for (unsigned int i = 0; i < irSignal.noRepetitions(noSends); i++)
33 send(irSignal.getRepeat(), irSignal.getFrequency());
34 send(irSignal.getEnding(), irSignal.getFrequency());
35}
36
37void IrSender::sendWhile(const IrSignal& irSignal, bool(*trigger)()) {
38 if (trigger()) {
39 // "Button is pressed", send
40
41 // Send the intro sequence,...
42 sendIrSignal(irSignal, 1);
43
44 // ... then, for as long as the button is held,
45 // send the repeat sequence
46 while (trigger()) {
47 send(irSignal.getRepeat());
48 }
49
50 // finally, the ending sequence (normally empty)
51 send(irSignal.getEnding());
52 } else {
53 // Button is not pressed, do nothing.
54 }
55}
56
57void IrSender::send(const IrSequence& irSequence, frequency_t frequency, dutycycle_t dutyCycle) {
58 enable(frequency, dutyCycle);
59#ifdef CONSIDER_COMPUTATIONAL_DELAYS
60 uint32_t refTime = micros();
61#endif
62 for (unsigned int i = 0U; i < irSequence.getLength(); i++) {
63#ifdef CONSIDER_COMPUTATIONAL_DELAYS
64#error not tested
65 microseconds_t duration = irSequence[i];
66 refTime += duration;
67 int32_t delay = refTime - micros(); // TODO verify overflow
68 if (delay <= 0)
69 return;
70#else
71 microseconds_t delay = irSequence[i];
72#endif
73 if (i & 1U)
74 sendSpace(delay);
75 else
76 sendMark(delay);
77 }
78}
int8_t dutycycle_t
Type for duty cycle in percent.
Definition: InfraredTypes.h:36
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
uint32_t frequency_t
Type for modulation frequency in Hz.
Definition: InfraredTypes.h:31
uint8_t pin_t
Type for GPIO pin, compatible with Arduino libs.
Definition: InfraredTypes.h:41
static Board * getInstance()
Definition: Board.h:53
void setPinMode(pin_t pin, PinMode mode)
Definition: Board.h:48
virtual void writeLow()
Definition: Board.h:298
virtual void sendMark(microseconds_t time)=0
virtual void send(const IrSequence &irSequence, frequency_t frequency=IrSignal::defaultFrequency, dutycycle_t dutyCycle=Board::defaultDutyCycle)
Sends an IrSequence with the prescribed frequency.
Definition: IrSender.cpp:57
IrSender(pin_t pin)
Definition: IrSender.cpp:21
virtual ~IrSender()
Definition: IrSender.cpp:26
virtual void enable(frequency_t frequency, dutycycle_t dutyCycle=Board::defaultDutyCycle)=0
virtual void sendSpace(microseconds_t time)
Definition: IrSender.h:47
void sendIrSignal(const IrSignal &irSignal, unsigned int noSends=1)
Sends the IrSignal given as argument the prescribed number of times.
Definition: IrSender.cpp:30
void mute()
Force output pin inactive.
Definition: IrSender.h:87
void sendWhile(const IrSignal &irSignal, bool(*trigger)())
Send an IrSignal, when and as long as trigger() returns true.
Definition: IrSender.cpp:37
This class consists of a vector of durations.
Definition: IrSequence.h:11
size_t getLength() const
Returns the number of durations.
Definition: IrSequence.h:65
This class models an IR signal with intro-, repeat-, and ending sequences.
Definition: IrSignal.h:11
frequency_t getFrequency() const
Definition: IrSignal.h:130
const IrSequence & getIntro() const
Definition: IrSignal.h:146
const IrSequence & getRepeat() const
Definition: IrSignal.h:142
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:209
const IrSequence & getEnding() const
Definition: IrSignal.h:138