#include "core.c"
#include "sensor.c"

main() {
	short int passiveIrVal, touchVal;    // 1bit value for pasive infrared and touch
	long lightVal1, lightVal2, 
	               accelXVal, accelYVal; // 16bit values for light and acceleration
	int tempVal;	    		     // 8 bit value for temperature
	long counter;		 	     // counter
	
	unsigned int accXVal, accYVal; 	     // 8bit acceleration values
	
	// setup memory and boot sign
	init_core();
	
	// reset loop counter
	counter=0; 
	
	// restart the watchdog timer (call at least evry 2 sec.)
   	restart_wdt();
 
 	// set up all analog inputs (A0..A4)
	setup_port_a( ALL_ANALOG );
   	setup_adc( ADC_CLOCK_INTERNAL );

	// main loop - forever
   	while(1) {
   		// restart watchdog timer
   		restart_wdt();
   		// increase the counter
 		counter++;
		
 		// leds on sensor board on
 		led_on1();
 		led_on2();
		
 		// reset the print buffer for sending via RF
	   	reset_rf_buffer();
	   	
	   	// print counter over serial and into RF-buffer
		printf("C%li\n", counter);
		printf(to_rf_buffer, "C%li\n", counter);

	   	// read passive infrared and print it over serial and into RF-buffer
 		passiveIrVal=PIR1();
		printf("P%i\n", passiveIrVal);
		printf(to_rf_buffer, "P%i\n", passiveIrVal);
		
		// read touch sensor and print it over serial and into RF-buffer
		touchVal=touch();
		printf("Q%i\n", touchVal);
		printf(to_rf_buffer, "Q%i\n", touchVal);


		// read light sensors and print it over serial and into RF-buffer
		lightVal1=light1();
		lightVal2=light2();
		printf("L%li:%li\n", lightVal1, lightVal2);
		printf(to_rf_buffer,"L%li:%li\n", lightVal1, lightVal2);
		
		// read acceration X and print it over serial and into RF-buffer
		accelXVal=get_accelerationX();
		printf("X%lu\n", accelXVal);
		printf(to_rf_buffer,"X%lu\n", accelXVal);
		
		// read acceration Y and print it over serial and into RF-buffer
		accelYVal=get_accelerationY();
		printf("Y%lu\n", accelYVal);
		printf(to_rf_buffer,"Y%lu\n", accelYVal);
		
		// read acceration X and print it over serial and into RF-buffer
		accXVal=get_accX();
		printf("X%u\n", accXVal);
		printf(to_rf_buffer,"X%u\n", accXVal);
		
		// read acceration Y and print it over serial and into RF-buffer
		accYVal=get_accY();
		printf("Y%u\n", accYVal);
		printf(to_rf_buffer,"Y%u\n", accYVal);
		
		
		// read temperature and print it over serial and into RF-buffer
		// introduces a 1 second delay!!!
		tempVal=temp();
		printf("T%i\n", tempVal);
		printf(to_rf_buffer,"T%i\n", tempVal);

		// print everything in the RF buffer
	   	RF_printf();

		// switch leds of - one after another
 		delay_ms(20);		
 		led_off1();
 		delay_ms(20);		
 		led_off2();

		// flash internal led
		led_on();
 		delay_ms(20);		
		led_off();

	} // main loop
}


