Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
IrSenderPwmSoft.cpp
Go to the documentation of this file.
1/*
2Copyright (C) 2020 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 <Arduino.h>
19#include "IrSenderPwmSoft.h"
20
22}
23
24//void IrSenderPwmSoft::sendSpace(milliseconds_t time) {
25// writeLow();
26// delayUSecs(time);
27//}
28
29//void inline IrSenderPwmSoft::sleepMicros(unsigned long us) {
30//#ifdef USE_SPIN_WAIT
31// sleepUntilMicros(micros() + us);
32//#else
33// if (us > 0U) // Is this necessary? (Official docu https://www.arduino.cc/en/Reference/DelayMicroseconds does not tell.)
34// delayMicroseconds((unsigned int) us);
35//#endif
36//}
37
38//void IrSenderPwmSoft::sleepUntilMicros(unsigned long targetTime) {
39//#ifdef USE_SPIN_WAIT
40// while (micros() < targetTime)
41// ;
42//#else
43// sleepMicros(targetTime - micros());
44//#endif
45//}
46
48 writeLow();
49 periodTime = (1000000UL + hz / 2L) / hz; // = 1000000/hz + 1/2 = round(1000000.0/hz)
50 periodOnTime = periodTime * dutyCycle / 100U /*- pulseCorrection*/;
52}
53
55 unsigned long start = micros();
56 unsigned long stop = start + time;
57 if (stop + periodTime < start)
58 // Counter wrap-around, happens very seldomly, but CAN happen.
59 // Just give up instead of possibly damaging the hardware.
60 return;
61
62 //unsigned long nextPeriodEnding = start;
63 unsigned long now = micros();
64 while (now < stop) {
65 unsigned long then = now + periodTime;
66 writeHigh();
68 writeLow();
69 //nextPeriodEnding += periodTime;
70 sleepUntilMicros(then);
71 now = micros();
72 }
73}
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
microseconds_t periodTime
virtual void sleepMicros(microseconds_t us)=0
virtual void sleepUntilMicros(uint32_t terminateTime)=0
microseconds_t periodOnTime
microseconds_t periodOffTime
void enable(frequency_t hz, dutycycle_t dutyCycle=Board::defaultDutyCycle)
void sendMark(microseconds_t time)
IrSenderPwmSoft(pin_t outputPin)
Sending function using timer PWM.
Definition: IrSenderPwm.h:29
void writeLow()
Definition: IrSender.h:39
void writeHigh()
Definition: IrSender.h:38