#ifndef RawIR_h
#define RawIR_h

// AardRemote raw IR data format.


// Both IR recieve and transmit routines use the following data format
// (though status will only be reported by the receiver, never sent)

// A single raw IR MARK or SPACE (or status) is stored as a 16 bit
// unsigned integer.
//
// The format of the unsigned int is pretty simple: The high 2 bits are a
// flag indicating the meaning of the low 14 bits:
//
// 0x4000 - low 14 bits are a mark duration
// 0x8000 - low 14 bits are a space duration
//
// The units for duration are microsecond/4.
//
// If the duration is greater or equal to 16383 then 16383 is used
// (and that is an unlikely value for an IR pulse, they should all
// be much shorter than that).
//
// The format of a status word returned by the recieve IR routines is:
//
// 0xc000 - low 14 bits are a status bitmask:
//          0x0001 - timeout at end of IR sequence
//          0x0002 - data lost due to buffer overflow
//          0x0004 - data lost due to duration overflow
//          0x0008 - had to disable input capture interrupt for a while

// Define macros to use to interpret the mark/space data format

#define BGIR_DURATION(v)     ((v) & 0x3fffu)
#define BGIR_MARK            0x4000u
#define BGIR_SPACE           0x8000u
#define BGIR_STATUS          0xc000u
#define BGIR_FLAG_BITS(v)    ((v) & 0xc000u)
#define BGIR_IS_MARK(v)      (BGIR_FLAG_BITS(v) == BGIR_MARK)
#define BGIR_IS_SPACE(v)     (BGIR_FLAG_BITS(v) == BGIR_SPACE)
#define BGIR_IS_STATUS(v)    (BGIR_FLAG_BITS(v) == BGIR_STATUS)
#define BGIR_END             0x0001u
#define BGIR_LOST_DATA       0x0002u
#define BGIR_LOST_TIME       0x0004u
#define BGIR_DISABLED        0x0008u
#define BGIR_IS_END(v)       ((v) & BGIR_END)
#define BGIR_IS_LOST_DATA(v) ((v) & BGIR_LOST_DATA)
#define BGIR_IS_LOST_TIME(v) ((v) & BGIR_LOST_TIME)
#define BGIR_IS_DISABLED(v)  ((v) & BGIR_DISABLED)


#endif // RawIR_h
