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