Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Author: Sarah Knepper <sarah.knepper@intel.com>
  2. # Copyright (c) 2014 Intel Corporation.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining
  5. # a copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and to
  9. # permit persons to whom the Software is furnished to do so, subject to
  10. # the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. import time
  23. import array
  24. import pyupm_ldt0028 as ldt0028
  25. NUMBER_OF_SECONDS = 10
  26. SAMPLES_PER_SECOND = 50
  27. THRESHOLD = 100
  28. # Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
  29. sensor = ldt0028.LDT0028(0)
  30. # Read the signal every 20 milliseconds for 10 seconds
  31. print 'For the next', NUMBER_OF_SECONDS, 'seconds,', \
  32. SAMPLES_PER_SECOND, 'samples will be taken every second.\n'
  33. buffer = array.array('H')
  34. for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
  35. buffer.append(sensor.getSample())
  36. time.sleep(1.0/SAMPLES_PER_SECOND)
  37. # Print the number of times the reading was greater than the threshold
  38. count = 0
  39. for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
  40. if buffer[i] > THRESHOLD:
  41. count += 1
  42. print sensor.name(), ' exceeded the threshold value of', \
  43. THRESHOLD, 'a total of', count, 'times,'
  44. print 'out of a total of', NUMBER_OF_SECONDS*SAMPLES_PER_SECOND, \
  45. 'reading.\n'
  46. # Print a graphical representation of the average value sampled
  47. # each second for the past 10 seconds, using a scale factor of 15
  48. print 'Now printing a graphical representation of the average reading '
  49. print 'each second for the last', NUMBER_OF_SECONDS, 'seconds.'
  50. SCALE_FACTOR = 15
  51. for i in range(0, NUMBER_OF_SECONDS):
  52. sum = 0
  53. for j in range(0, SAMPLES_PER_SECOND):
  54. sum += buffer[i*SAMPLES_PER_SECOND+j]
  55. average = sum / SAMPLES_PER_SECOND
  56. stars_to_print = int(round(average / SCALE_FACTOR))
  57. print '(' + repr(int(round(average))).rjust(4) + ') |', '*' * stars_to_print
  58. # Delete the sensor object
  59. del sensor