Infrared4Arduino
IrSenderPwm.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 3 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 <Arduino.h>
19 #include "IrSenderPwm.h"
20 #include <IrTimerDefs.h>
21 
22 IrSenderPwm *IrSenderPwm::instance = NULL;
23 
24 IrSenderPwm::IrSenderPwm() : IrSender(TIMER_PWM_PIN) {
25 }
26 
27 void IrSenderPwm::send(const IrSequence& irSequence, frequency_t frequency) {
28  enable(frequency/1000);
29  for (unsigned int i = 0; i < irSequence.getLength(); i++) {
30  digitalWrite(getOutputPin(), (i & 1) ? LOW : HIGH);
31  if (i & 1) {
33  } else {
35  }
36  delayUSecs(irSequence.getDurations()[i]);
37  }
38  digitalWrite(getOutputPin(), LOW);
39 }
40 
42  if (instance != NULL)
43  return NULL;
44  instance = new IrSenderPwm();
45  return instance;
46 }
47 
49  if (instance == NULL && create)
50  instance = new IrSenderPwm();
51  return instance;
52 }
53 
54 #ifndef UNUSED
55 #define UNUSED
57 #endif
59 void IrSenderPwm::enable(unsigned char khz UNUSED) {
61  pinMode(TIMER_PWM_PIN, OUTPUT);
62  digitalWrite(TIMER_PWM_PIN, LOW);
63  TIMER_CONFIG_KHZ(khz);
64 }
#define TIMER_ENABLE_PWM
Definition: IRremoteInt.h:228
#define TIMER_CONFIG_KHZ(val)
Definition: IRremoteInt.h:234
#define TIMER_DISABLE_INTR
Definition: IRremoteInt.h:231
static IrSenderPwm * getInstance(boolean create=false)
Returns a pointer to the instance, or NULL if not initialized.
Definition: IrSenderPwm.cpp:48
Abstract base class for all sending classes.
Definition: IrSender.h:27
#define TIMER_DISABLE_PWM
Definition: IRremoteInt.h:229
void delayUSecs(microseconds_t T)
Definition: IrSender.cpp:25
uint16_t frequency_t
Type for modulation frequency in Hz.
Definition: InfraredTypes.h:33
pin_t getOutputPin() const
Definition: IrSender.h:32
Sending function using timer PWM.
Definition: IrSenderPwm.h:29
size_t getLength() const
Returns the length of the data.
Definition: IrSequence.h:49
const microseconds_t * getDurations() const
Definition: IrSequence.h:57
This class consists of a vector of durations.
Definition: IrSequence.h:12
static IrSenderPwm * newInstance()
Creates a new instance (if not existing) and returns it.
Definition: IrSenderPwm.cpp:41
void send(const IrSequence &sequence, frequency_t frequency=IrSignal::defaultFrequency)
Sends an IrSequence with the prescribed frequency.
Definition: IrSenderPwm.cpp:27
Definition of timers etc is encapsulated in this file.
#define TIMER_PWM_PIN
Definition: IRremoteInt.h:269