beep.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "gpio.h"
  2. #include "timer.h"
  3. #include "elog.h"
  4. #include "beep.h"
  5. #include "common.h"
  6. #include <stdio.h>
  7. #include <sys/time.h>
  8. #if 1
  9. #define LOGD log_d
  10. #define LOGE log_e
  11. #else
  12. #define LOGD printf
  13. #define LOGE printf
  14. #endif
  15. #define BEEP_GPIO 103 //PD7=96+7
  16. #define BEEP_ON_LEVEL 1
  17. #define BEEP_OFF_LEVEL 0
  18. static beep_para_t beep_para[BEEP_MODE_MAX]={
  19. #if 0
  20. {1000, 1000, 5}, //BEEP_MODE_WARN1
  21. {1000, 1000, 5}, //BEEP_MODE_WARN1
  22. {500, 500, 10}, //BEEP_MODE_ERROR
  23. {200, 200, 25}, //BEEP_MODE_FAULT
  24. {50, 50, -1}, //BEEP_MODE_FATAL
  25. #else
  26. {500, 500, 300}, //BEEP_MODE_WARN1
  27. {333, 333, 450}, //BEEP_MODE_WARN2
  28. {500, 500, 10}, //BEEP_MODE_ERROR
  29. {200, 200, 25}, //BEEP_MODE_FAULT
  30. {50, 50, -1}, //BEEP_MODE_FATAL
  31. #endif
  32. };
  33. typedef struct {
  34. int io;
  35. int tmr;
  36. uint32_t cnt;
  37. beep_para_t para;
  38. int mode;
  39. int stop_level;
  40. }beep_handle_t;
  41. static beep_handle_t bpHandle={0};
  42. static inline MiscInfo_t* get_misc(void)
  43. {
  44. return &__globalDeviceManage._globalDevInfo.misc;
  45. }
  46. static void beep_handler(void *arg, int last)
  47. {
  48. beep_handle_t *h=(beep_handle_t*)arg;
  49. if(h->para.on_ms>0 && h->para.off_ms>0) {
  50. h->cnt++;
  51. if(h->cnt%2) {
  52. gpio_set(h->io, BEEP_OFF_LEVEL);
  53. }
  54. else {
  55. gpio_set(h->io, BEEP_ON_LEVEL);
  56. }
  57. }
  58. if(last) {
  59. timer_stop(h->tmr);
  60. gpio_set(h->io, BEEP_OFF_LEVEL);
  61. }
  62. }
  63. /////////////////////////////////////////////
  64. int beep_init(void)
  65. {
  66. beep_handle_t *h=&bpHandle;
  67. h->io = gpio_init(BEEP_GPIO, GPIO_OUT);
  68. h->tmr = timer_init();
  69. gpio_set(h->io, BEEP_OFF_LEVEL);
  70. h->mode = -1;
  71. //beep_set(BEEP_MODE_FATAL);
  72. return 0;
  73. }
  74. int beep_deinit(void)
  75. {
  76. beep_handle_t *h=&bpHandle;
  77. gpio_deinit(h->io);
  78. timer_deinit(h->tmr);
  79. h->io = -1;
  80. h->tmr = -1;
  81. return 0;
  82. }
  83. int beep_set(int mode)
  84. {
  85. beep_handle_t *h=&bpHandle;
  86. h->mode = mode;
  87. return beep_set_ex(&beep_para[h->mode]);
  88. }
  89. int beep_set_ex(beep_para_t *para)
  90. {
  91. int r = 0;
  92. beep_handle_t *h=&bpHandle;
  93. MiscInfo_t* misc=get_misc();
  94. if(!misc->led || !para || (para->on_ms<=0 && para->off_ms<=0) || para->times==0) {
  95. return -1;
  96. }
  97. h->cnt = 0;
  98. h->para = *para;
  99. if(h->para.on_ms>0) {
  100. gpio_set(h->io, BEEP_ON_LEVEL);
  101. if(h->para.off_ms>0) {
  102. h->para.times *= 2;
  103. }
  104. else {
  105. h->para.on_ms *= h->para.times;
  106. h->para.times = 1;
  107. }
  108. r = timer_start(h->tmr, h->para.on_ms, h->para.times, beep_handler, h);
  109. }
  110. else {
  111. gpio_set(h->io, BEEP_OFF_LEVEL);
  112. if(h->para.off_ms>0) {
  113. h->para.off_ms *= h->para.times;
  114. h->para.times = 1;
  115. r = timer_start(h->tmr, h->para.off_ms, h->para.times, beep_handler, h);
  116. }
  117. }
  118. return r;
  119. }
  120. int beep_reset(void)
  121. {
  122. beep_handle_t *h=&bpHandle;
  123. if(h->mode==-1) {
  124. return -1;
  125. }
  126. h->mode = -1;
  127. timer_stop(h->tmr);
  128. gpio_set(h->io, BEEP_OFF_LEVEL);
  129. return 0;
  130. }