123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #include "process.h"
- #include "gpio.h"
- #include "ac780x_gpio.h"
- uint16_t g_blinkLedTime; /*LED闪烁频率控制时间*/
- uint16_t g_blinkLedTgtTime; /*LED目标闪烁频率*/
- uint8_t g_detectTime; /*三个开关状态,检测时间*/
- #define STATUS_DETECTINTERVAL (40) /*60ms * 8 == 320ms 去抖动 */
- uint8_t g_lockstatus;
- uint8_t g_unlockstatus;
- uint8_t g_coverstatus;
- uint8_t g_state; //状态
- uint8_t lockbits;
- uint8_t unlockbits;
- uint8_t coverbits;
- static void update_state()
- {
- if((STATUS_CLOSE == g_lockstatus) & (STATUS_OPEN == g_unlockstatus)){
- g_state = STATE_LOCK;
-
- }else if((STATUS_OPEN == g_lockstatus) & (STATUS_CLOSE == g_unlockstatus)){
- g_state = STATE_UNLOCK;
- }else if((STATUS_OPEN == g_lockstatus) & (STATUS_OPEN == g_unlockstatus)){
- g_state = STATE_INTERMEDIATE;
- }else {
- g_state = STATE_EXCEPTION;
- }
-
- //if(STATUS_OPEN == g_coverstatus){
- // g_state = STATE_OPENCOVER;
- //}
- }
- void Process_Init(void)
- {
- /*初始化控制变量.*/
- g_blinkLedTime = 0;
- g_blinkLedTgtTime = BLINK_LED_DFTT;
-
- g_detectTime = 0;
-
- g_lockstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_CLOSE_KEY)? STATUS_OPEN:STATUS_CLOSE;
- g_unlockstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_OPEN_KEY)? STATUS_OPEN:STATUS_CLOSE;
- g_coverstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_COVER_KEY)? STATUS_OPEN:STATUS_CLOSE;
-
- lockbits = g_lockstatus == STATUS_OPEN?0xFF:0x00;
- unlockbits = g_unlockstatus == STATUS_OPEN?0xFF:0x00;
- coverbits = g_coverstatus == STATUS_OPEN?0xFF:0x00;
-
- /*上电默认LED点亮.*/
- //GREENLED_ON;
- update_state();
- }
- void Process_RunLedPrd(void)
- {
- /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
- if (g_blinkLedTime >= g_blinkLedTgtTime)
- {
- g_blinkLedTime = 0;
-
- //REDLED_OFF;
- //REDGREEN_TOGGLE;
-
- switch(g_state){
- case STATE_LOCK:
- REDLED_OFF;
- GREENLED_TOGGLE;
- break;
- case STATE_UNLOCK:
- GREENLED_OFF;
- REDLED_TOGGLE;
- break;
- case STATE_INTERMEDIATE:
- REDGREEN_TOGGLE;
- break;
- case STATE_OPENCOVER:
- REDLED_OFF;
- GREENLED_ON;
- break;
- case STATE_EXCEPTION:
- default:
- GREENLED_OFF;
- REDLED_ON;
- break;
-
- };
-
- }
-
- }
- void Process_ThreeStatus(void)
- {
- if(g_detectTime >= STATUS_DETECTINTERVAL){
- g_detectTime = 0;
-
-
- if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_CLOSE_KEY)){
- lockbits=(lockbits<<1)|0x01;
- }else{
- lockbits=(lockbits<<1)|0x00;
- }
-
- if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_OPEN_KEY)){
- unlockbits=(unlockbits<<1)|0x01;
- }else{
- unlockbits=(unlockbits<<1)|0x00;
- }
-
- if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_COVER_KEY)){
- coverbits=(coverbits<<1)|0x01;
- }else{
- coverbits=(coverbits<<1)|0x00;
- }
-
- if(g_lockstatus == STATUS_CLOSE && lockbits == 0xFF){
- g_lockstatus = STATUS_OPEN;
- }else if(g_lockstatus == STATUS_OPEN && lockbits == 0x00){
- g_lockstatus = STATUS_CLOSE;
- }
-
- if(g_unlockstatus == STATUS_CLOSE && unlockbits == 0xFF){
- g_unlockstatus = STATUS_OPEN;
- }else if(g_unlockstatus == STATUS_OPEN && unlockbits == 0x00){
- g_unlockstatus = STATUS_CLOSE;
- }
-
- if(g_coverstatus == STATUS_CLOSE && coverbits == 0xFF){
- g_coverstatus = STATUS_OPEN;
- }else if(g_coverstatus == STATUS_OPEN && coverbits == 0x00){
- g_coverstatus = STATUS_CLOSE;
- }
-
- update_state();
-
-
- }
-
- }
- uint8_t Process_LockStatus(void)
- {
- return g_lockstatus==STATUS_OPEN?0x00:0x01;
- }
- uint8_t Process_UnlockStatus(void)
- {
- return g_unlockstatus==STATUS_OPEN?0x00:0x01;
- }
- uint8_t Process_CoverStatus(void)
- {
- return g_coverstatus==STATUS_OPEN?0x00:0x01;
- }
|