Tweaks for avr-libc 1.2.x.
[bertos.git] / drv / buzzerled.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2004 Giovanni Bajo
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief Generic library to handle buzzers and leds
10  *
11  * This library is divided into three different layers:
12  *
13  *  - The topmost portable layer is buzzerled.[ch] which exposes a common API
14  *    enable/disable the devices. Basically, it handles the asynchronism to
15  *    implement bld_beep and bld_repeat.
16  *  - The middle layer is CPU-specific and exposes a single main function which
17  *    turns on/off each device.
18  *  - The lower layer is board-specific and communicates with the middle layer
19  *    with any required API. The idea is that devices can be tied to the CPU in
20  *    many different ways (many different pins), so this part should describe
21  *    which devices are present, and how they are connected.
22  *
23  * \version $Id$
24  *
25  * \author Giovanni Bajo <rasky@develer.com>
26  */
27
28 /*#*
29  *#* $Log$
30  *#* Revision 1.5  2004/12/08 09:43:41  bernie
31  *#* Add a todo item.
32  *#*
33  *#* Revision 1.4  2004/08/25 14:12:08  rasky
34  *#* Aggiornato il comment block dei log RCS
35  *#*
36  *#* Revision 1.3  2004/07/14 14:04:29  rasky
37  *#* Merge da SC: spostata bld_set inline perché si ottimizza parecchio tramite propagazione di costanti
38  *#*
39  *#* Revision 1.2  2004/06/03 11:27:09  bernie
40  *#* Add dual-license information.
41  *#*
42  *#* Revision 1.1  2004/05/23 18:36:05  bernie
43  *#* Import buzzerled driver.
44  *#*
45  *#*/
46
47 #include "buzzerled.h"
48 #include "timer.h"
49
50 static struct Timer timers[NUM_BLDS];
51 static bool timer_go[NUM_BLDS];
52
53 INLINE enum BLD_DEVICE hook_parm_to_device(void* parm)
54 {
55         struct Timer* t = (struct Timer*)parm;
56         int num_bld = t - &timers[0];
57
58         ASSERT(num_bld >= 0);
59         ASSERT(num_bld < NUM_BLDS);
60
61         return (enum BLD_DEVICE)num_bld;
62 }
63
64 static void hook_turn_off(void* parm)
65 {
66         enum BLD_DEVICE num_bld = hook_parm_to_device(parm);
67         bld_set(num_bld, false);
68 }
69
70 void bld_init(void)
71 {
72         bld_hw_init();
73 }
74
75 void bld_beep(enum BLD_DEVICE device, uint16_t duration)
76 {
77         // \todo This is not reentrant for the same device. FIXME!
78         struct Timer *t = &timers[device];
79         timer_set_delay(t, duration);
80         timer_set_event_softint(t, hook_turn_off, t);
81         timer_add(t);
82
83         bld_set(device, true);
84 }
85
86 void bld_beep_and_wait(enum BLD_DEVICE device, uint16_t duration)
87 {
88         bld_set(device, true);
89         timer_delay(duration);
90         bld_set(device, false);
91 }
92