/***************************************************************************
 *   Copyright (C) 2004-11-11 by                                           *
 *   Matthias Kranz (matthias.kranz@ifi.lmu.de)                            *
 *   Paul Holleis (paul.holleis@ifi.lmu.de)                                *
 *                                                                         *
 *   Last modified 2004-11-11                                              *
 *                                                                         *
 *   c file for the light dependant resistor A905014                       *
 *   version: 001 (2004-11-11, Matthias & Paul)                            *
 *                                                                         *
 *   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.             *
 **************************************************************************/

#ifndef __A905014_C__
#define __A905014_C__         097

/**
 * Switches the light sensor on
 * //power is not supplied via the awarecon connector
 */
#define LightSensorOn() {} //output_high(PIN_LIGHT_A905014_OUT)

/**
 * Switches the light sensor off
 * //power is not supplied via the awarecon connector
 */
#define LightSensorOff()  {} //output_low(PIN_LIGHT_A905014_OUT)

/**
 * Initializes the A905014 light sensor.
 *
 * @return returns 0 if no error
 */
#ifdef PIN_LIGHT_A905014_OUT
int LightSensorInit()
{
  //power is not supplied via the awarecon connector
  //bit_clear(TRIS_LIGHT_A905014_POWER);  // set power pin to output
  bit_set(TRIS_LIGHT_A905014_OUT);        // set sample pin to input
  LightSensorOff();                       // power off for light

  return 0;                               // indicate no error
}
#endif

/**
 * Gets the sensor value
 * @param value the sensor value
 * @return returns 0 if no error
 */
int LightSensorGet(int* value)
{
  *value = ADCRead8();
  return 0;
}


/**
 * Setups the microcontroller for sampling
 * Sets the correct AD channel, if neccessary
 * @return returns 0 if no error
 */
int LightSensorPrepare()
{
  // set channel
  if ( !ADCIsChSet(ADCH_LIGHT_A905014) )
  {
    ADCSetCh(ADCH_LIGHT_A905014);
  }
  // set conversion clock
  ADCSetClock(0);
  // start ADC
  ADCEnable();

  return 0;
}

//////////??????????????????????????????????????????????
/**
 * Packs the Light sensor value directly in the sendbuffer
 * as an ACL packet which is ready to be sent.
 * @param value Light sensor value
 * @return error code, 0 if no error
 *
 * Note: The format is described in acltypes.h
 */
int LightSensorPackInACL(int value)
{

  if ( ACLGetRemainingPayloadSpace() > 4 )
  {
    ACLAddNewType(ACL_TYPE_SFC_HI,ACL_TYPE_SFC_LO); // SFC
    ACLAddData(value);                // value
    ACLAddData(0);                  // index = 0
  }
  else
  {
    return 1;   // not enough space left
  }

  return 0; // no error
}

