/////////////////////////////////////////////////////////////////////////
////    Smart-Its Project				             ////
////  	www.smart-its.org  				             ////
/////////////////////////////////////////////////////////////////////////
////    Test program for the Vision Smart-It Version 0.05            ////
////    Checks sensors for changes and sends in the case of          ////
////    change a video for 1 second    				     ////
/////////////////////////////////////////////////////////////////////////
////    06 March 2002, by Albrecht Schmidt		             ////
////    Lancaster University                                         ////
////    http://www.comp.lancs.ac.uk/~albrecht/			     ////
/////////////////////////////////////////////////////////////////////////


// compiles with the CCS PCM C Compiler, Version 2.686, 7565  
// adc may be different in later version ...

#device adc=10
#include <16F876.H>
#include <STDLIB.H>
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT, NOLVP

#use delay(clock=20000000)



// for debug only
#use rs232(baud=115200,xmit=PIN_C2,rcv=PIN_C3)

#define LIGHT_THRESHOLD 100

print_version()
{
   printf("Version 0.05, build 0003 - 05/03/2002\r\n");
   printf("Albrecht Schmidt, Lancaster University\r\n");
   printf("http://www.comp.lancs.ac.uk/~albrecht/\r\n\r\n");
}


main() {

   long light, light0;
   // no suppot for bit arrays ... :(
   short int ball_0, ball_1, ball_2, ball_3, ball0_0, ball0_1, ball0_2, ball0_3;
   short int pir, pir0;
   
   boolean light_change, pir_change, ball_change, general_change;
   
   // switch off transmitter
   output_low(PIN_C6);
   // switch on camera
   output_high(PIN_C7);
     
   printf("booting vision Smart-It...\r\n");
   print_version();
   
   output_high(PIN_A1);
   delay_ms(200);
   output_high(PIN_A2);
   delay_ms(1000);
   output_low(PIN_A1);
   delay_ms(200);
   output_low(PIN_A2);
   delay_ms(500);
   output_high(PIN_A1);
   delay_ms(200);
   output_high(PIN_A2);

   // testing camera and transmitter
   // switch on transmitter
   output_high(PIN_C6);
   // transmit 3 sec
   delay_ms(3000);
   // switch off transmitter
   output_low(PIN_C6);
   output_low(PIN_A1);
   output_low(PIN_A2);   

   setup_port_a(RA0_ANALOG);
   setup_adc( ADC_CLOCK_INTERNAL );
   set_adc_channel( 0 );
   
   printf("\n\rVision Smart-It up and running...\r\n");

   // read the light
   light0 = Read_ADC();
   // get the passive IR 
   pir0 = input(PIN_B0);
   // get the ball switches
   ball0_0 = input(PIN_B1);
   ball0_1 = input(PIN_B2);
   ball0_2 = input(PIN_B3);
   ball0_3 = input(PIN_B4);
   
   while(1) {
	delay_ms(10);
   	// read the light
   	light = Read_ADC();
   	// get the passive IR 
   	pir = input(PIN_B0);
   	// get the ball switches
   	ball_0 = input(PIN_B1);
   	ball_1 = input(PIN_B2);
   	ball_2 = input(PIN_B3);
   	ball_3 = input(PIN_B4);

	printf("\r\nLight Reading: %lu %li %lx", light, light, light);  
	printf("\r\nPIR   Reading: %u",  pir);  	
	printf("\r\nBalls Reading: %u %u %u %u\r\n",ball_0, ball_1, ball_2, ball_3);
     	general_change = false;
	
	//compare light
	if (abs(light-light0) > LIGHT_THRESHOLD) {
		light_change = true;
		general_change = true;
	} else {
		light_change = false;
	}
	
	//compare PIR
	if (pir == pir0) {
		pir_change = false;
	} else {
		pir_change = true;
		general_change = true;
	}
	
	//compare balls
	ball_change = false;
	if ( (ball_0!=ball0_0 ) || (ball_1!=ball0_1 ) ||(ball_2!=ball0_2 ) ||(ball_3!=ball0_3 )) {
		ball_change = true;
		general_change = true;
	}
	
	if (light_change == true) {
		printf("\n\rLight Change!\n\r");
		output_high(PIN_A1);		
   	}
	
	if (pir_change == true) {
		printf("\n\rPIR Change!\n\r");
		output_high(PIN_A2);
	}
	
	if (ball_change == true) {
		printf("\n\rball Change!\n\r");
	}
	
	if (general_change == true) {
		printf("\n\rgeneral Change!\n\r");
		// switch on transmitter
   		output_high(PIN_C6);
   		// transmit 1 sec
   		delay_ms(1000);
   		// switch off transmitter
   		output_low(PIN_C6);
   		output_low(PIN_A2);
		output_low(PIN_A1);		
	}

	// save old value
	light0 = light;
	pir0 = pir;
	ball0_0 = ball_0;
	ball0_1 = ball_1;
	ball0_2 = ball_2;
	ball0_3 = ball_3;

  }
}
