beep.c 3.0 KB

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