// sensor board version 4

// access funtions to sensors:
// byte temp() -- reads temperature in degree celcius
// long light1()
// long light2() -- read the analog light values (0..1023)
// short int PIR() -- reads the current status of the passive IR sensor
// short int touch() -- reads the current status of the touch sensor
// unsigned long get_accelerationX()
// unsigned long get_accelerationY() -- read acceleration sensors
// unsigned int get_accX()
// unsigned int get_accY() -- read MSB of acceleration

// access to leds
// led_off1()
// led_off2()
// led_on1()
// led_on2()

#define OVERFLOW 65535
#define PIN_ADXL_XOUT PIN_B6
#define PIN_ADXL_YOUT PIN_B5
// updates id function...
void init_ds1621(byte id)
{	
	id<<1;
   	i2c_start();
   	i2c_write(0x90+(2*id));
   	i2c_write(0xac);
   	i2c_write(9);
   	i2c_stop();
//   	delay_ms(11);
}


byte read_temp_ds1621(byte id)
{        
	byte datah,datal;
   
	id<<1;
   	i2c_start();
	i2c_write(0x90+(2*id));
	i2c_write(0xee);
	i2c_stop();

	delay_ms(1000);
	restart_wdt();
	i2c_start();
	i2c_write(0x90+(2*id));
	i2c_write(0xaa);
	i2c_start();
   	i2c_write(0x91+(2*id));
   	datah=i2c_read();
   	datal=i2c_read(0);
   	i2c_stop();

     return(datah);
}


void led_on1()
{
	output_low(PIN_B2);
}
   
void led_off1()
{
	output_high(PIN_B2);
}


void led_on2()
{
	output_low(PIN_B3);
}
   
void led_off2()
{
	output_high(PIN_B3);
}

short int PIR()
{
	short int p;
	p=input(PIN_B4);
	return p;
}

short int PIR1()
{
	short int p;
	int i;
	for (i=0;i<100;i++) {
		p=input(PIN_B4);
		if (p==0) { return 0; };
		delay_ms(1);
	}
	return 1;
}


short int touch()
{
	short int p;
	p=input(PIN_B1);
	// input low (=0) when touched invert to 1
	if (p==0) return 1;
	 else return 0;
}

long light1()
{
	long tmp;
	set_adc_channel(0);
	delay_us(10);
	tmp = Read_ADC();
	return tmp;
}

long light2()
{
	long tmp;
	set_adc_channel(2);
	delay_us(10);
	tmp = Read_ADC();
	return tmp;
}


unsigned long get_accelerationX()
{
	unsigned long x_t1,x_t3,scaledacc,counter;
  	
	counter =0;
	while( INPUT(PIN_ADXL_XOUT) &&(counter<OVERFLOW) ) {++counter;};	// wait for adx = low
	counter =0;
	while( !INPUT(PIN_ADXL_XOUT)&&(counter<OVERFLOW) ) {++counter;};	// wait for adx = high
	x_t1=0;						
	while( INPUT(PIN_ADXL_XOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};	// count duration
	x_t3=0;
	while( !INPUT(PIN_ADXL_XOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};	// count duration

	if ( (x_t1==OVERFLOW) || (x_t1==OVERFLOW) )
	{
		return 0;
	}
	//now scale the results (could be better - but this is small)
	x_t3=x_t3+x_t1;

	counter=65535; 		//Maximum return value (comment by sfuchs)
	scaledacc=0;
	while (counter>x_t3)
	{
		scaledacc=scaledacc+x_t1;
		counter=counter-x_t3;
	}
	return(scaledacc);
}

unsigned long get_accelerationY()
{
	unsigned long x_t1,x_t3,scaledacc,counter;
  	
	counter =0;
	while( INPUT(PIN_ADXL_YOUT) &&(counter<OVERFLOW) ) {++counter;};	// wait for adx = low
	counter =0;
	while( !INPUT(PIN_ADXL_YOUT)&&(counter<OVERFLOW) ) {++counter;};	// wait for adx = high
	x_t1=0;						
	while( INPUT(PIN_ADXL_YOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};	// count duration
	x_t3=0;
	while( !INPUT(PIN_ADXL_YOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};	// count duration

	if ( (x_t1==OVERFLOW) || (x_t1==OVERFLOW) )
	{
		return 0;
	}
	//now scale the results (could be better - but this is small)
	x_t3=x_t3+x_t1;

	counter=65535; 		//Maximum return value (comment by sfuchs)
	scaledacc=0;
	while (counter>x_t3)
	{
		scaledacc=scaledacc+x_t1;
		counter=counter-x_t3;
	}
	return(scaledacc);
}

// takes about 1 second ...
byte temp()
{
	byte tmp;
	init_ds1621(6);
	delay_ms(10);
	tmp = read_temp_ds1621(6);
	return tmp;
}

unsigned int get_accX()
{
	unsigned long a;
	unsigned int as;
	a=get_accelerationX();
	as = make8(a,1);
	return as;
}

unsigned int get_accY()
{
	unsigned long a;
	unsigned int as;
	a=get_accelerationY();
	as = make8(a,1);
	return as;
}

