beep.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 stop_level;
  38. }beep_handle_t;
  39. static beep_handle_t bpHandle={0};
  40. static void beep_handler(void *arg, int last)
  41. {
  42. beep_handle_t *h=(beep_handle_t*)arg;
  43. if(h->para.on_ms>0 && h->para.off_ms>0) {
  44. h->cnt++;
  45. if(h->cnt%2) {
  46. gpio_set(h->io, BEEP_OFF_LEVEL);
  47. }
  48. else {
  49. gpio_set(h->io, BEEP_ON_LEVEL);
  50. }
  51. }
  52. if(last) {
  53. timer_stop(h->tmr);
  54. gpio_set(h->io, BEEP_OFF_LEVEL);
  55. }
  56. }
  57. /////////////////////////////////////////////
  58. int beep_init(void)
  59. {
  60. beep_handle_t *h=&bpHandle;
  61. h->io = gpio_init(BEEP_GPIO, GPIO_OUT);
  62. h->tmr = timer_init();
  63. gpio_set(h->io, BEEP_OFF_LEVEL);
  64. h->tmr = -1;
  65. //beep_set(BEEP_MODE_FATAL);
  66. return 0;
  67. }
  68. int beep_deinit(void)
  69. {
  70. beep_handle_t *h=&bpHandle;
  71. gpio_deinit(h->io);
  72. timer_deinit(h->tmr);
  73. h->io = -1;
  74. h->tmr = -1;
  75. return 0;
  76. }
  77. int beep_set(int mode)
  78. {
  79. beep_handle_t *h=&bpHandle;
  80. return beep_set_ex(&beep_para[mode]);
  81. }
  82. int beep_set_ex(beep_para_t *para)
  83. {
  84. int r = 0;
  85. beep_handle_t *h=&bpHandle;
  86. if(!para || (para->on_ms<=0 && para->off_ms<=0) || para->times==0) {
  87. return -1;
  88. }
  89. h->cnt = 0;
  90. h->para = *para;
  91. if(h->para.on_ms>0) {
  92. gpio_set(h->io, BEEP_ON_LEVEL);
  93. if(h->para.off_ms>0) {
  94. h->para.times *= 2;
  95. }
  96. else {
  97. h->para.on_ms *= h->para.times;
  98. h->para.times = 1;
  99. }
  100. r = timer_start(h->tmr, h->para.on_ms, h->para.times, beep_handler, h);
  101. }
  102. else {
  103. gpio_set(h->io, BEEP_OFF_LEVEL);
  104. if(h->para.off_ms>0) {
  105. h->para.off_ms *= h->para.times;
  106. h->para.times = 1;
  107. r = timer_start(h->tmr, h->para.off_ms, h->para.times, beep_handler, h);
  108. }
  109. }
  110. return r;
  111. }
  112. int beep_reset(void)
  113. {
  114. beep_handle_t *h=&bpHandle;
  115. timer_stop(h->tmr);
  116. gpio_set(h->io, BEEP_OFF_LEVEL);
  117. return 0;
  118. }