#define SW_OFF 0
#define SW_GOING_ON 1
#define SW_ON 2
#define SW_GOING_OFF 3
#define SW_JUST_ON 0
#define SW_JUST_OFF 1
BOOL isSw_on(void) {
return 0;
}
BYTE serveSw(void) {
static BYTE swState = SW_OFF;
static WORD swTimer = 0x0000;
static BYTE SwSwRetval = SW_IDLE;
switch (swState) {
case SW_OFF:
if (isSw_on()) {
timeSet(&swTimer, 10);
swState = SW_GOING_ON;
}
break;
case SW_GOING_ON:
if (isSw_on()) {
if (timeExpired(&swTimer)) {
swState = SW_ON;
SwSwRetval = SW_JUST_ON;
}
}else {
swState = SW_OFF; // Switch bounced, ingore
}
break;
case SW_ON:
if (!isSw_on()) {
swState = SW_GOING_OFF;
timeSet(&swTimer, 10);
}
SwSwRetval = SW_IS_ON;
break;
case SW_GOING_OFF:
if (!isSw_on()) {
if (timeExpired(&swTimer)) {
swState = SW_OFF;
SwSwRetval = SW_JUST_OFF;
}
}
else {
swState = SW_ON; // Switch bounced, ingore
}
break;
}
return(SwSwRetval);
}
void timeSet(WORD *pTimer, WORD time) {
WORD VoipCt;
do { // ISR may update this 16-bit value and PSoC
VoipCt = voipCt; // doesn't have atomic access,
} while (VoipCt != voipCt); // so read until stable
*pTimer = VoipCt + time;
}
BOOL timeExpired(WORD *pTimer) {
WORD Delta;
WORD VoipCt;
do { // ISR may update this 16-bit value and PSoC
VoipCt = voipCt; // doesn't have atomic access,
} while (VoipCt != voipCt); // so read until stable
Delta = *pTimer - VoipCt;
if (((BYTE)(Delta>>8)&0x80) == 0) {
return FALSE; // not expired
}else{
return TRUE; // expired
}
}
转载于:https://my.oschina.net/u/3629716/blog/1491291