/*-------------------------------------------------------------------------
   AR168M demo external controller MCS51
   Hook function copy right information

   Copyright (C) 2008. Lin, Rongrong <woody@palmmicro.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This software 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-------------------------------------------------------------------------*/

#include "type.h"
#include "8051.h"
#include "sys.h"

#define HOOK_IO		P2_4

#define HOOK_DOWN_DELAY		15	// wait 150ms for hook down to be steady

DCHAR _iHookDownCounter;
BOOLEAN _bOldHookDown;
volatile BOOLEAN _bHook;
volatile BOOLEAN _bHookDown;

void HookISR() using 1
{
	if (_bHook)	return;

	_bHookDown = HOOK_IO;
	_bHook = TRUE;
}

void HookInit()
{
	_bHook = FALSE;
	_bHookDown = _bOldHookDown = TRUE;
}

void HookRun()
{
	if (!_bHook)	return;

	if (_bHookDown)
	{
		_iHookDownCounter ++;
		if (_iHookDownCounter >= HOOK_DOWN_DELAY)
		{
			if (!_bOldHookDown)
			{
				UI_HandleKey('Z');
				_bOldHookDown = TRUE;
			}
			_iHookDownCounter = 0;
		}
	}
	else
	{
		_iHookDownCounter = 0;
		if (_bOldHookDown)
		{
			UI_HandleKey('z');
			_bOldHookDown = FALSE;
		}
	}

	_bHook = FALSE;
}
