/*
rf433.h - Arduino Library for RF433.
Copyright 2013-2014 - Laurent Menu-Kerforn
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef rf433t_h
#define rf433t_h

#define _RF_DEFAULTEMITPIN 5
#define _RF_MINSIGTICKS 50
#define _RF_MINRXTRANS 16
#define _RF_RXPIN 8 //Input Capture ICP1 
#define _RF_MAXTRANS 64
#define _RF_MAXRX 256
#define _RF_TIMER1

#ifdef _RF_TIMER1
#define _RF_TCCRA TCCR1A
#define _RF_TCCRB TCCR1B
#define _RF_TCNT TCNT1
#define _RF_OCRA OCR1A
#define _RF_OCFA OCF1A
#define _RF_OCRB OCR1B
#define _RF_TIMSK TIMSK1
#define _RF_TIFR TIFR1
#define _RF_TX_vect TIMER1_COMPA_vect
#define _RF_WGM2 WGM12
#define _RF_CS2 CS12
#define _RF_CS1 CS11
#define _RF_CS0 CS10
#define _RF_OCIEA OCIE1A

#define _RF_RX_vect TIMER1_CAPT_vect
#define _RF_ICR ICR1
#define _RF_ICIE ICIE1
#define _RF_ICF ICF1
#define _RF_ICES ICES1
#define _RF_ICNC ICNC1
#define _RF_ACSR ACSR
#define _RF_ACIC ACIC
// desactivation analog comparator OFF
// ACIC à 0 dans ACSR

#endif

#include "Arduino.h"

struct _srftrans {
	byte level;
	word ticks;
	};
/* debug */
/*
extern unsigned long collect[];
extern int cc;
*/
/* fin debug */

typedef struct _srftrans _rftrans;

class rf433 {
public:
	rf433();
	word micro2ticks(unsigned long t);
	unsigned long ticks2micro(word ticks);
	virtual int reset();
	int init();
	int poweron();
	int poweroff();
	int setpowerpin(int pin);
	int setpowerledpin(int pin);
	int setpowerled(int level);
	int setactivityled(int level);
	int setactivityledpin(int pin);
	int setpower(int level);
protected:
	int activityled;
	int powerpin;
	int powerled;
};

class rf433t : public rf433
{
public:
	rf433t(int pin);
	int enableint();
	int slowit();
	int disableint();
	int repeatmode(int nloop);
	virtual int addheader();
	virtual int addtailer();
	int dump();
	int isr();
	int reset();
	int start();
	int isrunning();
	int stop();
	int bufferize(char *code);
	int emit(char *string);
	int setemitpin(int pin);

protected:
 
	int emitpin;
	volatile int transindex;
	int transcount;
	volatile int stopit;
	volatile int status;	
	int repeat; // 0: infini
	volatile int repeatcount;
	int resetemit();
	int recbit(int level,int count);
	int recus(int level,unsigned long us);
	//durées exprimées en ticks dans le tableau suivant 
	//depend du prescale choisi
	_rftrans trans[_RF_MAXTRANS];
	//tout est exprimé en micro-secondes dans les tableaux suivants
	int ordor[2];
	unsigned int coeff[2];
};

class rf433r : public rf433
{
public:
	rf433r();
	int setminsiglen(long us);
	int setminrxtrans(int trans);
	int enableint();
	int disableint();
	int dump();
	int isr();
	int reset();
	int start();
	int isrunning();
	int bufferfull();
	int stop();
	int bufferize(char *code);
 	_rftrans trans[_RF_MAXRX];
	volatile int rxindex;

protected:
	long minsiglen;
	int minrxtrans;
	int resetemit();
};

#endif

