Infrared4Arduino
IrReader.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2015 Bengt Martensson.
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at
7 your option) any later version.
8 
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this 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 
30 class IrReader {
31 public:
32  // Defaults
35  static const size_t defaultCaptureLength = 100U;
36 
37 protected:
40 
41  size_t bufferSize;
42 
44  int16_t markExcess;
45 
47  bool timeouted;
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  virtual ~IrReader() {
64  };
65 
66 public:
67  virtual void reset() {
68  timeouted = false;
69  };
70 
76  virtual frequency_t getFrequency() const = 0;
77 
81  virtual void enable() {
82  };
83 
87  virtual void disable() {
88  };
89 
93  virtual void receive() = 0;
94 
99  virtual bool isReady() const = 0;
100 
105  virtual size_t getDataLength() const = 0;
106 
112  virtual microseconds_t getDuration(unsigned int index) const = 0;
113 
118  virtual void dump(Stream &stream) const;
119 
124  IrSequence *toIrSequence() const;
125 
126  virtual bool isEmpty() const {
127  return getDataLength() == 0;
128  }
129 
130  virtual void setEndingTimeout(milliseconds_t timeOut) {
131  endingTimeout = timeOut;
132  }
133 
135  return endingTimeout;
136  }
137 
138  virtual void setBeginningTimeout(milliseconds_t timeOut) {
139  beginningTimeout = timeOut;
140  }
141 
143  return beginningTimeout;
144  }
145 
146  unsigned int getBufferSize() const {
147  return bufferSize;
148  }
149 
155  void setMarkExcess(int16_t markExcess_) {
156  markExcess = markExcess_;
157  }
158 
164  int16_t getMarkExcess() const {
165  return markExcess;
166  }
167 };
bool timeouted
True if last receive ended with a timeout.
Definition: IrReader.h:47
virtual void setBeginningTimeout(milliseconds_t timeOut)
Definition: IrReader.h:138
static const milliseconds_t defaultBeginningTimeout
Definition: IrReader.h:33
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:32
static const milliseconds_t defaultEndingTimeout
Definition: IrReader.h:34
unsigned int getBufferSize() const
Definition: IrReader.h:146
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
virtual void enable()
Start reception of IR data.
Definition: IrReader.h:81
virtual milliseconds_t getBeginningTimeout() const
Definition: IrReader.h:142
virtual void disable()
Stop reception of IR data.
Definition: IrReader.h:87
size_t bufferSize
Definition: IrReader.h:41
uint16_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:25
virtual void dump(Stream &stream) const
Prints a textual representation of the received data to the Stream supplied.
Definition: IrReader.cpp:21
IrReader()
Definition: IrReader.h:60
Abstract base class for all IR readers, capturing or receiving.
Definition: IrReader.h:30
virtual size_t getDataLength() const =0
Returns the number of collected durations.
virtual void setEndingTimeout(milliseconds_t timeOut)
Definition: IrReader.h:130
virtual milliseconds_t getEndingTimeout() const
Definition: IrReader.h:134
milliseconds_t endingTimeout
Definition: IrReader.h:39
IrSequence * toIrSequence() const
Generates an IrSequence from the IrReader.
Definition: IrReader.cpp:32
IrReader(size_t bufSize_)
Constructs an IrReader with buffersize bufSize_, possibly increased to be even.
Definition: IrReader.h:57
virtual frequency_t getFrequency() const =0
Returns frequency of received signal.
int16_t getMarkExcess() const
Gets the markExcess, a number (possibly negative) to be subtracted from the on-durations and added to...
Definition: IrReader.h:164
virtual microseconds_t getDuration(unsigned int index) const =0
Returns the index-th duration, if possible.
milliseconds_t beginningTimeout
Definition: IrReader.h:38
This class consists of a vector of durations.
Definition: IrSequence.h:11
static const size_t defaultCaptureLength
Definition: IrReader.h:35
virtual bool isReady() const =0
Returns true if there is collected data.
This file defines some general data types that are used in the library.
virtual ~IrReader()
Definition: IrReader.h:63
static unsigned int forceEven(unsigned int x)
Definition: IrReader.h:49
virtual void reset()
Definition: IrReader.h:67
int16_t markExcess
Microseconds subtracted from pulses and added to gaps.
Definition: IrReader.h:44
virtual bool isEmpty() const
Definition: IrReader.h:126
virtual void receive()=0
Convenience function: enable, wait until data is collected or timeout has occured,...