source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/IRLibSendBase.cpp @ 8

Last change on this file since 8 was 3, checked in by markus, 7 years ago

Setup project structure

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