From b75f12acedeb4aa0cf2720ce4ec4553a4b117501 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Wed, 28 Nov 2018 09:18:18 +0000 Subject: [PATCH] piSRF - better handle timeouts, closing --- hardware/PiSrf/nrsrf.py | 11 ++++++++--- hardware/PiSrf/package.json | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/hardware/PiSrf/nrsrf.py b/hardware/PiSrf/nrsrf.py index e17a5a71..fbe7138c 100755 --- a/hardware/PiSrf/nrsrf.py +++ b/hardware/PiSrf/nrsrf.py @@ -8,6 +8,11 @@ import RPi.GPIO as GPIO import time import sys import os, select +import signal + +def signal_handler(sig, frame): + sys.exit(0) +signal.signal(signal.SIGINT, signal_handler) # Turn off warnings if you run it a second time... GPIO.setwarnings(False) @@ -23,11 +28,13 @@ def Measure(): GPIO.output(TRIGGER, True) time.sleep(0.00001) GPIO.output(TRIGGER, False) + start = time.time() + stop = time.time() while GPIO.input(ECHO)==0: start = time.time() Dif = time.time() - realstart if Dif > 0.2: - print("Ultrasonic Sensor Timed out, Restart.") + # print("Ultrasonic Sensor Timed out, Restart.") time.sleep(0.4) return 400 while GPIO.input(ECHO)==1: @@ -74,8 +81,6 @@ if len(sys.argv) > 1: time.sleep(SLEEP) except: # try to clean up on exit print("0.0") - GPIO.cleanup() - sys.exit(0) else: print("Bad params") diff --git a/hardware/PiSrf/package.json b/hardware/PiSrf/package.json index 166283b8..5f8b96af 100644 --- a/hardware/PiSrf/package.json +++ b/hardware/PiSrf/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pisrf", - "version" : "0.1.3", + "version" : "0.1.4", "description" : "A Node-RED node for a Raspberry Pi to use a SRF04 or SRF05 range finder", "dependencies" : { },