Contiki 2.5
sound-sensor.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: sound-sensor.c,v 1.5 2010/02/08 00:00:45 nifi Exp $
32  */
33 #include "contiki.h"
34 #include "dev/sound-sensor.h"
35 #include "dev/irq.h"
36 #include <stdlib.h>
37 
38 #define MIC_MIN_SENS 150
39 #define SAMPLE 1
40 
41 const struct sensors_sensor sound_sensor;
42 
43 static unsigned int sound, micdiff, micmax, avgmax;
44 static int8_t mode;
45 static int8_t sample_div;
46 static int8_t ctr;
47 static int16_t *sample_buffer;
48 static int buffer_size;
49 static int buf_pos;
50 
51 /*---------------------------------------------------------------------------*/
52 static int
53 irq(void)
54 {
55  micdiff = micdiff + abs(ADC12MEM4 - sound) - (micdiff >> 3);
56  sound = ADC12MEM4;
57 
58  if(mode == SAMPLE) {
59  ctr++;
60  if(ctr >= sample_div) {
61  ctr = 0;
62  sample_buffer[buf_pos++] = sound;
63  if(buf_pos >= buffer_size) {
64  mode = 0;
65  sensors_changed(&sound_sensor);
66  return 1;
67  }
68  }
69  }
70 
71 /* if(micdiff > MIC_MIN_SENS) { */
72 /* sensors_changed(&sound_sensor); */
73 /* } */
74 
75 /* if(micdiff > (avgmax >> 2)) { */
76 /* if(micdiff % 10 == 0) beep_beep(10); */
77 /* // Subtract a little... */
78 /* micdiff = micdiff - (micdiff >> 4); */
79 /* } */
80 
81 /* if(micmax < micdiff) { */
82 /* micmax = micdiff; */
83 /* } */
84 
85 /* if(micdiff > 2000) { */
86 /* leds_on(LEDS_GREEN); */
87 /* } */
88 /* if(micdiff > 3000) { */
89 /* leds_on(LEDS_YELLOW); */
90 /* } */
91 /* if(micdiff > 4000) { */
92 /* leds_on(LEDS_RED); */
93 /* } */
94 
95  return 0;
96 }
97 /*---------------------------------------------------------------------------*/
98 static int
99 value(int type)
100 {
101  /* try returning the max to see what values we get... */
102 /* int mictmp = micmax; */
103 /* avgmax = avgmax + micmax - (avgmax >> 3); */
104 /* micmax = micdiff; */
105 /* return mictmp; */
106  return micdiff;
107 }
108 /*---------------------------------------------------------------------------*/
109 static int
110 configure(int type, int value)
111 {
112  switch(type) {
113  case SENSORS_HW_INIT:
114  /* Initialization of ADC12 done by irq */
115  mode = 0;
116  buffer_size = 0;
117  return 1;
118  case SENSORS_ACTIVE:
119  if(value) {
120  if(!irq_adc12_active(4)) {
121  sound = micdiff = micmax = 0;
122  mode = 0;
123  ctr = 0;
124  sample_div = 0;
125  buf_pos = 0;
126  avgmax = 5000;
127  irq_adc12_activate(4, (INCH_0 + SREF_0), irq);
128  }
129  } else {
130  irq_adc12_deactivate(4);
131  }
132  return 1;
133  }
134  return 0;
135 }
136 /*---------------------------------------------------------------------------*/
137 static int
138 status(int type)
139 {
140  switch(type) {
141  case SENSORS_ACTIVE:
142  return irq_adc12_active(4);
143  case SENSORS_READY:
144  return (mode != SAMPLE) && irq_adc12_active(4);
145  }
146  return 0;
147 }
148 /*---------------------------------------------------------------------------*/
149 void
150 sound_sensor_start_sample(void)
151 {
152  if(buffer_size > 0) {
153  buf_pos = 0;
154  ctr = 0;
155  mode = SAMPLE;
156  }
157 }
158 /*---------------------------------------------------------------------------*/
159 void
160 sound_sensor_set_buffer(int16_t *buffer, int buf_size, int divider)
161 {
162  sample_buffer = buffer;
163  buffer_size = buf_size;
164  sample_div = divider;
165 }
166 /*---------------------------------------------------------------------------*/
167 SENSORS_SENSOR(sound_sensor, SOUND_SENSOR,
168  value, configure, status);