-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensor-threaded.py
57 lines (42 loc) · 1.51 KB
/
sensor-threaded.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python3
"""
Program: HC-SR04 Sensor Demo (sensor-threaded.py)
Author: M. Heidenreich, (c) 2020
Description:
This code is provided in support of the following YouTube tutorial:
https://youtu.be/JvQKZXCYMUM
This example shows how to use the HC-SR04 sensor to provide a continuous
distance readout with Raspberry Pi using a multi-threaded approach.
THIS SOFTWARE AND LINKED VIDEO TOTORIAL ARE PROVIDED "AS IS" AND THE
AUTHOR DISCLAIMS ALL WARRANTIES INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
from signal import signal, SIGTERM, SIGHUP, pause
from time import sleep
from threading import Thread
from gpiozero import DistanceSensor
reading = True
sensor = DistanceSensor(echo=20, trigger=21)
def safe_exit(signum, frame):
exit(1)
def read_distance():
while reading:
message = f"Distance: {sensor.value:1.2f} m"
print(message)
sleep(0.1)
try:
signal(SIGTERM, safe_exit)
signal(SIGHUP, safe_exit)
reader = Thread(target=read_distance, daemon=True)
reader.start()
pause()
except KeyboardInterrupt:
pass
finally:
reading = False
reader.join()
sensor.close()