#include "RF_Keys.h"
enum
{
	RF_IDLE = 0,
	RF_SETUP = 10,
	RF_PREAMBLE = 20,
	RF_TX_CODE = 30,
	RF_TX_CODE_WAIT = 40,
	RF_TX_DONE = 1000
};

uint16_t rfTxState = RF_IDLE;
uint8_t *rfKeyPtr;
uint8_t rfKeyCount;
uint8_t rfIdx;
uint32_t rfTime;

void initRF()
{
	initCC1101();
}

void rfSendKey(uint8_t key, uint8_t channel)
{
	Serial.print("Key - ");
	Serial.print(key);
	Serial.print(" Ch - ");
	Serial.println(channel);
	if (rfKey.key == RF_NO_KEY) // make sure there are no pending keys
	{
		rfKey.key = key;
		rfKey.channel = channel;
	}
}

/*******************************************************************************

*******************************************************************************/
void rfTask()
{

	rfTxTask();

	switch (rfState)
	{
	case 0:
		if (rfKey.key != RF_NO_KEY)
			rfState = 100;
		break;

	case 100: // send a key
		if (rfTxState == RF_IDLE)
			rfTxState = RF_SETUP;
		rfState++;
		break;

	case 101:
		if (rfTxState == RF_IDLE)
			rfState = 0;
	}
}

/********************************************************************************
This task is to simulate a replicated transmission from a DD2702H transmitter

The CC1100 will enter asynchronous transmission mode.
Then a preamble (0xAA) will be transmitted for approximately 145mS
The CC1100 will then be put into packet mode.
A keypacket sequence will be transmitted.
	A down or up key will transmit 7 packets.
	A stop or prog key will transmit 3 packets.
	There is an 82mS delay between each packet.

When tranmission is complete, CC1101 will be put into idle mode.


*********************************************************************************/
void rfTxTask(void)
{
	long x = 0;
	byte b;

	switch (rfTxState)
	{
	case RF_IDLE:
		break;

	case RF_SETUP:
		switch (rfKey.key)
		{
		case RF_KEY_DN:
			rfKeyPtr = &DnKey[0][0] + (rfKey.channel * 9 * 21);
			// Serial.println("RF Down Key");
			// Serial.print("Channel ");Serial.println(rfKey.channel);
			// Serial.println("Dn Key ");Serial.println((long unsigned int)&DnKey[0][0],HEX);
			// Serial.print("rfKeyPtr ");Serial.println((long unsigned int)rfKeyPtr,HEX);
			rfKeyCount = 9;
			break;

		case RF_KEY_UP:
			rfKeyPtr = &UpKey[0][0] + (rfKey.channel * 9 * 21);
			// Serial.println("RF Up Key");
			rfKeyCount = 9;
			break;

		case RF_KEY_STOP:
			rfKeyPtr = &StopKey[0][0] + (rfKey.channel * 3 * 21);
			// Serial.println("RF Stop Key");
			rfKeyCount = 3;
			break;

		case RF_KEY_PROG:
			rfKeyPtr = &ProgKey[0][0] + (rfKey.channel * 3 * 21);
			// Serial.println("RF Prog Key");
			rfKeyCount = 3;
			break;
		}
		rfTxState = RF_PREAMBLE;
		break;

	case RF_PREAMBLE:
		// async mode will be used to send a 140ms preamble
		SendCCData(0x08, 0x30); // async mode
		ccTxOn();
		ccTx.write(0x2d);
		ccTx.write(0xd4);
		ccTx.write(0x2d);
		ccTx.write(0xd4);
		while (++x < 500)
		{
			ccTx.write(0xAA);
		}
		// switch to packet mode to send key codes.
		// this will allow CC1100 to calculate the CRC and append to packet
		SendCCData(0x08, 0x04); // packet mode, fixed length, crc enabled
		rfIdx = 0;
		rfTxState = RF_TX_CODE;
		break;

	case RF_TX_CODE:
		SendCCCommand(CC_CMD_TX_FLUSH); // flush Tx FIFO
		for (b = 0; b < 21; b++)
		{
			RFTXArray[b] = *rfKeyPtr++;
			//				Serial.print(RFTXArray[b],HEX);Serial.print(" ");
		}
		//			Serial.println(" ");
		SendCCDataBurst(CC_CMD_FIFO, RFTXArray, 21);
		ccTxOn();
		rfTime = millis();
		while (GetCCData(CC_STATUS_MSTATE) != 1)
		{
			if (millis() - rfTime > 20)
			{
				//					Serial.println("RF Timeout");
				rfTxState = RF_IDLE;
				break;
			}
		}
		rfIdx++;
		rfTime = millis();
		rfTxState = RF_TX_CODE_WAIT;
		break;

	case RF_TX_CODE_WAIT:
		if (rfIdx >= rfKeyCount)
		{
			rfTxState = RF_TX_DONE;
		}
		else
		{
			if ((millis() - rfTime) > 82)
			{
				rfTxState = RF_TX_CODE;
			}
		}
		break;

	case RF_TX_DONE:
		rfKey.key = RF_NO_KEY;
		rfKey.channel = 0;
		rfTxState = RF_IDLE;
		break;
	}
}
