     }
 }
 
+/**
+ * Reads a command from the serial line and executes it-
+ */
 void loop() {
     String line = readCommand(Serial);
     processCommand(line, Serial);

/*
This is a minimalistic Girs server (http://harctoolbox.org/Girs.html).
It only depends on (a subset of) IRremote
(https://github.com/z3t0/Arduino-IRremote), It can be used with
IrScrutinizer (Sending/Capturing hw = Girs Client) and Lirc 0.9.4 and
later (driver girs http://lirc.org/html/girs.html).  (Authors of
similar software are encourage to implement support.)

It consists of an interacive IR server, taking one-line commands from
the "user" (which is typically not a person but another program), and
responds with a one-line response. In the language of the Girs
specifications, the modules "base", receive, and transmit are
implemented. (The two latter can be disabled by not defining two
corresponding CPP symbols.)

It understands the following commands:

* The "version" command returns the program name and version, 
* The "modules" command returns the modules implemented, normally base, receive and transmit.
* The "receve" command reads an IR signal using the deployed sensor.
* The "send" commands transmits a supplied raw signal the requested number of times.
        
Only the first character of the command is evaluated in this implementation.
        
The "receive" command returns the received IR sequence as a sequence
of durations, including a (dummy) trailing silence.  On-periods
("marks", "flashes") are prefixed by "+", while off-periods ("spaces",
"gaps") are prefixed by "-". The present version never times out.
        
The "send" command takes the following parameters:
                
      send noSends frequencyHz introLength repeatLength endingLength durations...
                
      where 

* frequencyHz denotes the modulation frequency in Hz
  (NOT khz, as is commonly used in IRremote)
* introLength denotes the length of the intro sequence, must be even,
* repeatLength denotes the length of the repeat sequence, must be even,
* endingLength denotes the length of the ending sequence (normally 0), must be even.
* duration... denotes the microsecond durations to send, 
  starting with the first on-period, ending with a (possibly dummy) trailing silence
                
Semantics: first the intro sequence will be sent once (i.e., the first
repeatLength durations) (if non-empty).  Then the repeat sequence will
be sent (noSends-1) times, unless the intro sequence was empty, in
which case it will be send noSends times.  Finally, the ending
sequence will be send once (if non-empty).
                        
Weaknesses of the IRremote implementation:
* Reception never times out.
* The IRrecv class does its own decoding which is irrelevant for us. It cannot be disabled.
* The timeout at the end on a signal reception is not configurable.
  For example, a NEC1 type signal will cut after the intro sequence,
  and the repeats will be considered independent signals.
* IR reception cannot be turned of. 
* The size of the data is platform dependent (unsigned int).
*/

#include <IRremote.h>
#include <limits.h>

// Define the ones that should be enabled
#define RECEIVE
#define TRANSMIT

// Change the following if desired

// Pin used by the receiver
#define INPUTPIN 11

// (The sending pin is in general not configurable, see the documentation of IRremote.)

// Character that ends the command lines. Do not change unless you known what
// you are doing. IrScrutinizer and Lirc expects \r.
#define EOLCHAR '\r'

// Baud rate for the serial/USB connection.
// 115200 is the default for IrScrutinizer and Lirc.
#define BAUDRATE 115200

////// End of user configurable variables ////////////////////

#define modulesSupported "base transmit receive"
#ifndef PROGNAME
#define PROGNAME "MicroGirs"
#endif
#ifndef VERSION
#define VERSION "2017-04-23"
#endif
#define okString "OK"
#define errorString "ERROR"
#define timeoutString "."

// For compatibility with IRremote, we deliberately use
// the platform dependent types.
typedef unsigned frequency_t;
typedef unsigned microseconds_t;

static const microseconds_t DUMMYENDING = 40000U;
static const frequency_t FREQUENCY_T_MAX = UINT16_MAX;
static const frequency_t MICROSECONDS_T_MAX = UINT16_MAX;

#ifdef RECEIVE
IRrecv irRecv(INPUTPIN);
#endif
#ifdef TRANSMIT
IRsend irSend;
#endif

///////// Tokenizer class. Breaks the command line into tokens. //////
class Tokenizer {
private:
    static const int invalidIndex = -1;


#ifdef RECEIVE
        case 'r': // receive
        //case 'a':
        //case 'c':
            receive(stream);
            break;
#endif // RECEIVE
