beep.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "gpio.h"
  2. #include "timer.h"
  3. #include "elog.h"
  4. #include "beep.h"
  5. #if 1
  6. #define LOGD log_d
  7. #define LOGE log_e
  8. #else
  9. #define LOGD printf
  10. #define LOGE printf
  11. #endif
  12. #define BEEP_GPIO 103 //PD7=96+7
  13. #define BEEP_ON_LEVEL 1
  14. #define BEEP_OFF_LEVEL 0
  15. static beep_para_t beep_para[BEEP_MODE_MAX]={
  16. {50, 100, 10}, //BEEP_MODE_WARN
  17. {50, 100, 10}, //BEEP_MODE_ERROR
  18. {50, 100, 10}, //BEEP_MODE_EMERG
  19. {50, 100, 10}, //BEEP_MODE_FATAL
  20. };
  21. typedef struct {
  22. int io;
  23. int tmr;
  24. uint32_t cnt;
  25. beep_para_t para;
  26. }beep_handle_t;
  27. static beep_handle_t bpHandle={0};
  28. static void tmr_handler(void *arg, int last)
  29. {
  30. beep_handle_t *h=(beep_handle_t*)arg;
  31. h->cnt++;
  32. if(h->cnt%2) {
  33. if(h->para.off_ms) {
  34. gpio_set(h->io, BEEP_ON_LEVEL);
  35. timer_setup(h->tmr, h->para.off_ms, 0);
  36. }
  37. }
  38. else {
  39. if(h->para.on_ms) {
  40. gpio_set(h->io, BEEP_OFF_LEVEL);
  41. timer_setup(h->tmr, h->para.on_ms, 0);
  42. }
  43. }
  44. if(last) {
  45. timer_stop(h->tmr);
  46. }
  47. }
  48. /////////////////////////////////////////////
  49. int beep_init(void)
  50. {
  51. beep_handle_t *h=&bpHandle;
  52. h->io = gpio_init(BEEP_GPIO, GPIO_OUT);
  53. gpio_set(h->io, BEEP_OFF_LEVEL);
  54. h->tmr = -1;
  55. //beep_para_t para={300, 300, 10};
  56. //beep_set_ex(&para);
  57. return 0;
  58. }
  59. int beep_deinit(void)
  60. {
  61. beep_handle_t *h=&bpHandle;
  62. gpio_deinit(h->io);
  63. timer_stop(h->tmr);
  64. h->io = -1;
  65. h->tmr = -1;
  66. return 0;
  67. }
  68. int beep_set(int mode)
  69. {
  70. beep_handle_t *h=&bpHandle;
  71. if(h->tmr!=-1) {
  72. timer_stop(h->tmr);
  73. }
  74. h->cnt = 0;
  75. h->para = beep_para[mode];
  76. if(h->para.on_ms>0 && h->para.off_ms>0) {
  77. h->para.times *=2;
  78. }
  79. h->tmr = timer_start(h->para.on_ms, 0, h->para.times, tmr_handler, h);
  80. return 0;
  81. }
  82. int beep_set_ex(beep_para_t *para)
  83. {
  84. beep_handle_t *h=&bpHandle;
  85. if(!para) {
  86. return -1;
  87. }
  88. if(h->tmr!=-1) {
  89. timer_stop(h->tmr);
  90. }
  91. h->cnt = 0;
  92. h->para = *para;
  93. if(h->para.on_ms>0 && h->para.off_ms>0) {
  94. h->para.times *=2;
  95. }
  96. h->tmr = timer_start(h->para.on_ms, 0, h->para.times, tmr_handler, h);
  97. return 0;
  98. }