Menu
Permalink
Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign upClick here to download the wire.h library. Arduino VirtualWire Library Click here to download: VirtualWire.h VirtualWire is an Arduino library that provides features to send short messages, without addressing, retransmit or acknowledgment, a bit like UDP over wireless, using ASK (amplitude shift keying).
Find file Copy path
Cannot retrieve contributors at this time
/* |
Streaming.h - Arduino library for supporting the << streaming operator |
Copyright (c) 2009 Mikal Hart. All rights reserved. |
modifed to support floating point precision - mem 20 Feb 2010 |
This library is free software; you can redistribute it and/or |
modify it under the terms of the GNU Lesser General Public |
License as published by the Free Software Foundation; either |
version 2.1 of the License, or (at your option) any later version. |
This library is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
Lesser General Public License for more details. |
You should have received a copy of the GNU Lesser General Public License |
along with GLCD. If not, see <http://www.gnu.org/licenses/>. |
*/ |
#ifndef ARDUINO_STREAMING |
#defineARDUINO_STREAMING |
#include<WProgram.h> |
#defineSTREAMING_LIBRARY_VERSION3 |
// Generic template |
template<classT> |
inline Print &operator <<(Print &stream, const T arg) |
{ stream.print(arg); return stream; } |
/// @cond hide_from_doxygen |
struct_BASED |
{ |
long val; |
int base; |
_BASED(long v, int b): val(v), base(b) |
{} |
}; |
struct_FLOAT |
{ |
float val; |
int digits; |
_FLOAT(double v, int d): val(v), digits(d) |
{} |
}; |
/// @endcond |
#define_HEX(a) _BASED(a, HEX) |
#define_DEC(a) _BASED(a, DEC) |
#define_OCT(a) _BASED(a, OCT) |
#define_BIN(a) _BASED(a, BIN) |
#define_BYTE(a) _BASED(a, BYTE) |
// Specialization for class _BASED |
// Thanks to Arduino forum user Ben Combee who suggested this |
// clever technique to allow for expressions like |
// Serial << _HEX(a); |
inline Print &operator <<(Print &obj, const _BASED &arg) |
{ obj.print(arg.val, arg.base); return obj; } |
// Specialization for class _FLOAT |
inline Print &operator <<(Print &obj, const _FLOAT &arg) |
{ obj.print(arg.val, arg.digits); return obj; } |
// Specialization for enum _EndLineCode |
// Thanks to Arduino forum user Paul V. who suggested this |
// clever technique to allow for expressions like |
// Serial << 'Hello!' << endl; |
enum _EndLineCode { endl }; |
inline Print &operator <<(Print &obj, _EndLineCode arg) |
{ obj.println(); return obj; } |
#endif |
Copy lines Copy permalink
Font size:
- API
Date | Description |
---|---|
2016-05-04 | Initial version. |
Introduction
This is yet another infrared library for the Arduino. (Although its name does notstart with YA...) It is a major rewrite of Chris Young's IRLib, (GitHub repo), which itself is a major rewrite of a library called IRremote, published by Ken Shirriff in his blog, now maintained on GitHub.It uses Michael Dreher'sIrWidget (article in German), see also this forum contribution.
The classes IrWidget and IrWidgetAggregating are based on Michael's code.The classes IrReceiverSampler and IrSenderPwm, and in particular the file IRremoteInt.h,are adapted from Kevin's and Chris' work. The remaining files are almost completely writtenfrom scratch, although the influence of Kevin and Chris is gratefully acknowledged.
This work is a low-level library (like IRremote and IRLib) that can be used in other projects, like AGirs, which is an high-level program taking commands interactivelyfrom a user or a program through a bi-directional Stream.The goal has been to provide a sound, object oriented basis for the fundamental basis, notto provide maximal functionality, the maximal number of protocols supported, or the most complete support ofdifferent hardware. A clean design and high readability, without being 'too' inefficient,has been the highest priority. Dynamic memory allocation with new and delete is used extensively.The user who is afraid of this can create his required objects at the start of the run, and keepthem. Most classes are immutable. The classes are const-correct.
API
Types
There are some project specific data typedefs in InfraredTypes.h.For durations in microseconds, the data type microseconds_t is to beused. If desired/necessary, this can be either uint16_t oruint32_t. For durations in milliseconds, use the typemillisecons_t. Likewise, use frequency_t for modulation frequency inHz (not kHz as in the IRremote/IRLib).
For 'sizes', size_t, the standard C type, is used.
Implementation dependent types like int are used if and only if it is OK for thecompiler to select any implementation allowed by the C++ language.
unsigned int is used for quantities that can 'impossibly' be larger than 65535.
IrSequences and IrSignals
An IrSequence is a vector of durations, i.e. sequence of interleaving gaps and spaces. It does notcontain the modulation frequence. As opposed to IRremote and IRLib, our sequences always start witha space and end with a gap. It is claimed to be a more relevant representation than the one of IRremote and IRLib.
An IrSignal consists of a modulation frequency and three IrSequences: intro-, repeat-, and ending sequence. All of these, but not all, can beempty. If repeat is empty, intro has to be non-empty and ending empty. The intro sequence is always sent first,then comes a zero or more repeat sequences, and finally the ending sequence. To send a signal n > 0 times shall meanthe following: If the intro is non-empty, send intro, n - 1 repeats, and then the ending. If the intro is empty,send n repeats, and then then ending.
Class construction
For some receiving and transmitting classes, multiple instantiations are not sensible,for other it may be. In this library, the classes that should only be instantiatedonce are implemented as singleton classes, i.e. with no public constructor, but insteada static 'factory' method (newThing()) that delivers a pointer to a newly constructed instanceof Thing,provided that Thing has not been instantiated before. The classes, where multiple instancesis sensible, come with public constructors. (However, the user still has to take responsibilityfor avoiding pin- and timer-conflicts.)
Hardware configuration
For hardware support, the file IRremoteInt.h from the IRremote project is used. This means thatall hardware that project supports is also supported here (for IrReceiverSampler and IrSenderPwm).(Actually, a small fix, borrowed from IRLib, was usedto support Arduinos with ATMega32U4 (Leonardo, Micro).)However, IrWidgetAggregating is currently supported on the boards Uno/Nano (ATmega328P), Leonardo/Micro (ATmega32U4),and Mega2560 (ATmega2560).
Several of the sending and receiving classes take a GPIO pin as argument to the constructor.However, the sending pin of IrSenderPwm and the capture pin of IrWidgetAggregatingare not configurable, but (due to hardware limitations) have to be taken from the following table:
Sender Pin | Capture Pin | |
---|---|---|
Uno/Nano (ATmega328P) | 3 | 8 |
Leonardo/Micro (ATmega32U4) | 9 | 4 |
Mega2560 (ATmega2560) | 9 | 49 |
Timeouts
All the receiving classes adhere to the following conventions: When initialized, it waitsup to the time beginningTimeout for the first on-period. If not received within that period,it returns with a timeout. Otherwise, is starts collecting data. It will collect data until one of thefollowing occurs:
- A silence of length endingTimeout has been detected. This is the normal ending. The detected last gap is returned with the data.
- The buffer gets full. Reception stops.
User parameters
As opposed to other infrared libraries, there are no user changeable parameters as CPP symbols.However, the timerconfiguration is compiled in, depending on the CPP processors given to the compiler, see the file IRremoteInt.h.
Files
As opposed to the predecessor projects, this project has a header (*.h) file and an implementation file (*.cpp, sometimes missing) for each public class.
Error handling
Simple answer: there is none. If a function is sent erroneous data,it just silently ignores the request, or does something else instead. This (unfortunately) seems to be the standard procedure in Arduino programming.
I am used to exception based error handling, for some reason this is not used by the Arduino community.
Constructive suggestions are welcome.
Protocols
Comparing with the predecessor works, this project may look meager, currently supporting only two protocols (NEC1 and RC5). It is planned to generate the corresponding C++ code automatically from the IRP notation. (For this reason, contributed implementations of more protocols are not solicited.)
Sending non-modulated signals.
RF signals (433 MHz and other carrier frequencies) do not use the IRtypical modulation. Also there are a few IR protocols (like Revox, Barco, Archer) that donot using modulation. These signals can be sent by the class IrSenderNonMod,after connecting suitable hardware capable of sending non-modulated (IR- or RF-) signalsto the GPIO pin given as argument to the constructor.
Dependencies
This library does not depend on any other libraries; only the standard Arduino environment.
Questions and answers
What is the difference between the IrReceiver* and the IrWidget* classes?
They are intended for two different use cases, receiving and capturing.Differently put, 'receive' uses ademodulating receiver (TSOPxxx, etc.),'capture' a non-demodulating decoder (TSMPxxx, OPLxxx QSExxx, etc.). Note that this terminology is not universally accepted (yet!).
Coding style
My goal is to write excellent code, even though I do not always succeed :-).'Everything as simple as possible, but not simpler.'Cleanliness, logical structure, readability and maintainability are the most importantrequirements. Efficiency (runtime and/or space) is also important, although it normallycomes on second place. [The Arduino Style Guide](https://www.arduino.cc/en/Reference/APIStyleGuide)has different goals (essentially optimizing for novice programmers,'Some of these run counter to professional programming practice'). It is therefore not given priority in this project.
Documentation
The main documentation for the classes is found in the source files themselves. It can be extracted to a browse-able documentation using the program Doxygen.After installing the program, fire up the program in the source directory. It will generate documentation in a subdirectory apidoc. To browse, open apidoc/index.html in a browser.
The documentation is written for the user of the library, not the developer.For this reason, the file Arduino.h has been deliberately excluded from the documentation,to keep it centered on the main issues for the programming on the target system.
Multi platform coding
For someone used to, e.g., Netbeans or Eclipse, the Arduino IDEfeels 'somewhat' primitive and limited. In particular, it does not support debugging. Mainlyfor this reason, the code in the present library is designed to compile, and at leastto some extent, run in a normal C++ environment on the host compiler. For this,some code modifications, in particular, a customized Arduino.h was needed.If the preprocessor symbol ARDUINO is defined,just includes the standard Arduino Arduino.h is included, otherwise (i.e. for compiling for the host),some more-or-less dummy stuff are defined, allowing compiling for, and execution/debuggingon the host.
This way, certain types of problems can be solved much faster. The drawback is that the codeis 'polluted' with ugly #ifdef ARDUINO statements, which decreases readability andmakes maintenance harder.
The subdirectory tests contains test(s) that run on the host. The supplied Makefileis intended for compiling for the host as target. It creates a library in thestandard sense (*.a), and can be used to build and run tests in subdirectory tests.
With the provided Doxyfile, Doxygen will document only the (strict) Arduino parts,not the 'portable C++'.
License
The entire work is licensed under the GPL3 license. Chris' as well as Ken'scode is licensed under the LGPL 2.1-license. Michael's code carries theGPL2-license, although he iswilling to agree to 'or later versions'.