* Enables IR output. The kHz value controls the modulation frequency in kilohertz. * IF PWM should be generated by a timer, it uses the platform specific timerConfigForSend() function, * otherwise it computes the delays used by the mark() function. * If IR_SEND_PIN is defined, maximum PWM frequency for an AVR @16 MHz is 170 kHz (180 kHz if NO_LED_SEND_FEEDBACK_CODE is defined) */
| 1496 | * If IR_SEND_PIN is defined, maximum PWM frequency for an AVR @16 MHz is 170 kHz (180 kHz if NO_LED_SEND_FEEDBACK_CODE is defined) |
| 1497 | */ |
| 1498 | void IRsend::enableIROut(uint_fast8_t aFrequencyKHz) { |
| 1499 | #if defined(SEND_PWM_BY_TIMER) |
| 1500 | timerConfigForSend(aFrequencyKHz); // must set output pin mode and disable receive interrupt if required, e.g. uses the same resource |
| 1501 | |
| 1502 | #elif defined(USE_NO_SEND_PWM) |
| 1503 | (void) aFrequencyKHz; |
| 1504 | |
| 1505 | #else |
| 1506 | periodTimeMicros = (1000U + (aFrequencyKHz / 2)) / aFrequencyKHz; // rounded value -> 26 for 38.46 kHz, 27 for 37.04 kHz, 25 for 40 kHz. |
| 1507 | # if defined(IR_SEND_PIN) |
| 1508 | periodOnTimeMicros = (((periodTimeMicros * IR_SEND_DUTY_CYCLE_PERCENT) + 50) / 100U); // +50 for rounding -> 830/100 for 30% and 16 MHz |
| 1509 | # else |
| 1510 | // Heuristics! We require a nanosecond correction for "slow" digitalWrite() functions |
| 1511 | periodOnTimeMicros = (((periodTimeMicros * IR_SEND_DUTY_CYCLE_PERCENT) + 50 - (PULSE_CORRECTION_NANOS / 10)) / 100U); // +50 for rounding -> 530/100 for 30% and 16 MHz |
| 1512 | # endif |
| 1513 | #endif // defined(SEND_PWM_BY_TIMER) |
| 1514 | |
| 1515 | #if defined(USE_OPEN_DRAIN_OUTPUT_FOR_SEND_PIN) && defined(OUTPUT_OPEN_DRAIN) // the mode INPUT for mimicking open drain is set at IRLedOff() |
| 1516 | # if defined(IR_SEND_PIN) |
| 1517 | pinModeFast(IR_SEND_PIN, OUTPUT_OPEN_DRAIN); |
| 1518 | # else |
| 1519 | pinModeFast(sendPin, OUTPUT_OPEN_DRAIN); |
| 1520 | # endif |
| 1521 | #else |
| 1522 | |
| 1523 | // For Non AVR platforms pin mode for SEND_PWM_BY_TIMER must be handled by the timerConfigForSend() function |
| 1524 | // because ESP 2.0.2 ledcWrite does not work if pin mode is set, and RP2040 requires gpio_set_function(IR_SEND_PIN, GPIO_FUNC_PWM); |
| 1525 | # if defined(__AVR__) || !defined(SEND_PWM_BY_TIMER) |
| 1526 | # if defined(IR_SEND_PIN) |
| 1527 | pinModeFast(IR_SEND_PIN, OUTPUT); |
| 1528 | # else |
| 1529 | if (__builtin_constant_p(sendPin)) { |
| 1530 | pinModeFast(sendPin, OUTPUT); |
| 1531 | } else { |
| 1532 | pinMode(sendPin, OUTPUT); |
| 1533 | } |
| 1534 | # endif |
| 1535 | # endif |
| 1536 | #endif // defined(USE_OPEN_DRAIN_OUTPUT_FOR_SEND_PIN) |
| 1537 | } |
| 1538 | |
| 1539 | #if defined(SEND_PWM_BY_TIMER) |
| 1540 | // Used for Bang&Olufsen |
no test coverage detected