import RPi.GPIO as GPIO
import threading

class Encoder(object):
    def __init__(self, r_en_a,r_en_b,l_en_a,l_en_b):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(r_en_a, GPIO.IN)
        GPIO.setup(r_en_b, GPIO.IN)
        GPIO.setup(l_en_a, GPIO.IN)
        GPIO.setup(l_en_b, GPIO.IN)
        self.l_en_a=l_en_a;self.l_en_b=l_en_b;
        self.r_en_a=r_en_a;self.r_en_b=r_en_b;

        GPIO.add_event_detect(r_en_a, GPIO.BOTH, callback=self.Update_encR)
        GPIO.add_event_detect(l_en_a, GPIO.BOTH, callback=self.Update_encL)
        self.count_R =0
        self.count_L=0

    def Update_encR(self,channel):
        if GPIO.input(self.r_en_a) == GPIO.input(self.r_en_b):
            self.count_R=self.count_R + 1
        else :
            self.count_R = self.count_R - 1  
        


    def Update_encL(self,channel):
        if GPIO.input(self.l_en_a) == GPIO.input(self.l_en_b):
            self.count_L=self.count_L + 1
        else :
            self.count_L = self.count_L - 1  
        return (self.count_L)
    
    def get_r_enc(self):
        return self.count_R
    def get_l_enc(self):
        return self.count_L
    def clear_encoders(self):
        self.count_R=0
        self.count_L=0

# r_en_a = 27 
# r_en_b = 10

# l_en_a = 5
# l_en_b = 6

# enc_obj = Encoder(27,10,5,6)

# def update_encoders():
#     threading.Timer(1,update_encoders).start()
#     print(" looping ")

# update_encoders()
[((6, 8, 6, 30), 'RPi.GPIO.setmode', 'GPIO.setmode', ({(6, 21, 6, 29): 'GPIO.BCM'}, {}), '(GPIO.BCM)', True, 'import RPi.GPIO as GPIO\n'), ((7, 8, 7, 35), 'RPi.GPIO.setup', 'GPIO.setup', ({(7, 19, 7, 25): 'r_en_a', (7, 27, 7, 34): 'GPIO.IN'}, {}), '(r_en_a, GPIO.IN)', True, 'import RPi.GPIO as GPIO\n'), ((8, 8, 8, 35), 'RPi.GPIO.setup', 'GPIO.setup', ({(8, 19, 8, 25): 'r_en_b', (8, 27, 8, 34): 'GPIO.IN'}, {}), '(r_en_b, GPIO.IN)', True, 'import RPi.GPIO as GPIO\n'), ((9, 8, 9, 35), 'RPi.GPIO.setup', 'GPIO.setup', ({(9, 19, 9, 25): 'l_en_a', (9, 27, 9, 34): 'GPIO.IN'}, {}), '(l_en_a, GPIO.IN)', True, 'import RPi.GPIO as GPIO\n'), ((10, 8, 10, 35), 'RPi.GPIO.setup', 'GPIO.setup', ({(10, 19, 10, 25): 'l_en_b', (10, 27, 10, 34): 'GPIO.IN'}, {}), '(l_en_b, GPIO.IN)', True, 'import RPi.GPIO as GPIO\n'), ((14, 8, 14, 75), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (), '', True, 'import RPi.GPIO as GPIO\n'), ((15, 8, 15, 75), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (), '', True, 'import RPi.GPIO as GPIO\n'), ((20, 11, 20, 34), 'RPi.GPIO.input', 'GPIO.input', ({(20, 22, 20, 33): 'self.r_en_a'}, {}), '(self.r_en_a)', True, 'import RPi.GPIO as GPIO\n'), ((20, 38, 20, 61), 'RPi.GPIO.input', 'GPIO.input', ({(20, 49, 20, 60): 'self.r_en_b'}, {}), '(self.r_en_b)', True, 'import RPi.GPIO as GPIO\n'), ((28, 11, 28, 34), 'RPi.GPIO.input', 'GPIO.input', ({(28, 22, 28, 33): 'self.l_en_a'}, {}), '(self.l_en_a)', True, 'import RPi.GPIO as GPIO\n'), ((28, 38, 28, 61), 'RPi.GPIO.input', 'GPIO.input', ({(28, 49, 28, 60): 'self.l_en_b'}, {}), '(self.l_en_b)', True, 'import RPi.GPIO as GPIO\n')]