Infrared4Arduino
IrWidget.cpp
Go to the documentation of this file.
1 #include "IrSignal.h"
2 
3 /* IR Widget: capture a raw IR signal and dump the timing of the non-demodulated signal
4 
5 http://www.piclist.com/images/boards/irwidget/index.htm
6 http://www.hifi-remote.com/forums/dload.php?action=file&file_id=2044
7 http://www.hifi-remote.com/wiki/index.php?title=IR_Scope_and_IR_Widget_User%27s_Guide
8 http://www.compendiumarcana.com/irwidget/
9 
10 Arduino digital pin numbers for the input capture pin (ICP) and the logic analyzer debugging pin (LA Dbg):
11 Board name / MCU | ICP pin | LA Dbg pin
12 -------------------------------------------|--------------.-----------|------------------------
13 Duemilanove/Uno (ATmega328P / ATmega168) | ICP1/PB0, Arduino pin 8 | PD6, Arduino pin 6
14 Leonardo (ATmega32U4) | ICP1/PD4, Arduino pin 4 | PD6, Arduino pin 12
15 Arduino Mega 2560 (ATmega2560) | ICP4/PL0, Arduino pin 49 | PL6, Arduino pin 43
16 
17 see also here:
18 http://arduino.cc/en/Hacking/PinMapping168 (also for ATmega328P)
19 http://arduino.cc/en/Hacking/PinMapping32u4
20 http://arduino.cc/en/Hacking/PinMapping2560
21  */
22 
23 // Copyright (c) 2012 Michael Dreher <michael(at)5dot1.de>
24 // this code may be distributed under the terms of the General Public License V2 (GPL V2)
25 // NOTE(BM) Michael agrees to "or later", see
26 // http://www.hifi-remote.com/forums/viewtopic.php?p=112586#112586
27 
28 // Code slighty reorganized by Bengt Martensson
29 
30 #if defined (DOXYGEN) || !defined(ARDUINO) || defined ARDUINO_ARCH_AVR
31 
32 #include "IrWidget.h"
33 
34 IrWidget::IrWidget(size_t captureLength,
35  bool pullup,
36  int16_t markExcess,
37  milliseconds_t beginningTimeout,
38  milliseconds_t endingTimeout) : IrReader(captureLength) {
39  setup(pullup);
43  //endingTimeout = _BV(RANGE_EXTENSION_BITS) - 1;
45 
46  // Test that allocated memory is indeed usable. Otherwise crash will occur.
47  for (unsigned int i = 0; i < bufferSize; i++)
48  captureData[i] = 0;
49 }
50 
52  delete[] captureData;
53 }
54 
56  endingTimeout = (ovlBitsDataType) ((timeOut/* /1000*/ + 16)/32);
57 }
58 
60  //return (uint16_t) (timerValueToNanoSeconds((((uint32_t) endingTimeout)*CAPTURE_PRESCALER_FACTOR)) / 1000);
61  return (uint16_t) 1000 * (endingTimeout << 15);
62 }
63 
64 void IrWidget::dump(Stream &stream) const {
65  bool printedSomething = IrSignal::dumpFrequency(stream, getFrequency());
66  if (printedSomething)
67  stream.print(' ');
68  IrReader::dump(stream);
69 }
70 
72 // Initialization
74 
75 // initialize Timer and IO pins, needs to be called once
76 void IrWidget::setup(bool pullup) {
77 #ifdef ARDUINO
78  // configure signal capture ICP pin as input
79  cbi(CAT2(DDR, CAP_PORT), CAP_PIN);
80  if (pullup)
81  sbi(CAT2(PORT, CAP_PORT), CAP_PIN); // enable the internal 10k pull-up resistor
82 
83 #if defined(DEBUG_PIN) && defined(DEBUG_PORT)
84  sbi(CAT2(DDR, DEBUG_PORT), DEBUG_PIN); // configure logic analyzer debug pin as output
85 #endif
86 
87  // init timer, disable power save mode of timer
88 #ifdef PRR0 // for ATmega32U4 and ATmega2560
89 #if PRTIM <= 2
90  cbi(PRR0, CAT2(PRTIM, CAP_TIM)); // for ATmega32U4 and ATmega2560
91 #else
92  cbi(PRR1, CAT2(PRTIM, CAP_TIM)); // for ATmega2560
93 #endif
94 #else
95  cbi(PRR, CAT2(PRTIM, CAP_TIM));
96 #endif
97 
98  CAT3(TCCR, CAP_TIM, A) = 0; // Timer mode 0 = normal
99  CAT3(TCCR, CAP_TIM, B) = _BV(CAT2(ICNC, CAP_TIM)) | CAPTURE_PRESCALER_SETTING; // prescaler according to setting, enable noise canceler
100 #else
101  std::cout << "pinMode(CAPTURE_PIN_1, " << (pullup ? "INPUT_PULLUP)" : "INPUT)") << std::endl;
102 #endif
103 }
104 
105 #endif // ARDUINO_ARCH_AVR
void setEndingTimeout(milliseconds_t timeout)
Sets the ending timeout.
Definition: IrWidget.cpp:55
virtual void setBeginningTimeout(milliseconds_t timeOut)
Definition: IrReader.h:138
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
milliseconds_t getEndingTimeout() const
Definition: IrWidget.cpp:59
void setMarkExcess(int16_t markExcess_)
Sets the markExcess, a number (possibly negative) to be subtracted from the on-durations and added to...
Definition: IrReader.h:155
uint8_t ovlBitsDataType
Definition: IrWidget.h:128
size_t bufferSize
Definition: IrReader.h:41
uint16_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:25
#define CAP_TIM
Definition: IrWidget.h:172
virtual void dump(Stream &stream) const
Prints a textual representation of the received data to the Stream supplied.
Definition: IrReader.cpp:21
#define CAT3(prefix, num, postfix)
Definition: IrWidget.h:184
Abstract base class for all IR readers, capturing or receiving.
Definition: IrReader.h:30
#define cbi(sfr, bit)
Definition: IrWidget.h:179
uint16_t * captureData
Definition: IrWidget.h:199
virtual ~IrWidget()
Definition: IrWidget.cpp:51
#define sbi(sfr, bit)
Definition: IrWidget.h:180
#define CAP_PORT
Definition: IrWidget.h:170
frequency_t getFrequency() const
Returns frequency of received signal.
Definition: IrWidget.h:102
bool dumpFrequency(Stream &stream) const
If the frequency is sensible, print it to the stream and return true.
Definition: IrSignal.h:87
#define CAPTURE_PRESCALER_SETTING
Definition: IrWidget.h:117
milliseconds_t beginningTimeout
Definition: IrReader.h:38
void dump(Stream &stream) const
Prints a textual representation of the received data to the Stream supplied.
Definition: IrWidget.cpp:64
ovlBitsDataType endingTimeout
Definition: IrWidget.h:133
#define CAT2(prefix, num)
Definition: IrWidget.h:182
#define CAP_PIN
Definition: IrWidget.h:171
int16_t markExcess
Microseconds subtracted from pulses and added to gaps.
Definition: IrReader.h:44
IrWidget(size_t captureLength=defaultCaptureLength, bool pullup=false, int16_t markExcess=defaultMarkExcess, milliseconds_t beginningTimeout=defaultBeginningTimeout, milliseconds_t endingTimeout=defaultEndingTimeout)
Definition: IrWidget.cpp:34