-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdial.py
42 lines (35 loc) · 1.14 KB
/
dial.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
#! /usr/bin/python3
import time
import threading
import RPi.GPIO as GPIO
class Dial (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
# BCM pin numbering!
GPIO.setmode(GPIO.BCM)
GPIO.setup([17, 18], direction=GPIO.IN, pull_up_down=GPIO.PUD_UP)
self.direction = 0
def __del__(self):
GPIO.cleanup()
def get_direction(self):
# Pickup the direction and zero it, so we only get it once
return_val = self.direction
self.direction = 0
return return_val
def run(self):
while True:
# We only need to check for an interrupt on one pin
# because we can simply test the value of the other to
# work out which direction the dial was turned.
# We cannot modify the global direction variable because
# it is processed afterwards, so it would not be thread safe
GPIO.wait_for_edge(17, GPIO.FALLING)
new_direction = GPIO.input(18)
if not new_direction:
new_direction = -1
# Save the new_direction
self.direction = new_direction
# Debounce period
time.sleep(0.3)