暫無描述

ldt0028.cxx 3.5KB

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