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 #include "IrWidget.h"
31 
32 IrWidget::IrWidget(size_t captureLength,
33  boolean pullup,
34  int16_t markExcess,
35  milliseconds_t beginningTimeout,
36  milliseconds_t endingTimeout) : IrReader(captureLength) {
37  setup(pullup);
39  setMarkExcess(markExcess);
40  setBeginningTimeout(beginningTimeout);
41  //endingTimeout = _BV(RANGE_EXTENSION_BITS) - 1;
42  setEndingTimeout(endingTimeout);
43 
44  // Test that allocated memory is indeed usable. Otherwise crash will occur.
45  for (unsigned int i = 0; i < bufferSize; i++)
46  captureData[i] = 0;
47 }
48 
50  delete[] captureData;
51 }
52 
54  endingTimeout = (ovlBitsDataType) ((timeOut/* /1000*/ + 16)/32);
55 }
56 
58  //return (uint16_t) (timerValueToNanoSeconds((((uint32_t) endingTimeout)*CAPTURE_PRESCALER_FACTOR)) / 1000);
59  return (uint16_t) 1000 * (endingTimeout << 15);
60 }
61 
62 void IrWidget::dump(Stream &stream) const {
63  boolean printedSomething = IrSignal::dumpFrequency(stream, getFrequency());
64  if (printedSomething)
65  stream.print(' ');
66  IrReader::dump(stream);
67 }
68 
70 // Initialization
72 
73 // initialize Timer and IO pins, needs to be called once
74 void IrWidget::setup(boolean pullup) {
75 #ifdef ARDUINO
76  // configure signal capture ICP pin as input
77  cbi(CAT2(DDR, CAP_PORT), CAP_PIN);
78  if (pullup)
79  sbi(CAT2(PORT, CAP_PORT), CAP_PIN); // enable the internal 10k pull-up resistor
80 
81 #if defined(DEBUG_PIN) && defined(DEBUG_PORT)
82  sbi(CAT2(DDR, DEBUG_PORT), DEBUG_PIN); // configure logic analyzer debug pin as output
83 #endif
84 
85  // init timer, disable power save mode of timer
86 #ifdef PRR0 // for ATmega32U4 and ATmega2560
87 #if PRTIM <= 2
88  cbi(PRR0, CAT2(PRTIM, CAP_TIM)); // for ATmega32U4 and ATmega2560
89 #else
90  cbi(PRR1, CAT2(PRTIM, CAP_TIM)); // for ATmega2560
91 #endif
92 #else
93  cbi(PRR, CAT2(PRTIM, CAP_TIM));
94 #endif
95 
96  CAT3(TCCR, CAP_TIM, A) = 0; // Timer mode 0 = normal
97  CAT3(TCCR, CAP_TIM, B) = _BV(CAT2(ICNC, CAP_TIM)) | CAPTURE_PRESCALER_SETTING; // prescaler according to setting, enable noise canceler
98 #else
99  std::cout << "pinMode(CAPTURE_PIN_1, " << (pullup ? "INPUT_PULLUP)" : "INPUT)") << std::endl;
100 #endif
101 }
void setEndingTimeout(milliseconds_t timeout)
Sets the ending timeout.
Definition: IrWidget.cpp:53
virtual void setBeginningTimeout(milliseconds_t timeOut)
Definition: IrReader.h:131
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:16
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:148
#define cbi(sfr, bit)
Definition: IRremoteInt.h:109
uint8_t ovlBitsDataType
Definition: IrWidget.h:122
milliseconds_t getEndingTimeout() const
Definition: IrWidget.cpp:57
size_t bufferSize
Definition: IrReader.h:41
uint16_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:26
#define CAP_TIM
Definition: IrWidget.h:167
#define CAT3(prefix, num, postfix)
Definition: IrWidget.h:179
IrWidget(size_t captureLength=defaultCaptureLength, boolean pullup=false, int16_t markExcess=defaultMarkExcess, milliseconds_t beginningTimeout=defaultBeginningTimeout, milliseconds_t endingTimeout=defaultEndingTimeout)
Definition: IrWidget.cpp:32
Abstract base class for all IR readers, capturing or receiving.
Definition: IrReader.h:30
virtual void dump(Stream &stream) const
Prints a textual representation of the received data to the Stream supplied.
Definition: IrReader.cpp:21
uint16_t * captureData
Definition: IrWidget.h:194
virtual ~IrWidget()
Definition: IrWidget.cpp:49
frequency_t getFrequency() const
Definition: IrWidget.h:97
#define sbi(sfr, bit)
Definition: IRremoteInt.h:113
#define CAP_PORT
Definition: IrWidget.h:165
#define CAPTURE_PRESCALER_SETTING
Definition: IrWidget.h:111
void dump(Stream &stream) const
Prints a textual representation of the received data to the Stream supplied.
Definition: IrWidget.cpp:62
ovlBitsDataType endingTimeout
Definition: IrWidget.h:129
#define CAT2(prefix, num)
Definition: IrWidget.h:177
boolean dumpFrequency(Stream &stream) const
If the frequency is sensible, print it to the stream and return true.
Definition: IrSignal.h:88
#define CAP_PIN
Definition: IrWidget.h:166