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 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 <Arduino.h>
19 #include "IrSenderPwm.h"
20 #include <IrTimerDefs.h>
21 
22 IrSenderPwm *IrSenderPwm::instance = NULL;
23 
24 IrSenderPwm::IrSenderPwm() : IrSender(SEND_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(SEND_PIN, OUTPUT);
62  digitalWrite(SEND_PIN, LOW);
63  TIMER_CONFIG_KHZ(khz);
64 }
#define TIMER_DISABLE_INTR
Definition: boarddefs.h:225
uint32_t frequency_t
Type for modulation frequency in Hz.
Definition: InfraredTypes.h:32
#define TIMER_DISABLE_PWM
Definition: boarddefs.h:223
const microseconds_t * getDurations() const
Definition: IrSequence.h:56
#define TIMER_CONFIG_KHZ(val)
Definition: boarddefs.h:228
size_t getLength() const
Returns the length of the data.
Definition: IrSequence.h:48
Abstract base class for all sending classes.
Definition: IrSender.h:26
void delayUSecs(microseconds_t T)
Definition: IrSender.cpp:25
pin_t getOutputPin() const
Definition: IrSender.h:31
Sending function using timer PWM.
Definition: IrSenderPwm.h:28
#define TIMER_ENABLE_PWM
Definition: boarddefs.h:222
This class consists of a vector of durations.
Definition: IrSequence.h:11
#define SEND_PIN
Definition: boarddefs.h:267
static IrSenderPwm * getInstance(bool create=false)
Returns a pointer to the instance, or NULL if not initialized.
Definition: IrSenderPwm.cpp:48
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.