| [3] | 1 | /* IRLibSendBase.cpp |
|---|
| 2 | * Part of IRLib Library for Arduino receiving, decoding, and sending |
|---|
| 3 | * infrared signals. See COPYRIGHT.txt and LICENSE.txt for more information. |
|---|
| 4 | */ |
|---|
| 5 | /* |
|---|
| 6 | * This module contains the base classes for sending. You will not create instances |
|---|
| 7 | * of these classes, rather you will use them as base classes in creating derived |
|---|
| 8 | * protocol specific decoders. Each protocol specific send class begins |
|---|
| 9 | * by calling enableIROut(uint8_t kHz) to set the carrier frequency. |
|---|
| 10 | * It then calls mark(int usec) and space(inc usec) to transmit marks and |
|---|
| 11 | * spaces of varying length of microseconds in the manner which the protocol defines. |
|---|
| 12 | */ |
|---|
| [30] | 13 | |
|---|
| 14 | #if !defined(ARDUINO_CIRCUITPLAY_NRF52840 ) |
|---|
| 15 | |
|---|
| [3] | 16 | #include "IRLibSendBase.h" |
|---|
| 17 | #include "IRLibHardware.h" |
|---|
| 18 | |
|---|
| 19 | /* |
|---|
| 20 | * Most of the protocols have a header consisting of a mark/space of a particular length followed by |
|---|
| 21 | * a series of variable length mark/space signals. Depending on the protocol they very the lengths of the |
|---|
| 22 | * mark or the space to indicate a data bit of "0" or "1". Most also end with a stop bit of "1". |
|---|
| 23 | * The basic structure of the sending and decoding these protocols led to lots of redundant code. |
|---|
| 24 | * Therefore I have implemented generic sending and decoding routines. You just need to pass a bunch of customized |
|---|
| 25 | * parameters and it does the work. This reduces compiled code size with only minor speed degradation. |
|---|
| 26 | * You may be able to implement new protocols by simply passing the proper values to these generic routines. |
|---|
| 27 | * The decoding routines do not encode stop bits. So you have to tell this routine whether or not to send one. |
|---|
| 28 | * Some protocols have a fixed amount of space between frames while others require that the entire frame |
|---|
| 29 | * be a particularly length despite the fact that the data transmission time may be veritable. Pass this |
|---|
| 30 | * frame length of time the parameter maxExtent. It's default value is zero. |
|---|
| 31 | */ |
|---|
| 32 | void IRsendBase::sendGeneric(uint32_t data, uint8_t numBits, uint16_t headMark, uint16_t headSpace, |
|---|
| 33 | uint16_t markOne, uint16_t markZero, uint16_t spaceOne, uint16_t spaceZero, |
|---|
| 34 | uint8_t kHz, bool useStop, uint32_t maxExtent) { |
|---|
| 35 | extent=0; |
|---|
| 36 | data = data << (32 - numBits); |
|---|
| 37 | enableIROut(kHz); |
|---|
| 38 | //Some protocols do not send a header when sending repeat codes. So we pass a zero value to indicate skipping this. |
|---|
| 39 | if(headMark) mark(headMark); |
|---|
| 40 | if(headSpace) space(headSpace); |
|---|
| 41 | for (uint8_t i = 0; i <numBits; i++) { |
|---|
| 42 | if (data & TOPBIT) { |
|---|
| 43 | mark(markOne); space(spaceOne); |
|---|
| 44 | } |
|---|
| 45 | else { |
|---|
| 46 | mark(markZero); space(spaceZero); |
|---|
| 47 | } |
|---|
| 48 | data <<= 1; |
|---|
| 49 | } |
|---|
| 50 | if(useStop) mark(markOne); //stop bit of "1" |
|---|
| 51 | if(maxExtent) { |
|---|
| 52 | #ifdef IRLIB_TRACE |
|---|
| 53 | Serial.print("maxExtent="); Serial.println(maxExtent); |
|---|
| 54 | Serial.print("extent="); Serial.println(extent); |
|---|
| 55 | Serial.print("Difference="); Serial.println(maxExtent-extent); |
|---|
| 56 | #endif |
|---|
| 57 | space(maxExtent-extent); |
|---|
| 58 | } |
|---|
| 59 | else space(spaceOne); |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | void IRsendBase::enableIROut(uint8_t khz) { |
|---|
| 65 | //NOTE: the comments on this routine accompanied the original early version of IRremote library |
|---|
| 66 | //which only used TIMER2. The parameters defined in IRLibTimer.h may or may not work this way. |
|---|
| 67 | // Enables IR output. The khz value controls the modulation frequency in kilohertz. |
|---|
| 68 | // The IR output will be on pin 3 (OC2B). |
|---|
| 69 | // This routine is designed for 36-40KHz; if you use it for other values, it's up to you |
|---|
| 70 | // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.) |
|---|
| 71 | // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B |
|---|
| 72 | // controlling the duty cycle. |
|---|
| 73 | // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A) |
|---|
| 74 | // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin. |
|---|
| 75 | // A few hours staring at the ATmega documentation and this will all make sense. |
|---|
| 76 | // See my Secrets of Arduino PWM at http://www.righto.com/2009/07/secrets-of-arduino-pwm.html for details. |
|---|
| 77 | |
|---|
| 78 | // Disable the Timer2 Interrupt (which is used for receiving IR) |
|---|
| 79 | IRLib_didIROut=true; //Tell the receiver we probably trashed his timer settings |
|---|
| 80 | IR_RECV_DISABLE_INTR; //Timer2 Overflow Interrupt |
|---|
| 81 | pinMode(IR_SEND_PWM_PIN, OUTPUT); |
|---|
| 82 | digitalWrite(IR_SEND_PWM_PIN, LOW); // When not sending PWM, we want it low |
|---|
| 83 | IR_SEND_CONFIG_KHZ(khz); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | IRsendBase::IRsendBase () { |
|---|
| 87 | pinMode(IR_SEND_PWM_PIN, OUTPUT); |
|---|
| 88 | digitalWrite(IR_SEND_PWM_PIN, LOW); // When not sending PWM, we want it low |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | //The Arduino built in function delayMicroseconds has limits we wish to exceed |
|---|
| 92 | //Therefore we have created this alternative |
|---|
| 93 | void IRLibDelayUSecs(uint16_t time) { |
|---|
| 94 | if(time){if(time>16000) {delayMicroseconds(time % 1000); delay(time/1000); } else delayMicroseconds(time);}; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void IRsendBase::mark(uint16_t time) { |
|---|
| 98 | IR_SEND_PWM_START; |
|---|
| 99 | IR_SEND_MARK_TIME(time); |
|---|
| 100 | extent+=time; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | void IRsendBase::space(uint16_t time) { |
|---|
| 104 | IR_SEND_PWM_STOP; |
|---|
| 105 | IRLibDelayUSecs(time); |
|---|
| 106 | extent+=time; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| [30] | 109 | |
|---|
| 110 | #endif //!defined(__NRF52__) |
|---|