Skip to content

Commit 24d20e3

Browse files
committed
Merge pull request Arduino-IRremote#198 from pcoughlin/master
update custom_delay function
2 parents 11cb3fe + 7aee7fc commit 24d20e3

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

IRremote.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class IRsend
253253
public:
254254
IRsend () { }
255255

256-
void custom_delay_ms (unsigned int time);
256+
void custom_delay_usec (unsigned long uSecs);
257257
void enableIROut (int khz) ;
258258
void mark (int usec) ;
259259
void space (int usec) ;

irSend.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void IRsend::sendRaw (unsigned int buf[], unsigned char len, unsigned char hz
2222
void IRsend::mark (int time)
2323
{
2424
TIMER_ENABLE_PWM; // Enable pin 3 PWM output
25-
if (time > 0) custom_delay_ms(time);
25+
if (time > 0) custom_delay_usec(time);
2626
}
2727

2828
//+=============================================================================
@@ -33,7 +33,7 @@ void IRsend::mark (int time)
3333
void IRsend::space (int time)
3434
{
3535
TIMER_DISABLE_PWM; // Disable pin 3 PWM output
36-
if (time > 0) IRsend::custom_delay_ms(time);
36+
if (time > 0) IRsend::custom_delay_usec(time);
3737
}
3838

3939

@@ -71,14 +71,16 @@ void IRsend::enableIROut (int khz)
7171
//+=============================================================================
7272
// Custom delay function that circumvents Arduino's delayMicroseconds limit
7373

74-
void IRsend::custom_delay_ms(unsigned int time) {
75-
if (time)
76-
{
77-
if (time > 16000)
78-
{
79-
delayMicroseconds(time % 1000);
80-
delay(time / 1000);
81-
}
82-
else delayMicroseconds(time);
83-
}
74+
void IRsend::custom_delay_usec(unsigned long uSecs) {
75+
if (uSecs > 4) {
76+
unsigned long start = micros();
77+
unsigned long endMicros = start + uSecs - 4;
78+
if (endMicros < start) { // Check if overflow
79+
while ( micros() > start ) {} // wait until overflow
80+
}
81+
while ( micros() < endMicros ) {} // normal wait
82+
} else {
83+
__asm__("nop\n\t"); // must have or compiler optimizes out
84+
}
8485
}
86+

0 commit comments

Comments
 (0)