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: sound-sensor.c,v 1.5 2010/02/08 00:00:45 nifi Exp $ 00032 */ 00033 #include "dev/sound-sensor.h" 00034 #include "dev/irq.h" 00035 #include <stdlib.h> 00036 #include <io.h> 00037 00038 #define MIC_MIN_SENS 150 00039 #define SAMPLE 1 00040 00041 const struct sensors_sensor sound_sensor; 00042 00043 static unsigned int sound, micdiff, micmax, avgmax; 00044 static int8_t mode; 00045 static int8_t sample_div; 00046 static int8_t ctr; 00047 static int16_t *sample_buffer; 00048 static int buffer_size; 00049 static int buf_pos; 00050 00051 /*---------------------------------------------------------------------------*/ 00052 static int 00053 irq(void) 00054 { 00055 micdiff = micdiff + abs(ADC12MEM4 - sound) - (micdiff >> 3); 00056 sound = ADC12MEM4; 00057 00058 if(mode == SAMPLE) { 00059 ctr++; 00060 if(ctr >= sample_div) { 00061 ctr = 0; 00062 sample_buffer[buf_pos++] = sound; 00063 if(buf_pos >= buffer_size) { 00064 mode = 0; 00065 sensors_changed(&sound_sensor); 00066 return 1; 00067 } 00068 } 00069 } 00070 00071 /* if(micdiff > MIC_MIN_SENS) { */ 00072 /* sensors_changed(&sound_sensor); */ 00073 /* } */ 00074 00075 /* if(micdiff > (avgmax >> 2)) { */ 00076 /* if(micdiff % 10 == 0) beep_beep(10); */ 00077 /* // Subtract a little... */ 00078 /* micdiff = micdiff - (micdiff >> 4); */ 00079 /* } */ 00080 00081 /* if(micmax < micdiff) { */ 00082 /* micmax = micdiff; */ 00083 /* } */ 00084 00085 /* if(micdiff > 2000) { */ 00086 /* leds_on(LEDS_GREEN); */ 00087 /* } */ 00088 /* if(micdiff > 3000) { */ 00089 /* leds_on(LEDS_YELLOW); */ 00090 /* } */ 00091 /* if(micdiff > 4000) { */ 00092 /* leds_on(LEDS_RED); */ 00093 /* } */ 00094 00095 return 0; 00096 } 00097 /*---------------------------------------------------------------------------*/ 00098 static int 00099 value(int type) 00100 { 00101 /* try returning the max to see what values we get... */ 00102 /* int mictmp = micmax; */ 00103 /* avgmax = avgmax + micmax - (avgmax >> 3); */ 00104 /* micmax = micdiff; */ 00105 /* return mictmp; */ 00106 return micdiff; 00107 } 00108 /*---------------------------------------------------------------------------*/ 00109 static int 00110 configure(int type, int value) 00111 { 00112 switch(type) { 00113 case SENSORS_HW_INIT: 00114 /* Initialization of ADC12 done by irq */ 00115 mode = 0; 00116 buffer_size = 0; 00117 return 1; 00118 case SENSORS_ACTIVE: 00119 if(value) { 00120 if(!irq_adc12_active(4)) { 00121 sound = micdiff = micmax = 0; 00122 mode = 0; 00123 ctr = 0; 00124 sample_div = 0; 00125 buf_pos = 0; 00126 avgmax = 5000; 00127 irq_adc12_activate(4, (INCH_0 + SREF_0), irq); 00128 } 00129 } else { 00130 irq_adc12_deactivate(4); 00131 } 00132 return 1; 00133 } 00134 return 0; 00135 } 00136 /*---------------------------------------------------------------------------*/ 00137 static int 00138 status(int type) 00139 { 00140 switch(type) { 00141 case SENSORS_ACTIVE: 00142 return irq_adc12_active(4); 00143 case SENSORS_READY: 00144 return (mode != SAMPLE) && irq_adc12_active(4); 00145 } 00146 return 0; 00147 } 00148 /*---------------------------------------------------------------------------*/ 00149 void 00150 sound_sensor_start_sample(void) 00151 { 00152 if(buffer_size > 0) { 00153 buf_pos = 0; 00154 ctr = 0; 00155 mode = SAMPLE; 00156 } 00157 } 00158 /*---------------------------------------------------------------------------*/ 00159 void 00160 sound_sensor_set_buffer(int16_t *buffer, int buf_size, int divider) 00161 { 00162 sample_buffer = buffer; 00163 buffer_size = buf_size; 00164 sample_div = divider; 00165 } 00166 /*---------------------------------------------------------------------------*/ 00167 SENSORS_SENSOR(sound_sensor, SOUND_SENSOR, 00168 value, configure, status);