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
23import time
24import array
25import pyupm_ldt0028 as ldt0028
26
27NUMBER_OF_SECONDS = 10
28SAMPLES_PER_SECOND = 50
29THRESHOLD = 100
30
31# Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
32sensor = ldt0028.LDT0028(0)
33
34# Read the signal every 20 milliseconds for 10 seconds
35print 'For the next', NUMBER_OF_SECONDS, 'seconds,', \
36      SAMPLES_PER_SECOND, 'samples will be taken every second.\n'
37buffer = array.array('H')
38for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
39    buffer.append(sensor.getSample())
40    time.sleep(1.0/SAMPLES_PER_SECOND)
41
42# Print the number of times the reading was greater than the threshold
43count = 0
44for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
45    if buffer[i] > THRESHOLD:
46        count += 1
47print sensor.name(), ' exceeded the threshold value of', \
48        THRESHOLD, 'a total of', count, 'times,'
49print 'out of a total of', NUMBER_OF_SECONDS*SAMPLES_PER_SECOND, \
50        'reading.\n'
51
52# Print a graphical representation of the average value sampled
53# each second for the past 10 seconds, using a scale factor of 15
54print 'Now printing a graphical representation of the average reading '
55print 'each second for the last', NUMBER_OF_SECONDS, 'seconds.'
56SCALE_FACTOR = 15
57for i in range(0, NUMBER_OF_SECONDS):
58    sum = 0
59    for j in range(0, SAMPLES_PER_SECOND):
60        sum += buffer[i*SAMPLES_PER_SECOND+j]
61    average = sum / SAMPLES_PER_SECOND
62    stars_to_print = int(round(average / SCALE_FACTOR))
63    print '(' + repr(int(round(average))).rjust(4) + ') |', '*' * stars_to_print
64
65# Delete the sensor object
66del sensor
67