#!/usr/bin/python # sudo apt-get install python3-rpi.gpio from RPi import GPIO from datetime import datetime import time ss = 0 # start time stamp ls = 0 # last time stamp # returns the elapsed milliseconds since the start of the program def millis(): dt = datetime.now() ms = dt.minute * 60 *1000 + dt.second * 1000 + dt.microsecond / 1000 return int(ms) def mark(tick): print("\t", end="") for i in range(tick): if i > 100: break if i == 34: print (">", end="") elif i== 42: print ("<", end="") elif i== 56: print (">", end="") elif i== 68: print ("<", end="") elif i== 100: print ("%", end="") else: print (".", end="") print() channel = 24 GPIO.setmode(GPIO.BCM) GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP) def main(): global ss,ls,channel try: while True: success = GPIO.wait_for_edge(channel, GPIO.FALLING, timeout = 50) if success is None: continue print("start") ss=0 while not success is None: ls = millis() if ss == 0: ss= ls # wait for up to 5 seconds for a rising edge (timeout is in milliseconds) success = GPIO.wait_for_edge(channel, GPIO.RISING, timeout=1000) ts1 = millis() print ('H {} {} ' . format(ts1-ss, ts1-ls), end="") mark(ts1-ls) ls = ts1 if not success is None: success = GPIO.wait_for_edge(channel, GPIO.FALLING, timeout=1000) if not success is None: ts2 = millis() print ('L {} {} ' . format(ts2-ss, ts2-ls), end="" ) mark(ts2-ls) except KeyboardInterrupt: # does not work if it runs in background. print ("\nQuit") if __name__ == '__main__': main() GPIO.cleanup()