/***************************************************************************
 *   Copyright (C) 18.10.2004 by                                           *
 *   Matthias Kranz (matthias.kranz@ifi.lmu.de)                            *
 *   Paul Holleis (paul.holleis@ifi.lmu.de)                                *
 *                                                                         *
 *   header file for adxl311je analog 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
 *****************************************************************************/
/// std.h is needed for ADC_CHANNEL_x enumeration
#ifndef __STD
  #include <std.h>
#endif

#ifndef USE_PORT_A_ANALOG
	#define USE_PORT_A_ANALOG
#endif

#ifndef __ADXL311JE
  #define __ADXL311JE
#endif
/******************************************************************************
 * global variables
 *****************************************************************************/
/// The analog input pins on which the accelerometer is normally connected - may have to be changed accoring to current layout.
enum AXDL_PINS 
{
	AXDL_311_X_OUT = ADC_CHANNEL_0,
	ADXL_311_Y_OUT = ADC_CHANNEL_1
};

/******************************************************************************
 * function signatures
 *****************************************************************************/
long * read_adxl311();
long * read_adxl311_meanof(int number);

/******************************************************************************
 * Code
 *****************************************************************************/
// PIN on channel 0 and 3 are the same (connected by a wire)
// PIN on channel 1 and 4 are the same (connected by a wire)
/**
 * This function returns both adxl values at once. The duplicate
 * analog reads result from a small problem with the Smart-Its board
 * and because of this problem, a0 is hardwired to a2 and a1 is 
 * hardwired to a3. (analog inputs a0 to a8 are on the Smart-Its.)
 *
 * Call to this function:
 * long * array_calling_read_adxl311 = read_adxl311();
 * access to variables with: array_calling_read_adxl311[0] and 
 * array_calling_read_adxl311[1]
 * \return array of type long * containing the two long values of the two axis of the adxl.
 */
long * read_adxl311() 
{
  long xaxis,yaxis,xaxis2,yaxis2;
  long retval[2];
  set_adc_channel(AXDL_311_X_OUT);//a0
  delay_us(50);
  xaxis = read_adc(ADC_START_AND_READ);
  delay_us(50);
  /// comment out the Y_OUT+3 line if you are using a "normal"
  /// smart its board and comment in the next line.
  set_adc_channel(ADXL_311_Y_OUT+3);//a4 HIER
  //set_adc_channel(ADXL_311_Y_OUT);//a4 HIER
  delay_us(50);
  yaxis2 = read_adc(ADC_START_AND_READ);
  set_adc_channel(ADXL_311_Y_OUT);//a1
  delay_us(50);
  yaxis = read_adc(ADC_START_AND_READ);
  delay_us(50);
  set_adc_channel(AXDL_311_X_OUT);//a3
  delay_us(50);
  xaxis2 = read_adc(ADC_START_AND_READ);
  retval[0] = xaxis;
  retval[1] = yaxis;
  // xaxis2 and yaxis2 are not needed
  return (retval);
}

/**
 * Calculates the mean value during <em>number</em> of calls.
 * \param number number of calls to read_adxl311 to calculate the mean of the 
 * values (xaxis, yaxis).
 * \return 0 if i <= 0, an array of xaxis and yaxis, with a calculated mean on 
 * <b>number</b> calls to read_adxl311().
 */

long * read_adxl311_meanof(int number) 
{
	long xtemp, ytemp;
	long retval[2];
	int i=0;

	retval[0] = retval[1] = 0;
	ytemp = xtemp = 0;

	for (i=0;i < number;i++)
	{
		long * bla;
		bla = read_adxl311();
		xtemp = xtemp + bla[0];
		ytemp = ytemp + bla[1];
	}
	xtemp= xtemp / number;
	ytemp= ytemp / number;
	
	retval[0] = (xtemp);
	retval[1] = (ytemp);
	return(retval);
}
