process.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. g_state = STATE_LOCK;
  30. }
  31. void Process_Init(void)
  32. {
  33. /*初始化控制变量.*/
  34. g_blinkLedTime = 0;
  35. g_blinkLedTgtTime = BLINK_LED_DFTT;
  36. g_detectTime = 0;
  37. g_lockstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_CLOSE_KEY)? STATUS_OPEN:STATUS_CLOSE;
  38. g_unlockstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_OPEN_KEY)? STATUS_OPEN:STATUS_CLOSE;
  39. g_coverstatus = GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_COVER_KEY)? STATUS_OPEN:STATUS_CLOSE;
  40. lockbits = g_lockstatus == STATUS_OPEN?0xFF:0x00;
  41. unlockbits = g_unlockstatus == STATUS_OPEN?0xFF:0x00;
  42. coverbits = g_coverstatus == STATUS_OPEN?0xFF:0x00;
  43. /*上电默认LED点亮.*/
  44. //GREENLED_ON;
  45. update_state();
  46. }
  47. void Process_RunLedPrd(void)
  48. {
  49. /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
  50. if (g_blinkLedTime >= g_blinkLedTgtTime)
  51. {
  52. g_blinkLedTime = 0;
  53. //REDLED_OFF;
  54. //REDGREEN_TOGGLE;
  55. switch(g_state){
  56. case STATE_LOCK:
  57. //REDLED_OFF;
  58. GREENLED_TOGGLE;
  59. break;
  60. case STATE_UNLOCK:
  61. GREENLED_OFF;
  62. //REDLED_TOGGLE;
  63. break;
  64. case STATE_INTERMEDIATE:
  65. //REDGREEN_TOGGLE;
  66. break;
  67. case STATE_OPENCOVER:
  68. //REDLED_OFF;
  69. GREENLED_ON;
  70. break;
  71. case STATE_EXCEPTION:
  72. default:
  73. GREENLED_OFF;
  74. //REDLED_ON;
  75. break;
  76. };
  77. }
  78. }
  79. void Process_ThreeStatus(void)
  80. {
  81. if(g_detectTime >= STATUS_DETECTINTERVAL){
  82. g_detectTime = 0;
  83. if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_CLOSE_KEY)){
  84. lockbits=(lockbits<<1)|0x01;
  85. }else{
  86. lockbits=(lockbits<<1)|0x00;
  87. }
  88. if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_OPEN_KEY)){
  89. unlockbits=(unlockbits<<1)|0x01;
  90. }else{
  91. unlockbits=(unlockbits<<1)|0x00;
  92. }
  93. if (GPIO_LEVEL_HIGH == GPIO_GetPinLevel(GPIO_COVER_KEY)){
  94. coverbits=(coverbits<<1)|0x01;
  95. }else{
  96. coverbits=(coverbits<<1)|0x00;
  97. }
  98. if(g_lockstatus == STATUS_CLOSE && lockbits == 0xFF){
  99. g_lockstatus = STATUS_OPEN;
  100. }else if(g_lockstatus == STATUS_OPEN && lockbits == 0x00){
  101. g_lockstatus = STATUS_CLOSE;
  102. }
  103. if(g_unlockstatus == STATUS_CLOSE && unlockbits == 0xFF){
  104. g_unlockstatus = STATUS_OPEN;
  105. }else if(g_unlockstatus == STATUS_OPEN && unlockbits == 0x00){
  106. g_unlockstatus = STATUS_CLOSE;
  107. }
  108. if(g_coverstatus == STATUS_CLOSE && coverbits == 0xFF){
  109. g_coverstatus = STATUS_OPEN;
  110. }else if(g_coverstatus == STATUS_OPEN && coverbits == 0x00){
  111. g_coverstatus = STATUS_CLOSE;
  112. }
  113. update_state();
  114. }
  115. }
  116. uint8_t Process_LockStatus(void)
  117. {
  118. return g_lockstatus==STATUS_OPEN?0x00:0x01;
  119. }
  120. uint8_t Process_UnlockStatus(void)
  121. {
  122. return g_unlockstatus==STATUS_OPEN?0x00:0x01;
  123. }
  124. uint8_t Process_CoverStatus(void)
  125. {
  126. return g_coverstatus==STATUS_OPEN?0x00:0x01;
  127. }