| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #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]={
- {50, 100, 10}, //BEEP_MODE_WARN
- {50, 100, 10}, //BEEP_MODE_ERROR
- {50, 100, 10}, //BEEP_MODE_EMERG
- {50, 100, 10}, //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 uint64_t get_ms(void)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (uint64_t)(tv.tv_sec)*1000 + (uint64_t)(tv.tv_usec)/1000;
- }
- static void tmr_handler(void *arg, int last)
- {
- beep_handle_t *h=(beep_handle_t*)arg;
-
- h->cnt++;
- if(h->cnt%2) {
- if(h->para.off_ms) {
- //gpio_set(h->io, BEEP_ON_LEVEL);
- //timer_setup(h->tmr, h->para.off_ms, 0);
- }
- }
- else {
- if(h->para.on_ms) {
- //gpio_set(h->io, BEEP_OFF_LEVEL);
- //timer_setup(h->tmr, h->para.off_ms, 0);
- }
- }
- printf("___cnt: %d, %lld\n", h->cnt, get_ms());
-
- if(last) {
- timer_stop(h->tmr);
- gpio_set(h->io, h->stop_level);
- }
- }
- /////////////////////////////////////////////
- int beep_init(void)
- {
- beep_handle_t *h=&bpHandle;
-
- h->io = gpio_init(BEEP_GPIO, GPIO_OUT);
- gpio_set(h->io, BEEP_OFF_LEVEL);
- h->tmr = -1;
- //beep_para_t para={1000, 1000, 10};
- //beep_set_ex(¶);
- return 0;
- }
- int beep_deinit(void)
- {
- beep_handle_t *h=&bpHandle;
-
- gpio_deinit(h->io);
- timer_stop(h->tmr);
- h->io = -1;
- h->tmr = -1;
-
- return 0;
- }
- int beep_set(int mode)
- {
- beep_handle_t *h=&bpHandle;
-
- if(h->tmr!=-1) {
- timer_stop(h->tmr);
- }
- h->cnt = 0;
- h->para = beep_para[mode];
- if(h->para.on_ms>0) {
- h->para.times *=2;
- h->tmr = timer_start(h->para.on_ms, h->para.off_ms, h->para.times, tmr_handler, h);
- }
-
- return 0;
- }
- int beep_set_ex(beep_para_t *para)
- {
- beep_handle_t *h=&bpHandle;
-
- if(!para) {
- return -1;
- }
-
- if(h->tmr!=-1) {
- timer_stop(h->tmr);
- }
-
- h->cnt = 0;
- h->para = *para;
- if(h->para.on_ms>0) {
- h->stop_level = BEEP_OFF_LEVEL;
- if(h->para.off_ms>0) {
- h->para.times *= 2;
- h->tmr = timer_start(h->para.on_ms, h->para.off_ms, h->para.times, tmr_handler, h);
- }
- else {
- h->para.times = 1;
- gpio_set(h->io, BEEP_ON_LEVEL);
- h->tmr = timer_start(h->para.on_ms*para->times, 0, 1, tmr_handler, h);
- }
- }
-
- return 0;
- }
|