/***************************************************************************
 *   Copyright (C) 28.7.2004 by                                            *
 *   Matthias Kranz (matthias.kranz@ifi.lmu.de)                            *
 *   Paul Holleis (paul.holleis@ifi.lmu.de)                                *
 *   Marion Gall                                                           *
 *                                                                         *
 *   header file for adxl311je digital 2g accelerometer.                   *
 *   Last modified: 21.10.2004                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 **************************************************************************/
/******************************************************************************
 * defines
 *****************************************************************************/
#ifndef __STD
	#include <std.h>
#endif

#ifndef _LIMITS
	#include <limits.h>
#endif

#ifndef __ADXL202JE
  #define __ADXL202JE
#endif

#define OVERFLOW ULONG_MAX
#define MAX_VALUE ULONG_MAX

/// Pin the x-axis out is connected to, <em>PIN_B6</em>.
#define PIN_ADXL_XOUT PIN_B6//<- WAR B6
/// Pin the y-axis out is connected to, <em>PIN_B5</em>..
#define PIN_ADXL_YOUT PIN_B5//<- WAR B5
/// Pin the z-axis out is connected to, <em>PIN_B3</em>..
#define PIN_ADXL_ZOUT PIN_B3//<- WAR B3
/// Pin the u-axis out is connected to, <em>PIN_B4</em>..
#define PIN_ADXL_UOUT PIN_B4//<- WAR B4

/******************************************************************************
 * global variables
 *****************************************************************************/

/******************************************************************************
 * function signatures
 *****************************************************************************/
// ACCELEROMETER FUNCTIONS
unsigned long get_accelerationX();
unsigned long get_accelerationY();
unsigned long get_accelerationZ();
unsigned long get_accelerationU();
unsigned int get_accX();
unsigned int get_accY();
unsigned int get_accZ();
unsigned int get_accU();

/******************************************************************************
 * Code
 *****************************************************************************/
/*
 *
 *
 */
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;
}

/*
 *
 *
 */
unsigned int get_accZ()
{
	unsigned long a;
	unsigned int as;
	a=get_accelerationZ();
	as = make8(a,1);
	return as;
}

/*
 *
 *
 */
unsigned int get_accU()
{
	unsigned long a;
	unsigned int as;
	a=get_accelerationU();
	as = make8(a,1);
	return as;
}

/*
 *
 *
 */
unsigned long get_accelerationX()
{
	unsigned long x_t1,x_t3,scaledacc,counter;
	float val;
	counter =0;
	//restart_wdt();
	// wait for adx = low
	while( input(PIN_ADXL_XOUT) &&(counter<OVERFLOW) ) {++counter;};	
	counter =0;
	//restart_wdt();
	// wait for adx = high
	while( !input(PIN_ADXL_XOUT)&&(counter<OVERFLOW) ) {++counter;};	
	x_t1=0;		
	//restart_wdt();				
	// count duration
	while( input(PIN_ADXL_XOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};	
	x_t3=0;
	//restart_wdt();
	// count duration
	while( !input(PIN_ADXL_XOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};	
	if ( (x_t1==OVERFLOW) || (x_t1==OVERFLOW) )
	{
		return 0;
	}
	//now scale the results (could be better - but this is small)
	/////// NEU NEU NEU
	x_t3=x_t3+x_t1;
	val = (((float)x_t1 / (float)x_t3)-0.5);
	////////fprintf(STDSTREAM, "%1.2f x3 = %li, x1 = %li\n", val, x_t3,x_t1);
	// max return value
	counter=MAX_VALUE; 		
	scaledacc=0;
	//restart_wdt();
	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;
	//restart_wdt();
	// wait for adx = low
	while( input(PIN_ADXL_YOUT) &&(counter<OVERFLOW) ) {++counter;};	
	counter =0;
	//restart_wdt();
	// wait for adx = high
	while( !input(PIN_ADXL_YOUT)&&(counter<OVERFLOW) ) {++counter;};	
	x_t1=0;		
	//restart_wdt();				
	// count duration
	while( input(PIN_ADXL_YOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};	
	x_t3=0;
	//restart_wdt();
	// count duration
	while( !input(PIN_ADXL_YOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};	
	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;
	//restart_wdt();
	// max return value
	counter=MAX_VALUE; 
	scaledacc=0;
	while (counter>x_t3)
	{
		scaledacc=scaledacc+x_t1;
		counter=counter-x_t3;
	}
	return(scaledacc);
}

/*
 *
 *
 */
unsigned long get_accelerationZ()
{
	unsigned long x_t1,x_t3,scaledacc,counter;
	counter =0;
	//restart_wdt();
	// wait for adx = low
	while( input(PIN_ADXL_ZOUT) &&(counter<OVERFLOW) ) {++counter;};	
	counter =0;
	//restart_wdt();
	// wait for adx = high
	while( !input(PIN_ADXL_ZOUT)&&(counter<OVERFLOW) ) {++counter;};	
	x_t1=0;		
	//restart_wdt();				
	// count duration
	while( input(PIN_ADXL_ZOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};	
	x_t3=0;
	//restart_wdt();
	// count duration
	while( !input(PIN_ADXL_ZOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};	
	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;
	//restart_wdt();
	// max return value
	counter=MAX_VALUE;
	scaledacc=0;
	while (counter>x_t3)
	{
		scaledacc=scaledacc+x_t1;
		counter=counter-x_t3;
	}
	return(scaledacc);
}

/*
 *
 *
 */
unsigned long get_accelerationU()
{
	unsigned long x_t1,x_t3,scaledacc,counter;
	counter =0;
	//restart_wdt();
	// wait for adx = low
	while( input(PIN_ADXL_UOUT) &&(counter<OVERFLOW) ) {++counter;};	
	counter =0;
	//restart_wdt();
	// wait for adx = high
	while( !input(PIN_ADXL_UOUT)&&(counter<OVERFLOW) ) {++counter;};	
	x_t1=0;		
	//restart_wdt();				
	// count duration
	while( input(PIN_ADXL_UOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};	
	x_t3=0;
	//restart_wdt();
	// count duration
	while( !input(PIN_ADXL_UOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};	
	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;
	//restart_wdt();
	// max return value
	counter=MAX_VALUE; 
	scaledacc=0;
	while (counter>x_t3)
	{
		scaledacc=scaledacc+x_t1;
		counter=counter-x_t3;
	}
	return(scaledacc);
}


