mirror of
https://github.com/eclipse/upm.git
synced 2025-07-02 09:51:14 +03:00
python_examples: Reformatted, self-checking, executable
* Moved body of each python example to main. This allows for basic load module testing for CI * General cleanup of python modules (crlf/tabs/prints/etc) * Chmod'ed to 755 to allow running examples without specifying the python interpreter * Added ctest for loading python2/3 modules * Added jniclasscode pragma for java swig interface files. * Updated check_examplenames.py module to check all languages vs. a cxx example name * Added tests for checking python module and test loading * Added 'make test' to travis-ci run (run ctests) * Print a more meaningful message when not building cxx docs into python modules * Updated check_clean.py to only check java wrapper files * ENABLED ctests for UPM * Deleted using_carrays.py python example - this is covered by other examples Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
74
examples/python/ldt0028.py
Normal file → Executable file
74
examples/python/ldt0028.py
Normal file → Executable file
@ -24,43 +24,47 @@ import time
|
||||
import array
|
||||
import pyupm_ldt0028 as ldt0028
|
||||
|
||||
NUMBER_OF_SECONDS = 10
|
||||
SAMPLES_PER_SECOND = 50
|
||||
THRESHOLD = 100
|
||||
def main():
|
||||
NUMBER_OF_SECONDS = 10
|
||||
SAMPLES_PER_SECOND = 50
|
||||
THRESHOLD = 100
|
||||
|
||||
# Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
|
||||
sensor = ldt0028.LDT0028(0)
|
||||
# Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
|
||||
sensor = ldt0028.LDT0028(0)
|
||||
|
||||
# Read the signal every 20 milliseconds for 10 seconds
|
||||
print 'For the next', NUMBER_OF_SECONDS, 'seconds,', \
|
||||
SAMPLES_PER_SECOND, 'samples will be taken every second.\n'
|
||||
buffer = array.array('H')
|
||||
for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
|
||||
buffer.append(sensor.getSample())
|
||||
time.sleep(1.0/SAMPLES_PER_SECOND)
|
||||
# Read the signal every 20 milliseconds for 10 seconds
|
||||
print 'For the next', NUMBER_OF_SECONDS, 'seconds,', \
|
||||
SAMPLES_PER_SECOND, 'samples will be taken every second.\n'
|
||||
buffer = array.array('H')
|
||||
for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
|
||||
buffer.append(sensor.getSample())
|
||||
time.sleep(1.0/SAMPLES_PER_SECOND)
|
||||
|
||||
# Print the number of times the reading was greater than the threshold
|
||||
count = 0
|
||||
for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
|
||||
if buffer[i] > THRESHOLD:
|
||||
count += 1
|
||||
print sensor.name(), ' exceeded the threshold value of', \
|
||||
THRESHOLD, 'a total of', count, 'times,'
|
||||
print 'out of a total of', NUMBER_OF_SECONDS*SAMPLES_PER_SECOND, \
|
||||
'reading.\n'
|
||||
# Print the number of times the reading was greater than the threshold
|
||||
count = 0
|
||||
for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
|
||||
if buffer[i] > THRESHOLD:
|
||||
count += 1
|
||||
print sensor.name(), ' exceeded the threshold value of', \
|
||||
THRESHOLD, 'a total of', count, 'times,'
|
||||
print 'out of a total of', NUMBER_OF_SECONDS*SAMPLES_PER_SECOND, \
|
||||
'reading.\n'
|
||||
|
||||
# Print a graphical representation of the average value sampled
|
||||
# each second for the past 10 seconds, using a scale factor of 15
|
||||
print 'Now printing a graphical representation of the average reading '
|
||||
print 'each second for the last', NUMBER_OF_SECONDS, 'seconds.'
|
||||
SCALE_FACTOR = 15
|
||||
for i in range(0, NUMBER_OF_SECONDS):
|
||||
sum = 0
|
||||
for j in range(0, SAMPLES_PER_SECOND):
|
||||
sum += buffer[i*SAMPLES_PER_SECOND+j]
|
||||
average = sum / SAMPLES_PER_SECOND
|
||||
stars_to_print = int(round(average / SCALE_FACTOR))
|
||||
print '(' + repr(int(round(average))).rjust(4) + ') |', '*' * stars_to_print
|
||||
# Print a graphical representation of the average value sampled
|
||||
# each second for the past 10 seconds, using a scale factor of 15
|
||||
print 'Now printing a graphical representation of the average reading '
|
||||
print 'each second for the last', NUMBER_OF_SECONDS, 'seconds.'
|
||||
SCALE_FACTOR = 15
|
||||
for i in range(0, NUMBER_OF_SECONDS):
|
||||
sum = 0
|
||||
for j in range(0, SAMPLES_PER_SECOND):
|
||||
sum += buffer[i*SAMPLES_PER_SECOND+j]
|
||||
average = sum / SAMPLES_PER_SECOND
|
||||
stars_to_print = int(round(average / SCALE_FACTOR))
|
||||
print '(' + repr(int(round(average))).rjust(4) + ') |', '*' * stars_to_print
|
||||
|
||||
# Delete the sensor object
|
||||
del sensor
|
||||
# Delete the sensor object
|
||||
del sensor
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Reference in New Issue
Block a user