7 """Class to decode mechanical rotary encoder pulses.""" 9 def __init__(self, pi, gpioA, gpioB, callback):
12 Instantiate the class with the pi and gpios connected to 13 rotary encoder contacts A and B. The common contact 14 should be connected to ground. The callback is 15 called when the rotary encoder is turned. It takes 16 one parameter which is +1 for clockwise and -1 for 34 print("pos={}".format(pos)) 38 decoder = rotary_encoder.decoder(pi, 7, 8, callback) 58 self.pi.set_mode(gpioA, pigpio.INPUT)
59 self.pi.set_mode(gpioB, pigpio.INPUT)
61 self.pi.set_pull_up_down(gpioA, pigpio.PUD_UP)
62 self.pi.set_pull_up_down(gpioB, pigpio.PUD_UP)
64 self.
cbA = self.pi.callback(gpioA, pigpio.EITHER_EDGE, self.
_pulse)
65 self.
cbB = self.pi.callback(gpioB, pigpio.EITHER_EDGE, self.
_pulse)
70 Decode the rotary encoder pulse. 72 +---------+ +---------+ 0 76 +---------+ +---------+ +----- 1 78 +---------+ +---------+ 0 82 ----+ +---------+ +---------+ 1 85 if gpio == self.
gpioA:
93 if gpio == self.
gpioA and level == 1:
96 elif gpio == self.
gpioB and level == 1:
103 Cancel the rotary encoder decoder. 109 if __name__ ==
"__main__":
114 import rotary_encoder
124 print(
"pos={}".format(pos))
def __init__(self, pi, gpioA, gpioB, callback)
def _pulse(self, gpio, level, tick)