beep.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "gpio.h"
  2. #include "timer.h"
  3. #include "rtdbg.h"
  4. #include "beep.h"
  5. #include "paras.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 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(BEEP_GPIO, BEEP_OFF_LEVEL);
  48. }
  49. else {
  50. gpio_set(BEEP_GPIO, BEEP_ON_LEVEL);
  51. }
  52. }
  53. if(last) {
  54. timer_stop(h->tmr);
  55. gpio_set(BEEP_GPIO, BEEP_OFF_LEVEL);
  56. }
  57. }
  58. /////////////////////////////////////////////
  59. int beep_init(void)
  60. {
  61. beep_handle_t *h=&bpHandle;
  62. gpio_init(BEEP_GPIO, GPIO_OUT, BEEP_OFF_LEVEL);
  63. h->tmr = timer_init();
  64. h->mode = -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(BEEP_GPIO);
  72. timer_deinit(h->tmr);
  73. h->tmr = -1;
  74. return 0;
  75. }
  76. int beep_set(int mode)
  77. {
  78. beep_handle_t *h=&bpHandle;
  79. h->mode = mode;
  80. return beep_set_ex(&beep_para[h->mode]);
  81. }
  82. int beep_set_ex(beep_para_t *para)
  83. {
  84. int r = 0;
  85. beep_handle_t *h=&bpHandle;
  86. misc_data_t *misc=&paras_get()->misc;
  87. if(!misc->beep || !para || (para->on_ms<=0 && para->off_ms<=0) || para->times==0) {
  88. return -1;
  89. }
  90. h->cnt = 0;
  91. h->para = *para;
  92. if(h->para.on_ms>0) {
  93. gpio_set(BEEP_GPIO, BEEP_ON_LEVEL);
  94. if(h->para.off_ms>0) {
  95. h->para.times *= 2;
  96. }
  97. else {
  98. h->para.on_ms *= h->para.times;
  99. h->para.times = 1;
  100. }
  101. r = timer_start(h->tmr, h->para.on_ms, h->para.times, beep_handler, h);
  102. }
  103. else {
  104. gpio_set(BEEP_GPIO, BEEP_OFF_LEVEL);
  105. if(h->para.off_ms>0) {
  106. h->para.off_ms *= h->para.times;
  107. h->para.times = 1;
  108. r = timer_start(h->tmr, h->para.off_ms, h->para.times, beep_handler, h);
  109. }
  110. }
  111. return r;
  112. }
  113. int beep_reset(void)
  114. {
  115. beep_handle_t *h=&bpHandle;
  116. if(h->mode==-1) {
  117. return -1;
  118. }
  119. h->mode = -1;
  120. timer_stop(h->tmr);
  121. gpio_set(BEEP_GPIO, BEEP_OFF_LEVEL);
  122. return 0;
  123. }