beep.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. {50, 100, 10}, //BEEP_MODE_WARN
  19. {50, 100, 10}, //BEEP_MODE_ERROR
  20. {50, 100, 10}, //BEEP_MODE_EMERG
  21. {50, 100, 10}, //BEEP_MODE_FATAL
  22. };
  23. typedef struct {
  24. int io;
  25. int tmr;
  26. uint32_t cnt;
  27. beep_para_t para;
  28. int stop_level;
  29. }beep_handle_t;
  30. static beep_handle_t bpHandle={0};
  31. static uint64_t get_ms(void)
  32. {
  33. struct timeval tv;
  34. gettimeofday(&tv, NULL);
  35. return (uint64_t)(tv.tv_sec)*1000 + (uint64_t)(tv.tv_usec)/1000;
  36. }
  37. static void tmr_handler(void *arg, int last)
  38. {
  39. beep_handle_t *h=(beep_handle_t*)arg;
  40. h->cnt++;
  41. if(h->cnt%2) {
  42. if(h->para.off_ms) {
  43. //gpio_set(h->io, BEEP_ON_LEVEL);
  44. //timer_setup(h->tmr, h->para.off_ms, 0);
  45. }
  46. }
  47. else {
  48. if(h->para.on_ms) {
  49. //gpio_set(h->io, BEEP_OFF_LEVEL);
  50. //timer_setup(h->tmr, h->para.off_ms, 0);
  51. }
  52. }
  53. printf("___cnt: %d, %lld\n", h->cnt, get_ms());
  54. if(last) {
  55. timer_stop(h->tmr);
  56. gpio_set(h->io, h->stop_level);
  57. }
  58. }
  59. /////////////////////////////////////////////
  60. int beep_init(void)
  61. {
  62. beep_handle_t *h=&bpHandle;
  63. h->io = gpio_init(BEEP_GPIO, GPIO_OUT);
  64. gpio_set(h->io, BEEP_OFF_LEVEL);
  65. h->tmr = -1;
  66. //beep_para_t para={1000, 1000, 10};
  67. //beep_set_ex(&para);
  68. return 0;
  69. }
  70. int beep_deinit(void)
  71. {
  72. beep_handle_t *h=&bpHandle;
  73. gpio_deinit(h->io);
  74. timer_stop(h->tmr);
  75. h->io = -1;
  76. h->tmr = -1;
  77. return 0;
  78. }
  79. int beep_set(int mode)
  80. {
  81. beep_handle_t *h=&bpHandle;
  82. if(h->tmr!=-1) {
  83. timer_stop(h->tmr);
  84. }
  85. h->cnt = 0;
  86. h->para = beep_para[mode];
  87. if(h->para.on_ms>0) {
  88. h->para.times *=2;
  89. h->tmr = timer_start(h->para.on_ms, h->para.off_ms, h->para.times, tmr_handler, h);
  90. }
  91. return 0;
  92. }
  93. int beep_set_ex(beep_para_t *para)
  94. {
  95. beep_handle_t *h=&bpHandle;
  96. if(!para) {
  97. return -1;
  98. }
  99. if(h->tmr!=-1) {
  100. timer_stop(h->tmr);
  101. }
  102. h->cnt = 0;
  103. h->para = *para;
  104. if(h->para.on_ms>0) {
  105. h->stop_level = BEEP_OFF_LEVEL;
  106. if(h->para.off_ms>0) {
  107. h->para.times *= 2;
  108. h->tmr = timer_start(h->para.on_ms, h->para.off_ms, h->para.times, tmr_handler, h);
  109. }
  110. else {
  111. h->para.times = 1;
  112. gpio_set(h->io, BEEP_ON_LEVEL);
  113. h->tmr = timer_start(h->para.on_ms*para->times, 0, 1, tmr_handler, h);
  114. }
  115. }
  116. return 0;
  117. }