1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <iostream>
26 #include <string>
27 #include <stdexcept>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <functional>
31 #include <string.h>
32 #include "mic.h"
33 
34 using namespace upm;
35 
Microphone(int micPin)36 Microphone::Microphone(int micPin) {
37     // initialise analog mic input
38 
39     if ( !(m_micCtx = mraa_aio_init(micPin)) )
40       {
41         throw std::invalid_argument(std::string(__FUNCTION__) +
42                                     ": mraa_aio_init() failed, invalid pin?");
43         return;
44       }
45 }
46 
~Microphone()47 Microphone::~Microphone() {
48     // close analog input
49     mraa_result_t error;
50     error = mraa_aio_close(m_micCtx);
51     if (error != MRAA_SUCCESS) {
52         mraa_result_print(error);
53     }
54 }
55 
56 int
getSampledWindow(unsigned int freqMS,int numberOfSamples,uint16_t * buffer)57 Microphone::getSampledWindow (unsigned int freqMS, int numberOfSamples,
58                             uint16_t * buffer) {
59     int sampleIdx = 0;
60 
61     // must have freq
62     if (!freqMS) {
63         return 0;
64     }
65 
66     // too much samples
67     if (numberOfSamples > 0xFFFFFF) {
68         return 0;
69     }
70 
71     while (sampleIdx < numberOfSamples) {
72         buffer[sampleIdx++] = mraa_aio_read (m_micCtx);
73         usleep(freqMS * 1000);
74     }
75 
76     return sampleIdx;
77 }
78 
79 int
findThreshold(thresholdContext * ctx,unsigned int threshold,uint16_t * buffer,int len)80 Microphone::findThreshold (thresholdContext* ctx, unsigned int threshold,
81                                 uint16_t * buffer, int len) {
82     long sum = 0;
83     for (unsigned int i = 0; i < len; i++) {
84         sum += buffer[i];
85     }
86 
87     ctx->averageReading = sum / len;
88     ctx->runningAverage = (((ctx->averagedOver-1) * ctx->runningAverage) + ctx->averageReading) / ctx->averagedOver;
89 
90     if (ctx->runningAverage > threshold) {
91         return ctx->runningAverage;
92     } else {
93         return 0;
94     }
95 }
96 
97 void
printGraph(thresholdContext * ctx)98 Microphone::printGraph (thresholdContext* ctx) {
99     for (int i = 0; i < ctx->runningAverage; i++)
100         std::cout << ".";
101     std::cout << std::endl;
102 }
103