rotary_encoder.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import pigpio
00004 
00005 class decoder:
00006 
00007    """Class to decode mechanical rotary encoder pulses."""
00008 
00009    def __init__(self, pi, gpioA, gpioB, callback):
00010 
00011       """
00012       Instantiate the class with the pi and gpios connected to
00013       rotary encoder contacts A and B.  The common contact
00014       should be connected to ground.  The callback is
00015       called when the rotary encoder is turned.  It takes
00016       one parameter which is +1 for clockwise and -1 for
00017       counterclockwise.
00018 
00019       EXAMPLE
00020 
00021       import time
00022       import pigpio
00023 
00024       import rotary_encoder
00025 
00026       pos = 0
00027 
00028       def callback(way):
00029 
00030          global pos
00031 
00032          pos += way
00033 
00034          print("pos={}".format(pos))
00035 
00036       pi = pigpio.pi()
00037 
00038       decoder = rotary_encoder.decoder(pi, 7, 8, callback)
00039 
00040       time.sleep(300)
00041 
00042       decoder.cancel()
00043 
00044       pi.stop()
00045 
00046       """
00047 
00048       self.pi = pi
00049       self.gpioA = gpioA
00050       self.gpioB = gpioB
00051       self.callback = callback
00052 
00053       self.levA = 0
00054       self.levB = 0
00055 
00056       self.lastGpio = None
00057 
00058       self.pi.set_mode(gpioA, pigpio.INPUT)
00059       self.pi.set_mode(gpioB, pigpio.INPUT)
00060 
00061       self.pi.set_pull_up_down(gpioA, pigpio.PUD_UP)
00062       self.pi.set_pull_up_down(gpioB, pigpio.PUD_UP)
00063 
00064       self.cbA = self.pi.callback(gpioA, pigpio.EITHER_EDGE, self._pulse)
00065       self.cbB = self.pi.callback(gpioB, pigpio.EITHER_EDGE, self._pulse)
00066 
00067    def _pulse(self, gpio, level, tick):
00068 
00069       """
00070       Decode the rotary encoder pulse.
00071 
00072                    +---------+         +---------+      0
00073                    |         |         |         |
00074          A         |         |         |         |
00075                    |         |         |         |
00076          +---------+         +---------+         +----- 1
00077 
00078              +---------+         +---------+            0
00079              |         |         |         |
00080          B   |         |         |         |
00081              |         |         |         |
00082          ----+         +---------+         +---------+  1
00083       """
00084 
00085       if gpio == self.gpioA:
00086          self.levA = level
00087       else:
00088          self.levB = level;
00089 
00090       if gpio != self.lastGpio: # debounce
00091          self.lastGpio = gpio
00092 
00093          if   gpio == self.gpioA and level == 1:
00094             if self.levB == 1:
00095                self.callback(1)
00096          elif gpio == self.gpioB and level == 1:
00097             if self.levA == 1:
00098                self.callback(-1)
00099 
00100    def cancel(self):
00101 
00102       """
00103       Cancel the rotary encoder decoder.
00104       """
00105 
00106       self.cbA.cancel()
00107       self.cbB.cancel()
00108 
00109 if __name__ == "__main__":
00110 
00111    import time
00112    import pigpio
00113 
00114    import rotary_encoder
00115 
00116    pos = 0
00117 
00118    def callback(way):
00119 
00120       global pos
00121 
00122       pos += way
00123 
00124       print("pos={}".format(pos))
00125 
00126    pi = pigpio.pi()
00127 
00128    decoder = rotary_encoder.decoder(pi, 7, 8, callback)
00129 
00130    time.sleep(300)
00131 
00132    decoder.cancel()
00133 
00134    pi.stop()
00135 


cob_hand_bridge
Author(s): Mathias L├╝dtke
autogenerated on Thu Jun 6 2019 20:43:57