process.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "process.h"
  2. #include "gpio.h"
  3. #include "ac780x_gpio.h"
  4. uint16_t g_blinkLedTime; /*LED闪烁频率控制时间*/
  5. uint16_t g_blinkLedTgtTime; /*LED目标闪烁频率*/
  6. uint8_t g_detectTime; /*三个开关状态,检测时间*/
  7. #define STATUS_DETECTINTERVAL (40) /*60ms * 8 == 320ms 去抖动 */
  8. uint8_t g_lockstatus;
  9. uint8_t g_unlockstatus;
  10. uint8_t g_coverstatus;
  11. uint8_t g_state; //状态
  12. uint8_t lockbits;
  13. uint8_t unlockbits;
  14. uint8_t coverbits;
  15. static void update_state()
  16. {
  17. if((STATUS_CLOSE == g_lockstatus) & (STATUS_OPEN == g_unlockstatus)){
  18. g_state = STATE_LOCK;
  19. }else if((STATUS_OPEN == g_lockstatus) & (STATUS_CLOSE == g_unlockstatus)){
  20. g_state = STATE_UNLOCK;
  21. }else if((STATUS_OPEN == g_lockstatus) & (STATUS_OPEN == g_unlockstatus)){
  22. g_state = STATE_INTERMEDIATE;
  23. }else {
  24. g_state = STATE_EXCEPTION;
  25. }
  26. //if(STATUS_OPEN == g_coverstatus){
  27. // g_state = STATE_OPENCOVER;
  28. //}
  29. }
  30. void Process_Init(void)
  31. {
  32. /*初始化控制变量.*/
  33. g_blinkLedTime = 0;
  34. g_blinkLedTgtTime = BLINK_LED_DFTT;
  35. g_detectTime = 0;
  36. g_lockstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_CLOSE_KEY)? STATUS_OPEN:STATUS_CLOSE;
  37. g_unlockstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_OPEN_KEY)? STATUS_OPEN:STATUS_CLOSE;
  38. g_coverstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_COVER_KEY)? STATUS_OPEN:STATUS_CLOSE;
  39. lockbits = g_lockstatus == STATUS_OPEN?0xFF:0x00;
  40. unlockbits = g_unlockstatus == STATUS_OPEN?0xFF:0x00;
  41. coverbits = g_coverstatus == STATUS_OPEN?0xFF:0x00;
  42. /*上电默认LED点亮.*/
  43. //GREENLED_ON;
  44. update_state();
  45. }
  46. void Process_RunLedPrd(void)
  47. {
  48. /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
  49. if (g_blinkLedTime >= g_blinkLedTgtTime)
  50. {
  51. g_blinkLedTime = 0;
  52. //REDLED_OFF;
  53. //REDGREEN_TOGGLE;
  54. switch(g_state){
  55. case STATE_LOCK:
  56. REDLED_OFF;
  57. GREENLED_TOGGLE;
  58. break;
  59. case STATE_UNLOCK:
  60. GREENLED_OFF;
  61. REDLED_TOGGLE;
  62. break;
  63. case STATE_INTERMEDIATE:
  64. REDGREEN_TOGGLE;
  65. break;
  66. case STATE_OPENCOVER:
  67. REDLED_OFF;
  68. GREENLED_ON;
  69. break;
  70. case STATE_EXCEPTION:
  71. default:
  72. GREENLED_OFF;
  73. REDLED_ON;
  74. break;
  75. };
  76. }
  77. }
  78. void Process_ThreeStatus(void)
  79. {
  80. if(g_detectTime >= STATUS_DETECTINTERVAL){
  81. g_detectTime = 0;
  82. if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_CLOSE_KEY)){
  83. lockbits=(lockbits<<1)|0x01;
  84. }else{
  85. lockbits=(lockbits<<1)|0x00;
  86. }
  87. if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_OPEN_KEY)){
  88. unlockbits=(unlockbits<<1)|0x01;
  89. }else{
  90. unlockbits=(unlockbits<<1)|0x00;
  91. }
  92. if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_COVER_KEY)){
  93. coverbits=(coverbits<<1)|0x01;
  94. }else{
  95. coverbits=(coverbits<<1)|0x00;
  96. }
  97. if(g_lockstatus == STATUS_CLOSE && lockbits == 0xFF){
  98. g_lockstatus = STATUS_OPEN;
  99. }else if(g_lockstatus == STATUS_OPEN && lockbits == 0x00){
  100. g_lockstatus = STATUS_CLOSE;
  101. }
  102. if(g_unlockstatus == STATUS_CLOSE && unlockbits == 0xFF){
  103. g_unlockstatus = STATUS_OPEN;
  104. }else if(g_unlockstatus == STATUS_OPEN && unlockbits == 0x00){
  105. g_unlockstatus = STATUS_CLOSE;
  106. }
  107. if(g_coverstatus == STATUS_CLOSE && coverbits == 0xFF){
  108. g_coverstatus = STATUS_OPEN;
  109. }else if(g_coverstatus == STATUS_OPEN && coverbits == 0x00){
  110. g_coverstatus = STATUS_CLOSE;
  111. }
  112. update_state();
  113. }
  114. }
  115. uint8_t Process_LockStatus(void)
  116. {
  117. return g_lockstatus==STATUS_OPEN?0x00:0x01;
  118. }
  119. uint8_t Process_UnlockStatus(void)
  120. {
  121. return g_unlockstatus==STATUS_OPEN?0x00:0x01;
  122. }
  123. uint8_t Process_CoverStatus(void)
  124. {
  125. return g_coverstatus==STATUS_OPEN?0x00:0x01;
  126. }