Contiki 2.6

beep.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: beep.c,v 1.4 2006/07/07 06:36:38 nifi Exp $
00032  */
00033 
00034 #include "contiki.h"
00035 #include "contiki-esb.h"
00036 #include "sys/clock.h"
00037 
00038 #define ON  1
00039 #define OFF 0
00040 
00041 /*
00042  * Flag to indicate if any of these functions should generate a sound
00043  * or not. The function beep_off() is used to change this flag so none
00044  * of the functions will generate sounds and beep_on() to turn sound
00045  * back on.
00046  */
00047 static char onoroff = ON;
00048 
00049 /*
00050  * BEEPER_BIT is the bit in the io-register that is connected to the actual 
00051  * beeper, setting the bit high vill generate a high pitch tone.
00052  */
00053 #define BEEPER_BIT 0x08
00054 
00055 /*-----------------------------------------------------------------------------------*/
00056 void
00057 beep_alarm(int alarmmode, int len)
00058 {
00059   len = len / 200;
00060   
00061   while(len > 0) {
00062     /*
00063      * Check here if we should beep or not since if we do it outside the
00064      * while loop the call to this function would take muck less time, i.e.
00065      * beep_on()/beep_off() would have side effects that might not be
00066      * predictable.
00067      */
00068     if(onoroff == ON) {
00069       if((alarmmode == BEEP_ALARM1) && ((len & 7) > 4)) {
00070         P2OUT |= BEEPER_BIT;
00071       } else if((alarmmode == BEEP_ALARM2) && ((len & 15) > 12)) {
00072         P2OUT |= BEEPER_BIT;
00073       } else {
00074         P2OUT &= ~BEEPER_BIT;
00075       }
00076     }
00077     clock_delay(200);
00078     len--;
00079   }
00080   P2OUT &= ~BEEPER_BIT;
00081 }
00082 /*-----------------------------------------------------------------------------------*/
00083 void
00084 beep_beep(int i)
00085 {
00086   if(onoroff == ON) {
00087     /* Beep. */
00088     P2OUT |= BEEPER_BIT;
00089     clock_delay(i);
00090     P2OUT &= ~BEEPER_BIT;
00091   }
00092 }
00093 /*-----------------------------------------------------------------------------------*/
00094 void
00095 beep(void)
00096 {
00097   beep_beep(20);
00098 }
00099 /*-----------------------------------------------------------------------------------*/
00100 void
00101 beep_down(int d)
00102 {
00103   int i;
00104   for(i = 8; i < d; i += i / 8) {
00105     beep_beep(10);
00106     clock_delay(i);
00107   }
00108 }
00109 /*-----------------------------------------------------------------------------------*/
00110 void
00111 beep_on(void)
00112 {
00113   onoroff = ON;
00114 }
00115 /*-----------------------------------------------------------------------------------*/
00116 void
00117 beep_off(void)
00118 {
00119   onoroff = OFF;
00120 }
00121 /*-----------------------------------------------------------------------------------*/
00122 void
00123 beep_spinup(void)
00124 {
00125   unsigned int i;
00126 
00127   for(i = 10000; i > 80; i -= i / 20) {
00128     beep_beep(2);
00129     clock_delay(i);
00130   }
00131 
00132   for(i = 4980; i > 2000; i -= 20) {
00133     leds_on(LEDS_ALL);
00134     clock_delay(5000 - i);
00135     leds_off(LEDS_ALL);
00136     clock_delay(i);
00137   }
00138 
00139 }
00140 /*-----------------------------------------------------------------------------------*/
00141 void
00142 beep_quick(int n)
00143 {
00144   int i;
00145   for(i = 0; i < n; ++i) {
00146     beep_beep(2000);
00147     clock_delay(20000);
00148   }
00149 }
00150 /*-----------------------------------------------------------------------------------*/
00151 void beep_long(clock_time_t len) {
00152   /*
00153    * Check if the beeper is turned on or off, i.e. if a call should generate 
00154    * a noise or not.
00155    */
00156   if(onoroff == ON) {
00157     /* Turn on the beeper. */
00158     P2OUT |= BEEPER_BIT;
00159   }
00160 
00161   clock_wait(len);
00162 
00163   if(onoroff == ON) {
00164     /* Turn the beeper off. */
00165     P2OUT &= ~BEEPER_BIT;
00166   }
00167 }
00168 /*-----------------------------------------------------------------------------------*/