Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / drv / buzzer.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2003 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief Buzzer driver (implementation)
35  *
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Francesco Sacchi <batt@develer.com>
38  */
39
40 #include "buzzer.h"
41
42 #include "hw/hw_buzzer.h"
43 #include <drv/timer.h>
44
45 #include <mware/event.h>
46
47 #include <cfg/debug.h>
48 #include <cfg/module.h>
49
50
51 /* Local vars */
52 static Timer buz_timer;
53 static bool buz_timer_running;
54 static mtime_t buz_repeat_interval;
55 static mtime_t buz_repeat_duration;
56
57
58 /**
59  * Turn off buzzer, called by software timer
60  */
61 static void buz_softint(void)
62 {
63         if (IS_BUZZER_ON)
64         {
65                 BUZZER_OFF;
66                 if (buz_repeat_interval)
67                 {
68                         /* Wait for interval time */
69                         timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_interval));
70                         timer_add(&buz_timer);
71                 }
72                 else
73                         buz_timer_running = false;
74         }
75         else if (buz_repeat_interval)
76         {
77                 /* Wait for beep time */
78                 BUZZER_ON;
79                 timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_duration));
80                 timer_add(&buz_timer);
81         }
82         else
83                 buz_timer_running = false;
84 }
85
86
87 /**
88  * Beep for the specified ms time
89  */
90 void buz_beep(mtime_t time)
91 {
92         cpu_flags_t flags;
93         IRQ_SAVE_DISABLE(flags);
94
95         /* Remove the software interrupt if it was already queued */
96         if (buz_timer_running)
97                 timer_abort(&buz_timer);
98
99         /* Turn on buzzer */
100         BUZZER_ON;
101
102         /* Add software interrupt to turn the buzzer off later */
103         buz_timer_running = true;
104         timer_setDelay(&buz_timer, ms_to_ticks(time));
105         timer_add(&buz_timer);
106
107         IRQ_RESTORE(flags);
108 }
109
110
111 /**
112  * Start buzzer repetition
113  */
114 void buz_repeat_start(mtime_t duration, mtime_t interval)
115 {
116         buz_repeat_interval = interval;
117         buz_repeat_duration = duration;
118         buz_beep(duration);
119 }
120
121
122 /**
123  * Stop buzzer repetition
124  */
125 void buz_repeat_stop(void)
126 {
127         cpu_flags_t flags;
128         IRQ_SAVE_DISABLE(flags);
129
130         /* Remove the software interrupt if it was already queued */
131         if (buz_timer_running)
132         {
133                 timer_abort(&buz_timer);
134                 buz_timer_running = false;
135         }
136
137         buz_repeat_interval = 0;
138         BUZZER_OFF;
139
140         IRQ_RESTORE(flags);
141 }
142
143 MOD_DEFINE(buzzer)
144
145 /**
146  * Initialize buzzer.
147  */
148 void buz_init(void)
149 {
150         MOD_CHECK(timer);
151
152         BUZZER_HW_INIT;
153
154         /* Init software interrupt. */
155         timer_setSoftint(&buz_timer, (Hook)buz_softint, 0);
156
157         MOD_INIT(buzzer);
158 }