• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Author: Sarah Knepper <sarah.knepper@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 <unistd.h>
26 #include <iostream>
27 #include <iomanip>
28 #include <cmath>
29 #include "ldt0028.h"
30 
31 int
main(int argc,char ** argv)32 main(int argc, char **argv)
33 {
34 //! [Interesting]
35     const int NUMBER_OF_SECONDS = 10;
36     const int MICROSECONDS_PER_SECOND = 1000000;
37     const int SAMPLES_PER_SECOND = 50;
38     const int THRESHOLD = 100;
39 
40     // Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
41     upm::LDT0028* sensor = new upm::LDT0028(0);
42 
43     // Read the signal every 20 milliseconds for 10 seconds
44     std::cout << "For the next " << NUMBER_OF_SECONDS << " seconds, "
45               << SAMPLES_PER_SECOND << " samples will be taken every second."
46               << std::endl << std::endl;
47     uint16_t* buffer = new uint16_t[NUMBER_OF_SECONDS * SAMPLES_PER_SECOND];
48     for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
49         buffer[i] = (uint16_t) sensor->getSample();
50         usleep(MICROSECONDS_PER_SECOND / SAMPLES_PER_SECOND);
51     }
52 
53     // Print the number of times the reading was greater than the threshold
54     int count = 0;
55     for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
56         if (buffer[i] > THRESHOLD) {
57             count++;
58         }
59     }
60     std::cout << sensor->name() << " exceeded the threshold value of " <<
61         THRESHOLD << " a total of " << count << " times," << std::endl
62         << "out of a total of " << NUMBER_OF_SECONDS*SAMPLES_PER_SECOND
63         << " readings." << std::endl << std::endl;
64 
65     // Print a graphical representation of the average value sampled
66     // each second for the past 10 seconds, using a scale factor of 15
67     std::cout << "Now printing a graphical representation of the average reading "
68         << std::endl << "each second for the last "
69         << NUMBER_OF_SECONDS << " seconds." << std::endl;
70     const int SCALE_FACTOR = 15;
71     for (int i=0; i < NUMBER_OF_SECONDS; i++) {
72         long sum = 0;
73         for (int j=0; j < SAMPLES_PER_SECOND; j++) {
74             sum += buffer[i*SAMPLES_PER_SECOND + j];
75         }
76         double average = (double) sum / (double) SAMPLES_PER_SECOND;
77         int stars_to_print = (int) round(average / SCALE_FACTOR);
78         std::cout << "(" << std::setw(4) << (int) round(average) << ") | ";
79         for (int j=0; j<stars_to_print; j++) {
80             std::cout << "*";
81         }
82         std::cout << std::endl;
83     }
84 
85     // Delete the sensor object
86     delete sensor;
87 //! [Interesting]
88 
89     return 0;
90 }
91