Contiki 2.5
beep.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: beep.c,v 1.4 2006/07/07 06:36:38 nifi Exp $
32  */
33 
34 #include "contiki.h"
35 #include "contiki-esb.h"
36 
37 #define ON 1
38 #define OFF 0
39 
40 /*
41  * Flag to indicate if any of these functions should generate a sound
42  * or not. The function beep_off() is used to change this flag so none
43  * of the functions will generate sounds and beep_on() to turn sound
44  * back on.
45  */
46 static char onoroff = ON;
47 
48 /*
49  * BEEPER_BIT is the bit in the io-register that is connected to the actual
50  * beeper, setting the bit high vill generate a high pitch tone.
51  */
52 #define BEEPER_BIT 0x08
53 
54 /*-----------------------------------------------------------------------------------*/
55 void
56 beep_alarm(int alarmmode, int len)
57 {
58  len = len / 200;
59 
60  while(len > 0) {
61  /*
62  * Check here if we should beep or not since if we do it outside the
63  * while loop the call to this function would take muck less time, i.e.
64  * beep_on()/beep_off() would have side effects that might not be
65  * predictable.
66  */
67  if(onoroff == ON) {
68  if((alarmmode == BEEP_ALARM1) && ((len & 7) > 4)) {
69  P2OUT |= BEEPER_BIT;
70  } else if((alarmmode == BEEP_ALARM2) && ((len & 15) > 12)) {
71  P2OUT |= BEEPER_BIT;
72  } else {
73  P2OUT &= ~BEEPER_BIT;
74  }
75  }
76  clock_delay(200);
77  len--;
78  }
79  P2OUT &= ~BEEPER_BIT;
80 }
81 /*-----------------------------------------------------------------------------------*/
82 void
83 beep_beep(int i)
84 {
85  if(onoroff == ON) {
86  /* Beep. */
87  P2OUT |= BEEPER_BIT;
88  clock_delay(i);
89  P2OUT &= ~BEEPER_BIT;
90  }
91 }
92 /*-----------------------------------------------------------------------------------*/
93 void
94 beep(void)
95 {
96  beep_beep(20);
97 }
98 /*-----------------------------------------------------------------------------------*/
99 void
100 beep_down(int d)
101 {
102  int i;
103  for(i = 8; i < d; i += i / 8) {
104  beep_beep(10);
105  clock_delay(i);
106  }
107 }
108 /*-----------------------------------------------------------------------------------*/
109 void
110 beep_on(void)
111 {
112  onoroff = ON;
113 }
114 /*-----------------------------------------------------------------------------------*/
115 void
116 beep_off(void)
117 {
118  onoroff = OFF;
119 }
120 /*-----------------------------------------------------------------------------------*/
121 void
123 {
124  unsigned int i;
125 
126  for(i = 10000; i > 80; i -= i / 20) {
127  beep_beep(2);
128  clock_delay(i);
129  }
130 
131  for(i = 4980; i > 2000; i -= 20) {
132  leds_on(LEDS_ALL);
133  clock_delay(5000 - i);
134  leds_off(LEDS_ALL);
135  clock_delay(i);
136  }
137 
138 }
139 /*-----------------------------------------------------------------------------------*/
140 void
141 beep_quick(int n)
142 {
143  int i;
144  for(i = 0; i < n; ++i) {
145  beep_beep(2000);
146  clock_delay(20000);
147  }
148 }
149 /*-----------------------------------------------------------------------------------*/
150 void beep_long(clock_time_t len) {
151  /*
152  * Check if the beeper is turned on or off, i.e. if a call should generate
153  * a noise or not.
154  */
155  if(onoroff == ON) {
156  /* Turn on the beeper. */
157  P2OUT |= BEEPER_BIT;
158  }
159 
160  clock_wait(len);
161 
162  if(onoroff == ON) {
163  /* Turn the beeper off. */
164  P2OUT &= ~BEEPER_BIT;
165  }
166 }
167 /*-----------------------------------------------------------------------------------*/