5

I am developing a small application on a Raspberry Pi. For this question I can boil it down to the usage of a rotary encoder. I found the usage of the dtoverlay the most convinient way for me. Hence, I use the evdev module in python3. My current code looks like this:

import time
from threading import Thread
from evdev import InputDevice, events
import evdev


rotary_dev = InputDevice('/dev/input/event1')

def rotaryAction(dev):
   for ev in dev.read_loop():
       ev = evdev.util.categorize(ev)
       if isinstance(ev, events.RelEvent):
           if ev.event.value == 1:
               print("Value: ",ev.event.value)
           elif ev.event.value == -1:
               print("Value: ",ev.event.value)

t = Thread(target=rotaryAction, args=(rotary_dev,))
t.start()

while True:
   try:
       time.sleep(5)
   except KeyboardInterrupt:
       print("Interrupt")
       #Do something to stop the wait for device events
       break

Now my question is, how can I terminate this program in the best way. I cannot stop the thread because the for loop waits for an event on the device. I was thinking to somehow emulate an event, but I am not sure (= I have no clue) how to do that. I didn't find any references on the web, I wonder why..

I appreciate your help!

0 Answers0