/** External defines for schip's Capture function library.
 *   Stolen from and based on the Arduinoish FreqMeasure library:
 *       http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
 *       Copyright (c) 2011 PJRC.COM, LLC - Paul Stoffregen <paul@pjrc.com>
 *       Version 1.0
 * Further copy wronged by M. Schippling 11/15 schip@etantdonnes.com
 * Version 1.0
 *
 * Note: Only ported to ATmega 328p on Uno and ProMini
 * 	 The rest is an exercise for the abuser, see capture.h.
 */
#ifndef MSCapture_h
#define MSCapture_h

#include <inttypes.h>

// number of elements to buffer
#define MSCAPTURE_BUFFER_LEN 32

class MSCaptureClass
{
public:
	static void begin( uint8_t prescale, uint8_t mode);
	static uint8_t available(void);
	static uint32_t read(void);
	static void end(void);
};

// arguments to begin():
// possible pre-scale values for timer -- capture resolutions
//  note: special values to match 328p TCCR1B register
//  	   pre-scale values in bits 0,1,2 positions
enum
{
	MSCck1	 	= 0x01,	// 16Mhz - 0.0625us
	MSCck8	 	= 0x02,	//  2Mhz - 0.5uS
	MSCck64		= 0x03,	// 250Khz - 4uS
	MSCck256	= 0x04,	//  62.5KHz - 16uS
	MSCck1024	= 0x05,	//  15.625Khz - 64uS
};

// possible modes -- Set by top two bits of mode byte: bits 6,7
//  note: special values to match 328p TCCR1B register
//  	   ICES1 edge setting in bit 6 position
enum
{
	MSCmPERIOD 	= 0xC0,	// b11000000 -- measure pulse period btw rising edges
	MSCmHPULSE 	= 0x40,	// b01000000 -- measure HIGH pulse length
	MSCmLPULSE	= 0x80,	// b10000000 -- measure LOW pulse length
	MSCmAPULSE	= 0x00,	// b10000000 -- measure alternating H/L pulse lengths
	MSCmEDGE	= 0x40,	// mask to get High/Low state of edge select
	MSCmMODE	= 0xC0,	// mask to get only MODE bits
};

// one and only one object of this class allowed, here it is:
extern MSCaptureClass MSCapture;

#endif // MSCapture_h
