beep.c 3.1 KB

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