Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
IrReader.h
Go to the documentation of this file.
1/*
2Copyright (C) 2015 Bengt Martensson.
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or (at
7your option) any later version.
8
9This program is distributed in the hope that it will be useful, but
10WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see http://www.gnu.org/licenses/.
16*/
17
18#pragma once
19
20#include <Arduino.h>
21
22#include "InfraredTypes.h"
23#include "IrSequence.h"
24
30class IrReader {
31public:
32 // Defaults
33 static constexpr milliseconds_t defaultBeginningTimeout = 2000U;
34 static constexpr milliseconds_t defaultEndingTimeout = 30U;
35 static constexpr size_t defaultCaptureLength = 100U;
36
37protected:
40
41 size_t bufferSize;
42
44 int16_t markExcess;
45
48
49 static unsigned int forceEven(unsigned int x) {
50 return (x & 1) ? x + 1 : x;
51 }
52
57 IrReader(size_t bufSize_) : bufferSize(forceEven(bufSize_)),timeouted(false) {
58 }
59
61 }
62
63#ifndef DOXYGEN
64 IrReader(const IrReader&) = delete;
65 IrReader(IrReader&&) = delete;
66 IrReader& operator=(const IrReader&) = delete;
67 IrReader& operator=(IrReader&&) = delete;
68#endif // ! DOXYGEN
69
70 virtual ~IrReader() {
71 };
72
73public:
74 virtual void reset() {
75 timeouted = false;
76 };
77
83 virtual frequency_t getFrequency() const = 0;
84
88 virtual void enable() {
89 };
90
94 virtual void disable() {
95 };
96
100 virtual void receive() = 0;
101
106 virtual bool isReady() const = 0;
107
112 operator bool() const {
113 return isReady();
114 }
115
120 virtual size_t getDataLength() const = 0;
121
127 virtual microseconds_t getDuration(unsigned int index) const = 0;
128
133 virtual void dump(Stream &stream) const;
134
139 IrSequence *toIrSequence() const;
140
141 virtual bool isEmpty() const {
142 return getDataLength() == 0;
143 }
144
145 virtual void setEndingTimeout(milliseconds_t timeOut) {
146 endingTimeout = timeOut;
147 }
148
150 return endingTimeout;
151 }
152
153 virtual void setBeginningTimeout(milliseconds_t timeOut) {
154 beginningTimeout = timeOut;
155 }
156
158 return beginningTimeout;
159 }
160
161 unsigned int getBufferSize() const {
162 return bufferSize;
163 }
164
170 void setMarkExcess(int16_t markExcess_) {
171 markExcess = markExcess_;
172 }
173
179 int16_t getMarkExcess() const {
180 return markExcess;
181 }
182};
This file defines some general data types that are used in the library.
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
uint32_t frequency_t
Type for modulation frequency in Hz.
Definition: InfraredTypes.h:31
uint16_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:24
Abstract base class for all IR readers, capturing or receiving.
Definition: IrReader.h:30
virtual void disable()
Stop reception of IR data.
Definition: IrReader.h:94
virtual bool isReady() const =0
Returns true if there is collected data.
IrReader(size_t bufSize_)
Constructs an IrReader with buffersize bufSize_, possibly increased to be even.
Definition: IrReader.h:57
virtual void receive()=0
Convenience function: enable, wait until data is collected or timeout has occured,...
virtual void setBeginningTimeout(milliseconds_t timeOut)
Definition: IrReader.h:153
virtual frequency_t getFrequency() const =0
Returns frequency of received signal.
virtual size_t getDataLength() const =0
Returns the number of collected durations.
virtual milliseconds_t getEndingTimeout() const
Definition: IrReader.h:149
static constexpr size_t defaultCaptureLength
Definition: IrReader.h:35
virtual milliseconds_t getBeginningTimeout() const
Definition: IrReader.h:157
int16_t markExcess
Microseconds subtracted from pulses and added to gaps.
Definition: IrReader.h:44
milliseconds_t beginningTimeout
Definition: IrReader.h:38
static constexpr milliseconds_t defaultEndingTimeout
Definition: IrReader.h:34
static unsigned int forceEven(unsigned int x)
Definition: IrReader.h:49
virtual void reset()
Definition: IrReader.h:74
virtual void enable()
Start reception of IR data.
Definition: IrReader.h:88
size_t bufferSize
Definition: IrReader.h:41
IrReader()
Definition: IrReader.h:60
static constexpr milliseconds_t defaultBeginningTimeout
Definition: IrReader.h:33
virtual ~IrReader()
Definition: IrReader.h:70
IrSequence * toIrSequence() const
Generates an IrSequence from the IrReader.
Definition: IrReader.cpp:31
virtual bool isEmpty() const
Definition: IrReader.h:141
milliseconds_t endingTimeout
Definition: IrReader.h:39
unsigned int getBufferSize() const
Definition: IrReader.h:161
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:170
virtual void setEndingTimeout(milliseconds_t timeOut)
Definition: IrReader.h:145
bool timeouted
True if last receive ended with a timeout.
Definition: IrReader.h:47
virtual void dump(Stream &stream) const
Prints a textual representation of the received data to the Stream supplied.
Definition: IrReader.cpp:21
int16_t getMarkExcess() const
Gets the markExcess, a number (possibly negative) to be subtracted from the on-durations and added to...
Definition: IrReader.h:179
virtual microseconds_t getDuration(unsigned int index) const =0
Returns the index-th duration, if possible.
This class consists of a vector of durations.
Definition: IrSequence.h:11