| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #include "gpio.h"
- #include "timer.h"
- #include "elog.h"
- #include "beep.h"
- #include <stdio.h>
- #include <sys/time.h>
- #if 1
- #define LOGD log_d
- #define LOGE log_e
- #else
- #define LOGD printf
- #define LOGE printf
- #endif
- #define BEEP_GPIO 103 //PD7=96+7
- #define BEEP_ON_LEVEL 1
- #define BEEP_OFF_LEVEL 0
- static beep_para_t beep_para[BEEP_MODE_MAX]={
- {1000, 1000, 5}, //BEEP_MODE_WARN
- {500, 500, 10}, //BEEP_MODE_ERROR
- {200, 200, 25}, //BEEP_MODE_FAULT
- {50, 50, -1}, //BEEP_MODE_FATAL
- };
- typedef struct {
- int io;
- int tmr;
- uint32_t cnt;
- beep_para_t para;
- int stop_level;
- }beep_handle_t;
- static beep_handle_t bpHandle={0};
- static void beep_handler(void *arg, int last)
- {
- beep_handle_t *h=(beep_handle_t*)arg;
-
- if(h->para.on_ms>0 && h->para.off_ms>0) {
- h->cnt++;
- if(h->cnt%2) {
- gpio_set(h->io, BEEP_OFF_LEVEL);
- }
- else {
- gpio_set(h->io, BEEP_ON_LEVEL);
- }
- }
-
- if(last) {
- timer_stop(h->tmr);
- gpio_set(h->io, BEEP_OFF_LEVEL);
- }
- }
- /////////////////////////////////////////////
- int beep_init(void)
- {
- beep_handle_t *h=&bpHandle;
-
- h->io = gpio_init(BEEP_GPIO, GPIO_OUT);
- h->tmr = timer_init();
- gpio_set(h->io, BEEP_OFF_LEVEL);
- h->tmr = -1;
- //beep_set(BEEP_MODE_FATAL);
- return 0;
- }
- int beep_deinit(void)
- {
- beep_handle_t *h=&bpHandle;
-
- gpio_deinit(h->io);
- timer_deinit(h->tmr);
- h->io = -1;
- h->tmr = -1;
-
- return 0;
- }
- int beep_set(int mode)
- {
- beep_handle_t *h=&bpHandle;
-
- return beep_set_ex(&beep_para[mode]);
- }
- int beep_set_ex(beep_para_t *para)
- {
- int r = 0;
- beep_handle_t *h=&bpHandle;
-
- if(!para || (para->on_ms<=0 && para->off_ms<=0) || para->times==0) {
- return -1;
- }
-
- h->cnt = 0;
- h->para = *para;
- if(h->para.on_ms>0) {
- gpio_set(h->io, BEEP_ON_LEVEL);
- if(h->para.off_ms>0) {
- h->para.times *= 2;
- }
- else {
- h->para.on_ms *= h->para.times;
- h->para.times = 1;
- }
- r = timer_start(h->tmr, h->para.on_ms, h->para.times, beep_handler, h);
- }
- else {
- gpio_set(h->io, BEEP_OFF_LEVEL);
- if(h->para.off_ms>0) {
- h->para.off_ms *= h->para.times;
- h->para.times = 1;
- r = timer_start(h->tmr, h->para.off_ms, h->para.times, beep_handler, h);
- }
- }
-
- return r;
- }
|