📄 dc550_hookswitchmonitor.c
字号:
/*****************************************************************************/
/* CONFIDENTIAL */
/* Sigpro Copyright 2003, All rights reserved */
/*****************************************************************************/
/* CLIENT: Telematrix */
/* PROJECT: DC550 Digital Centrex Phone */
/* FILE: dc550_hookswitchmonitor.c */
/*****************************************************************************/
/* This file contains the code used to detect and debounce the hookswitch */
/* state. */
/*****************************************************************************/
#include "dc550_hookswitchmonitor.h"
// #include "dc550_usartdriver.h"
/******************************************************************************
* LOCAL CONSTANTS AND DATA TYPES
*****************************************************************************/
typedef enum enum_HS_STATE {
HS_IDLE=0, HS_DEBOUNCING_OFF, HS_OFF, HS_DEBOUNCING_ON
} HS_STATE;
struct str_hsTaskData {
HS_STATE state;
signed char ctr;
};
/******************************************************************************
* STATIC VARIABLES
*****************************************************************************/
static struct str_hsTaskData hsTaskData;
/******************************************************************************
* FUNCTION: void hookswitchmonitor_init(void)
******************************************************************************
* DESCRIPTION:
* This function is called to initialize the hookswitch monitor task. The
* hookswitch state will be initially set to on-hook but the actual state
* must be debounced which will require several iterations of the exec()
* function before the hookswitch state can be verified.
*****************************************************************************/
void hookswitchmonitor_init(void)
{
/* Initialize global/static variables */
hsTaskData.state = HS_IDLE;
hsTaskData.ctr = 0;
/* Initialize port pin of interest */
HOOKSWITCH_DIR &= ~(HOOKSWITCH_MSK);
/* Read the current status of the hook switch */
hsTaskData.state = (((HOOKSWITCH_IN & HOOKSWITCH_MSK) == 0) ?
HS_OFF : HS_IDLE);
}
/******************************************************************************
* FUNCTION: void hookswitchmonitor_exec(void)
******************************************************************************
* DESCRIPTION:
* This function is called to execute the hookswitch monitor task.
*****************************************************************************/
void hookswitchmonitor_exec(void)
{
unsigned int hookState;
/* Read the current status of the hook switch */
hookState = (((HOOKSWITCH_IN & HOOKSWITCH_MSK) == 0) ?
HOOKSTATE_UP : HOOKSTATE_DOWN);
/* Phone is Off-Hook */
if (hookState == HOOKSTATE_UP) {
/* If was on-hook last time, zero the counter and start over */
if ( (hsTaskData.state == HS_DEBOUNCING_ON) ||
(hsTaskData.state == HS_IDLE) ) {
hsTaskData.ctr = 0;
if (hsTaskData.state == HS_DEBOUNCING_ON)
hsTaskData.state = HS_OFF;
}
/* Still debouncing... */
if ( (hsTaskData.state != HS_OFF) &&
(hsTaskData.ctr < HS_OFF_MAXCNT) ) {
/* Increment debounce counter */
hsTaskData.ctr++;
/* Set state to debouncing down */
hsTaskData.state = HS_DEBOUNCING_OFF;
/* Is the phone solid off-hook yet? */
if (hsTaskData.ctr == HS_OFF_MAXCNT) {
/* Set our state to HS_OFF */
hsTaskData.state = HS_OFF;
// dc550_printf("\r\nOFF HOOK");
}
}
}
/* Phone is On-Hook */
else {
/* If was off-hook last time, zero the counter and start over */
if ( (hsTaskData.state == HS_DEBOUNCING_OFF) ||
(hsTaskData.state == HS_OFF) ) {
hsTaskData.ctr = 0;
if (hsTaskData.state == HS_DEBOUNCING_OFF)
hsTaskData.state = HS_IDLE;
}
/* Still debouncing... */
if ( (hsTaskData.state != HS_IDLE) &&
(hsTaskData.ctr < HS_ON_MAXCNT) ) {
/* Increment debounce counter */
hsTaskData.ctr++;
/* Set state to debouncing on-hook */
hsTaskData.state = HS_DEBOUNCING_ON;
/* Is the phone solid on-hook yet? */
if (hsTaskData.ctr == HS_ON_MAXCNT) {
/* Set our state to HS_IDLE */
hsTaskData.state = HS_IDLE;
// dc550_printf("\r\nON HOOK");
}
}
}
}
/******************************************************************************
* FUNCTION: unsigned int hookswitchmonitor_gethookswitchstate(void)
******************************************************************************
* DESCRIPTION:
* This accessor function returns the last debounced state of the hookswitch.
*****************************************************************************/
unsigned int hookswitchmonitor_gethookswitchstate(void)
{
switch (hsTaskData.state) {
case (HS_IDLE):
case (HS_DEBOUNCING_OFF):
return (HOOKSTATE_DOWN);
case (HS_OFF):
case (HS_DEBOUNCING_ON):
return (HOOKSTATE_UP);
default:
return (HOOKSTATE_DOWN);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -