| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #include "gpio.h"
- #include "timer.h"
- #include "log.h"
- #include "beep.h"
- #include "paras.h"
- #include <stdio.h>
- #include <sys/time.h>
- #define BEEP_GPIO GET_PIN(E, 15)
- #define BEEP_ON_LEVEL 1
- #define BEEP_OFF_LEVEL 0
- static beep_para_t beep_para[BEEP_MODE_MAX]={
- #if 0
- {1000, 1000, 5}, //BEEP_MODE_WARN1
- {1000, 1000, 5}, //BEEP_MODE_WARN1
- {500, 500, 10}, //BEEP_MODE_ERROR
- {200, 200, 25}, //BEEP_MODE_FAULT
- {50, 50, -1}, //BEEP_MODE_FATAL
- #else
- {500, 500, 60}, //BEEP_MODE_WARN1
- {333, 333, 450}, //BEEP_MODE_WARN2
- {500, 500, 10}, //BEEP_MODE_ERROR
- {200, 200, 25}, //BEEP_MODE_FAULT
- {50, 50, -1}, //BEEP_MODE_FATAL
- #endif
- };
- typedef struct {
- int tmr;
- uint32_t cnt;
- beep_para_t para;
- int mode;
- 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(BEEP_GPIO, BEEP_OFF_LEVEL);
- }
- else {
- gpio_set(BEEP_GPIO, BEEP_ON_LEVEL);
- }
- }
-
- if(last) {
- timer_stop(h->tmr);
- gpio_set(BEEP_GPIO, BEEP_OFF_LEVEL);
- }
- }
- /////////////////////////////////////////////
- int beep_init(void)
- {
- beep_handle_t *h=&bpHandle;
-
- gpio_init(BEEP_GPIO, GPIO_OUT, BEEP_OFF_LEVEL);
- h->tmr = timer_init();
- h->mode = -1;
- gpio_set(BEEP_GPIO, BEEP_ON_LEVEL);
- sleep(1);
- gpio_set(BEEP_GPIO, BEEP_OFF_LEVEL);
- //beep_set(BEEP_MODE_WARN1);
- return 0;
- }
- int beep_deinit(void)
- {
- beep_handle_t *h=&bpHandle;
-
- gpio_deinit(BEEP_GPIO);
- timer_deinit(h->tmr);
- h->tmr = -1;
-
- return 0;
- }
- int beep_set(int mode)
- {
- beep_handle_t *h=&bpHandle;
-
- h->mode = mode;
- return beep_set_ex(&beep_para[h->mode]);
- }
- int beep_set_ex(beep_para_t *para)
- {
- int r = 0;
- beep_handle_t *h=&bpHandle;
- misc_data_t *misc=¶s_get()->misc;
- // if(!misc->beep || !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(BEEP_GPIO, 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(BEEP_GPIO, 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;
- }
- int beep_reset(void)
- {
- beep_handle_t *h=&bpHandle;
- if(h->mode==-1) {
- return -1;
- }
- h->mode = -1;
- timer_stop(h->tmr);
- gpio_set(BEEP_GPIO, BEEP_OFF_LEVEL);
- return 0;
- }
|