/**************************************************************************************************
---------------------------------------------------------------------------------------------------
	Copyright (c) 2007, Jonathan Bagg
	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 of Jonathan Bagg 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.
---------------------------------------------------------------------------------------------------
   Project name : Infidigm AVR Drivers
   Processor	: ATMega64, ATMega128
   File name    : debounce.c
---------------------------------------------------------------------------------------------------
   Modifications: 

   Revision 1.0  2007/05/19 	Bagg
   - Cleaned up for release
   

---------------------------------------------------------------------------------------------------
   Created      : 12 May 2007     	           Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
   Input Debounce Filter
---------------------------------------------------------------------------------------------------
**************************************************************************************************/
#include <avr/io.h> 
#include <avr/interrupt.h>
#include "debounce.h"
#include "tim.h"

/**************************************************************************************************
*   Individual Pin Debounce Time
* 		1	use individual port / pin debounce times
* 		0	use the global debouce time for all ports / pins
**************************************************************************************************/
#define INDIV_TIME	0

/**************************************************************************************************
*   Maximum debounce time (mS)
* 		1	maximum debounce time < 32768
* 		0	maximum debounce time < 128
**************************************************************************************************/
#define	TIME_SIZE	0

/**************************************************************************************************
*   Global Debounce time for all pins when INDIV_TIME = 0
**************************************************************************************************/
#define	GLOBAL_TIME		25

/**************************************************************************************************
*   For the preprocessor only!
**************************************************************************************************/
#define PORT_SIZE	8
#if TIME_SIZE==1
	typedef unsigned int TIME_TYPE;
	#define	ABS_MASK	0x7FFF
#else
	typedef unsigned char TIME_TYPE;
	#define	ABS_MASK	0x7F
#endif

/**************************************************************************************************
*   Input Debounce variable set
**************************************************************************************************/
static struct {
	volatile unsigned char *port;		//pointer to port to read
	TIME_TYPE snapshot[PORT_SIZE];		//value of systimer at last pin / bit change
	#if INDIV_TIME==1
	TIME_TYPE time[PORT_SIZE];			//debounce / filter time for each pin / bit
	#endif
	unsigned char last;					//previous logic level of pin / bit at last read
	unsigned char status;				//filtered pins / bits
	unsigned char rising;				//pins / bits to filter on the rising edge
	unsigned char falling;				//pins / bits to filter on the falling edge
} input[IN_SIZE];

/**************************************************************************************************
*   setup_debounce(); - See 'debounce.h' Header file for Description
**************************************************************************************************/
void setup_debounce(void)
{
  // setup default debouce times when using Individual times
 #if INDIV_TIME==1
 unsigned char i,j;
 for (i=0;i<IN_SIZE;i++) {
 	for (j=0;j<PORT_SIZE;j++) {
 		input[i].time[j] = GLOBAL_TIME;
 	}
 }
 #endif
 
  // setup PORTB - B7 rising and falling, B1 rising only. Filter Time = GLOBAL_TIME
 input[IN_PINB].port = &PINB;
 input[IN_PINB].rising = _BV(PB7) | _BV(PB1);
 input[IN_PINB].falling = _BV(PB7);
   // setup PORTD - D4 rising and falling.
 input[IN_PIND].port = &PIND;
 input[IN_PIND].rising = _BV(PD4);
 input[IN_PIND].falling = _BV(PD4);
 //input[IN_PIND].time[PD4] = 80;	// D4 Filter Time = 80mS
}

/**************************************************************************************************
*   update_debounce(); - See 'debounce.h' Header file for Description
**************************************************************************************************/
void update_debounce(void)
{
	static unsigned char i;
	unsigned char read, change, flip, output=0;
	unsigned int time;

	time = read_systimer();	
	read = *input[i].port;
	 // force rising / falling update
	input[i].status&= ~(~read & ~input[i].falling);
	input[i].status|= read & ~input[i].rising;
	 // debouce filter
	change = input[i].last ^ read;
	flip = read ^ input[i].status;
	input[i].last = read;
	if (change || flip) {
		unsigned char j;
		for (j=0; j<PORT_SIZE;j++) {
			output>>= 1;
			if (change & 0x01) {
				input[i].snapshot[j] = time;
			}
			#if INDIV_TIME==1
			else if ((flip & 0x01) && (((time - input[i].snapshot[j]) & ABS_MASK) >= input[i].time[j]))
			#else
			else if ((flip & 0x01) && (((time - input[i].snapshot[j]) & ABS_MASK) >= GLOBAL_TIME))
			#endif
				output|=0x80;
			change>>= 1;
			flip>>= 1;
		}
	}
	input[i].status^=output;
	
	if (i < IN_SIZE)
		i++;
	else
		i=0;
}

/**************************************************************************************************
*   read_debounce(); - See 'debounce.h' Header file for Description
**************************************************************************************************/
unsigned char read_debounce(unsigned char id, unsigned char pin)
{
 unsigned char level;
 
 if (input[id].status & (1 << pin))
 	level = 1;
 else
 	level = 0;
 	
 return level; 	
}
