/*  BSD-License:

Copyright (c) 2007 by Nils Springob, nicai-systems, Germany

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  * Neither the name nicai-systems nor the names of its contributors may be
    used to endorse or promote products derived from this software without
    specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

/*! @file    line.c
 *  @brief   Zuordnung der physikalischen Pins zu symbolischen Namen
 *  @author  Nils Springob (nils@nicai-systems.de)
 *  @date    2009-08-19
 */
 
/*
* 12.08.2012 li Anpassung an meine umgebung
*/ 

#include "iodefs.h"
#include "line.h"
#include <avr/eeprom.h>
#include <util/delay.h>

#define LINE_EEPROM ((void *)0)

#ifdef __cplusplus
extern "C" {
#endif


line_cal_t line_cal;


static void do_calibrateWhite(uint8_t idx);
static void do_calibrateBlack(uint8_t idx);


/*****************************************************************
 * Initialisierung des Liniensensors.
 * Ruft bei Bedarf analog_init() und line_readPersistent() auf.
 * Die Interrupts müssen aktiviert sein, damit die Sensorwerte gemessen werden!
 */
void line_init( void )
{
   //if (!(nibobee_initialization & NIBOBEE_ANALOG_INITIALIZED))
   //{
   //   analog_init();
   //}
   line_readPersistent();
   activate_output_bit( IO_LINE_EN );
}



/*****************************************************************
* Speichert die Kaibrierungsdaten im EEPROM des ATmega16
*/
void line_writePersistent( void )
{
   eeprom_write_block (&line_cal, LINE_EEPROM, sizeof(line_cal_t));
}



/*****************************************************************
* Laden der Kaibrierungsdaten aus dem EEPROM des ATmega16
*/
void line_readPersistent( void )
{
   eeprom_read_block (&line_cal, LINE_EEPROM, sizeof(line_cal_t));
  
   if ( line_cal.white[0] == 0xffff )    // EEPROM geloescht ?? 
   {
                                        // use default   
      line_cal.white[0]=0x200;
      line_cal.white[1]=0x300;
      line_cal.white[2]=0x200;
      line_cal.black[0]=4;
      line_cal.black[1]=4;
      line_cal.black[2]=4;
   }
}



/*****************************************************************
 * Liefert den normalisierten Wert des Liniensensors mit dem index @param idx .
 * Die Interrupts müssen aktiviert sein, damit die Sensorwerte gemessen werden!
 * Der Parameter @param idx kann die Werte LINE_L, LINE_C oder LINE_R annehmen.
 */
uint16_t line_get(uint8_t idx) 
{
   int16_t val16;
   uint32_t val;

   if (idx < 3 )           // 0 bis 2 erlaubt 
   {
                           // differnez Wert
      val16 = (int16_t)analog_getValue(ANALOG_L1+idx)-(int16_t)analog_getValue(ANALOG_L0+idx);
      if (val16 < 0 )
         val16 = 0;
      
      val = val16;
      val *= 1024;                   
      val /= line_cal.white[idx];      // Promille Wert zu weiss  
      
      if (val > line_cal.black[idx])    
         val -= line_cal.black[idx];   // promille Wert black abziehen.
      else
         return 0;
      
      if (val > INT16_MAX)
         return INT16_MAX;
      
      return val;                      // soviel mehr als schwarz 
   }
   return 0;
}



/*************************************************************
** differenz werte fuer weiss sichern
*/
static void do_calibrateWhite(uint8_t idx)
{
   int16_t val16;

   if ( idx < 3 )
   {
      val16 = (int16_t)analog_getValue(ANALOG_L1+idx)-(int16_t)analog_getValue(ANALOG_L0+idx);
      if (val16 < 0)
         val16 = 0;
      line_cal.white[idx] = val16;
   }
}



/*************************************************************
** Promille Werte der differenz werte schwarz gegen weiss sichern
*/
static void do_calibrateBlack(uint8_t idx)
{
   int16_t val16;
   uint32_t val;
   
   if (idx<3)
   {
                                      // differenz der Schwarz Werte
      val16 = (int16_t)analog_getValue(ANALOG_L1+idx)-(int16_t)analog_getValue(ANALOG_L0+idx);
      if (val16 < 0)
         val16=0;
      val = val16;
      val *= 1024;                    // * 1024 ( ca. 1000 = Promille )  
      val /= line_cal.white[idx];     // geteilt durch die Differnz white
      line_cal.black[idx] = val;      // sichern  
   }
}



/*****************************************************************
 * Kalibrierung auf weisse Oberfläche für normalisiserten Wert 1024.
 * Die Interrupts müssen aktiviert sein, damit die Sensorwerte gemessen werden!
 */
void line_calibrateWhite(void)
{
   _delay_ms(100);
   do_calibrateWhite( LINE_L );
   do_calibrateWhite( LINE_C );
   do_calibrateWhite( LINE_R );
}


/*****************************************************************
 * Kalibrierung auf schwarze Oberfläche für normalisiserten Wert 0.
 * Die Interrupts müssen aktiviert sein, damit die Sensorwerte gemessen werden!
 * Die Funktion darf erst nach line_calibrateWhite() aufgerufen werden.
 */
void line_calibrateBlack(void)
{
   _delay_ms(100);
   do_calibrateBlack( LINE_L );
   do_calibrateBlack( LINE_C );
   do_calibrateBlack( LINE_R );
}


#ifdef __cplusplus
} // extern "C"
#endif

