-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotary_class.py
executable file
·105 lines (87 loc) · 2.49 KB
/
rotary_class.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
#
# Raspberry Pi Rotary Encoder Class
# $Id: rotary_class.py,v 1.2 2014/01/31 13:34:48 bob Exp $
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# This class uses standard rotary encoder with push switch
#
#
import RPi.GPIO as GPIO
class RotaryEncoder:
CLOCKWISE=1
ANTICLOCKWISE=2
BUTTONDOWN=3
BUTTONUP=4
rotary_a = 0
rotary_b = 0
rotary_c = 0
last_state = 0
direction = 0
# Initialise rotary encoder object
def __init__(self,pinA,pinB,button,callback):
self.pinA = pinA
self.pinB = pinB
self.button = button
self.callback = callback
GPIO.setmode(GPIO.BCM)
# The following lines enable the internal pull-up resistors
# on version 2 (latest) boards
GPIO.setwarnings(False)
GPIO.setup(self.pinA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.pinB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# For version 1 (old) boards comment out the above four lines
# and un-comment the following 3 lines
#GPIO.setup(self.pinA, GPIO.IN)
#GPIO.setup(self.pinB, GPIO.IN)
#GPIO.setup(self.button, GPIO.IN)
# Add event detection to the GPIO inputs
GPIO.add_event_detect(self.pinA, GPIO.FALLING, callback=self.switch_event)
GPIO.add_event_detect(self.pinB, GPIO.FALLING, callback=self.switch_event)
GPIO.add_event_detect(self.button, GPIO.BOTH, callback=self.button_event, bouncetime=200)
return
# Call back routine called by switch events
def switch_event(self,switch):
if GPIO.input(self.pinA):
self.rotary_a = 1
else:
self.rotary_a = 0
if GPIO.input(self.pinB):
self.rotary_b = 1
else:
self.rotary_b = 0
self.rotary_c = self.rotary_a ^ self.rotary_b
new_state = self.rotary_a * 4 + self.rotary_b * 2 + self.rotary_c * 1
delta = (new_state - self.last_state) % 4
self.last_state = new_state
event = 0
if delta == 1:
if self.direction == self.CLOCKWISE:
# print "Clockwise"
event = self.direction
else:
self.direction = self.CLOCKWISE
elif delta == 3:
if self.direction == self.ANTICLOCKWISE:
# print "Anticlockwise"
event = self.direction
else:
self.direction = self.ANTICLOCKWISE
if event > 0:
self.callback(event)
return
# Push button up event
def button_event(self,button):
if GPIO.input(button):
event = self.BUTTONUP
else:
event = self.BUTTONDOWN
self.callback(event)
return
# Get a switch state
def getSwitchState(self, switch):
return GPIO.input(switch)
# End of RotaryEncoder class