/**********************************************************************//**
 * @ingroup test_unit_priv
 * @brief Test of function: calculate_temperature(uint8_t MSB, uint8_t LSB) in tc72.c
 * @return char* - if test pass: 0
			     - if test fail: the error code line where it fails
 * @note Test parameters is taken from tc72 datasheet
 *************************************************************************/
static char* test_tc72_calc(void)
{
	mu_assert( "Err-01-100", calculate_temperature(0b11001001, 0b00000000) == -55.00 );
	mu_assert( "Err-01-101", calculate_temperature(0b11100111, 0b00000000) == -25.00 );
	mu_assert( "Err-01-102", calculate_temperature(0b11111111, 0b11000000) == -0.25 );
	mu_assert( "Err-01-103", calculate_temperature(0b00000000, 0b00000000) == 0.00 );
	mu_assert( "Err-01-104", calculate_temperature(0b00000000, 0b01000000) == 0.25 );
	mu_assert( "Err-01-105", calculate_temperature(0b00000000, 0b10000000) == 0.50 );
	mu_assert( "Err-01-106", calculate_temperature(0b00011001, 0b00000000) == 25.00 );
	mu_assert( "Err-01-107", calculate_temperature(0b01111101, 0b00000000) == 125.00 );

	return 0;
}
void main(void)
	{
	//Stops Watchdog
	WDTCTL = WDTPW | WDTHOLD;

	//Calibrate timer
	if(CALBC1_1MHZ==0xFF)
	{
		while(1);
	}
	DCOCTL = 0;
	BCSCTL1 = CALBC1_1MHZ;
	DCOCTL = CALDCO_1MHZ;

	//Timer 0
	TA0CCTL0 = CCIE;
	TA0CCR0 = 32768;
	TA0CTL = TASSEL_1 + MC_1;

	//Enable pushbutton S2
    P1DIR &= ~S2; // pushbutton is configured as input
    P1IE |= S2;   // port interrupt enabled
    P1OUT |= S2;
    P1IES &= ~S2;  // interrupt happens when button is released
    P1REN |= S2;  // to avoid some weird toggling (and LED blinking) when touching the P1.3
    P1IFG &= ~S2;

    //Enable red and green LED
    LED_DIR = LED_1; //LEDs are set as output
    LED_OUT &= ~(LED_1); //LEDs are turned off

    //Enable ports for UART connection
	P1SEL  |= BIT1 + BIT2;	 	//Enable module function on pin
	P1SEL2 |= BIT1 + BIT2;  	//Select second module; P1.1 = RXD, P1.2=TXD

	//Enable clock
	UCA0CTL1 |= UCSSEL_2;    	// Set clock source SMCLK
	UCA0BR0 = 104;           	// 1MHz 9600 (N=BRCLK/baudrate)
	UCA0BR1 = 0;             	// 1MHz 9600
	UCA0MCTL = UCBRS0;       	// Modulation UCBRSx = 1
	UCA0CTL1 &= ~UCSWRST;    	// **Initialize USCI state machine**

	//Enable motor
    P2DIR = 0x3C; // Allow P2 pins 2 to 5 to output for motor and
    P2OUT = 0x3C; // Disable everything.

	IE2 |= UCA0RXIE;         	//enable RX interrupt

	__enable_interrupt();

	while(1)
	{
		__bis_SR_register(GIE);


		if(clean_tank == 1)
		{
			//Filter reminders
			P1OUT |= LED_1;			//Kuidas initializeda ja kinni panna led ilma toggleita
			clean = 0;
			clean_tank = 0;
		}

		if(feed_fishes)
		{
			//Motor operations
			enable_motor(feed);
			feed_fishes = 0;
		}

		if(send_data)
		{
			//start bit (255)
			model_output = 0xFF;
			send_systemdata((int) (model_output));

			//light (0/1)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_0 + ADC10DIV_3;	// Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			model_output = calculate_lightlevel(adc_value);
			send_systemdata((int) (model_output));

			//water (0/1)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_7 + ADC10DIV_3;	// Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			model_output = calculate_waterlevel(adc_value);
			send_systemdata((int) (model_output));

			//feed (0/1)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_5 + ADC10DIV_3;	// Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			model_output = calculate_feedlevel(adc_value);
			send_systemdata((int) (model_output));

			//temperature value (0..40)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_4 + ADC10DIV_3;	// Temp Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			__delay_cycles(3000);	// settle time 30ms
			model_output = calculate_temperature(adc_value);
			send_systemdata((int) (model_output));

			//temperature state (0/1/2)
			temp_state = temperature_state(model_output);
			send_systemdata((int) (temp_state));

			//Filter cleaning (0/1)
			send_systemdata((int) (clean));

			//exit send_data
			send_data = 0;
		}
	}
}