#include <msp430.h> 

#include "./HAL/HAL_PMM.h"
#include "./HAL/HAL_PMAP.h"

#include "DelayTimer.h"
#include "TemperatureSensorCircuitry.h"
#include "LedIndicatorTimer.h"
#include "Radio.h"

#define	PACKET_LEN		(0x06)
#define	PAN_SERIENUMBER	(0x1337)
unsigned char TxPacket[PACKET_LEN+1]= {PACKET_LEN, ((PAN_SERIENUMBER >> 8) & 0xFF), ((PAN_SERIENUMBER >> 0) & 0xFF), 0x00, 0x00, 0x00, 0x00};

// unused pin are set to analog because:
// Analog disables the output driver and the input Schmitt-trigger to prevent parasitic cross currents when applying
// analog signals
const static uint8_t portmapping[] =
{
	// Port P1:
		PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	// Pin 0 -> 3
		PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	// Pin 4 -> 7
	// Port P2:
		PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	// Pin 0 -> 3
		PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	// Pin 4 -> 7
	// Port P3:
		PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	PM_ANALOG,	// Pin 0 -> 3
		PM_NONE,	PM_NONE,	PM_NONE,	PM_ANALOG	// Pin 4 -> 7
};


int main(void)
{
	uint16_t RawTemperatures[2];
	uint8_t CycleCount = 0;

    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
    UCSCTL4 |= SELA__VLOCLK;	// Set VLO as auxiliary clock source

    // Set Vcore to a minimum of level 2 to support the radio
    HAL_PMM_SetVCore(2);

    // Initialize delay timer
    DelayTimer_Init();

    // Initialize Led Indicator timer
    LedIndicatorTimer_Init();

    // Initialize GPIO
    /* Pre-setup all gpio ports */														// This is done to reduce current consumption
	PADIR = 0x00;																		// Set to input;
	PAOUT = 0x00;																		// Set to logic low

	PBDIR = 0x00;																		// Set to input;
	PBOUT = 0x00;																		// Set to logic low

	PCDIR = 0x00;																		// Set to input;
	PCOUT = 0x00;																		// Set to logic low

	P2DIR &= ~(BIT2 | BIT3);															// Set P2.2 and P2.5 to input
	P2DIR |= (BIT6);																	// Set P2.6 to output
	P3DIR |= (BIT4 | BIT6);																// Set P3.4 and P3.6 to output

    // Set Port Mapping
	HAL_PMAP_MAPIO(portmapping, (uint8_t *) &P1MAP0, 3, 0);

	// Set Pin Function Select
	P1SEL = 0x00;
	P2SEL = BIT2 | BIT5;
	P3SEL = 0x00;

	// Initialize Temperature sensor circuit
	TSC_Init();
    // Initialize Led indicator timer

    // Initialize Radio
    Radio_Init();

    // Wait for the energy harvesting to acquire enough charge.
    DelayTimer_Wait(250);

    while(1)
    {
    	// Do temperature measurement
    	TSC_Measure(&RawTemperatures[0], &RawTemperatures[1]);

        // Wait for the energy harvesting to acquire enough charge.
        DelayTimer_Wait(250);

        // Write temperatures to Tx packet
        TxPacket[3] = (RawTemperatures[0] >> 8);
        TxPacket[4] = (RawTemperatures[0] >> 0);
        TxPacket[5] = (RawTemperatures[1] >> 8);
        TxPacket[6] = (RawTemperatures[1] >> 0);
    	// Transmit temperatures
    	Radio_Transmit((uint8_t *)TxPacket, sizeof(TxPacket));

    	// Enable Led indicator
    	if (CycleCount == 0)
    		LedIndicatorTimer_Enable();

    	// increase cycle counter
    	CycleCount = (++CycleCount) % 5;

        // Wait for next measurement
        DelayTimer_Wait(750);
    }
}
