0

so i have 3 GPIO.add_event_detect's each one comes with callback function when only one GPIO.add_event_detect is enabled, it shows proper output but if i add 3 of them it doesn't show anything while i turn encoders..

from Tkinter import *
from Tkinter import Listbox
import RPi.GPIO as GPIO
from time import sleep
import threading
import random
import sys
import os

# detecting DISPLAY, setting DISPLAY 
if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')

rEncoder1_counter = 0
rEncoder2_counter = 0
rEncoder3_counter = 0

# GPIO Pins #
Enc_A = 17  
Enc_B = 27
#-----------#
Enc_C = 22  
Enc_D = 23
#-----------#
Enc_E = 24  
Enc_F = 25
#-----------#

# GPIO settings
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)
#
GPIO.setup(Enc_A, GPIO.IN)
GPIO.setup(Enc_B, GPIO.IN)
#
GPIO.setup(Enc_C, GPIO.IN)
GPIO.setup(Enc_D, GPIO.IN)
#
GPIO.setup(Enc_E, GPIO.IN)
GPIO.setup(Enc_F, GPIO.IN)

# reading encoder
def rEncoder1(Enc_A):
    global rEncoder1_counter
    sleep(0.002)
    Switch_A = GPIO.input(Enc_A)
    Switch_B = GPIO.input(Enc_B)
 
    if (Switch_A == 1) and (Switch_B == 0):
        rEncoder1_counter += 1
        print  (rEncoder1_counter)
        listbox.insert(END,rEncoder1_counter)
        while Switch_B == 0:
            Switch_B = GPIO.input(Enc_B)
        while Switch_B == 1:
            Switch_B = GPIO.input(Enc_B)
        return
 
    elif (Switch_A == 1) and (Switch_B == 1):
        rEncoder1_counter -= 1
        print  (rEncoder1_counter)
        listbox.insert(END,rEncoder1_counter) 
        while Switch_A == 1:
            Switch_A = GPIO.input(Enc_A)
        return
    else:
        return

def rEncoder2(Enc_C):
    global rEncoder2_counter
    sleep(0.002)
    Switch_C = GPIO.input(Enc_C)
    Switch_D = GPIO.input(Enc_D)
 
    if (Switch_C == 1) and (Switch_D == 0):
        rEncoder2_counter += 1
        print  (rEncoder2_counter)
        listbox.insert(END,rEncoder2_counter)
        while Switch_D == 0:
            Switch_D = GPIO.input(Enc_D)
        while Switch_D == 1:
            Switch_D = GPIO.input(Enc_D)
        return
 
    elif (Switch_C == 1) and (Switch_D == 1):
        rEncoder2_counter -= 1
        print  (rEncoder2_counter)
        listbox.insert(END,rEncoder2_counter) 
        while Switch_C == 1:
            Switch_C = GPIO.input(Enc_C)
        return
    else:
        return

def rEncoder3(Enc_E):
    global rEncoder3_counter
    sleep(0.002)
    Switch_E = GPIO.input(Enc_E)
    Switch_F = GPIO.input(Enc_F)
 
    if (Switch_E == 1) and (Switch_F == 0):
        rEncoder3_counter += 1
        print  (rEncoder3_counter)
        listbox.insert(END,rEncoder3_counter)
        while Switch_F == 0:
            Switch_F = GPIO.input(Enc_F)
        while Switch_F == 1:
            Switch_F = GPIO.input(Enc_F)
        return
 
    elif (Switch_E == 1) and (Switch_F == 1):
        rEncoder3_counter -= 1
        print  (rEncoder3_counter)
        listbox.insert(END,rEncoder3_counter) 
        while Switch_E == 1:
            Switch_E = GPIO.input(Enc_E)
        return
    else:
        return

# GPIO output detection
GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rEncoder1, bouncetime=10)
GPIO.add_event_detect(Enc_C, GPIO.RISING, callback=rEncoder2, bouncetime=10)
GPIO.add_event_detect(Enc_E, GPIO.RISING, callback=rEncoder3, bouncetime=10)

# window settings
window = Tk()
window.title("gui_test")

# window.geometry('800x600')
window.configure(bg='black')
window.attributes('-fullscreen', True)

# font
#myFont = font.Font(family='Helvetica', size=12, weight='bold')

# scrolbar
scrollbar = Scrollbar(window)

# listbox , font=myFont
listbox = Listbox(window, yscrollcommand=scrollbar.set)
listbox.config(yscrollcommand=scrollbar.set)
listbox.pack()
listbox.see("end")
#
scrollbar.config(command=listbox.yview)
scrollbar.pack(side=RIGHT, fill=Y)

# commanding mainloop for starting main loop
window.mainloop()

1 Answer 1

2

When a callback is running all other callbacks and the main program are blocked.

You have while loops in the callbacks which means they are blocked for periods of times.

I suggest you redesign and do the minimum amount of processing in each callback. I suggest setting flags only, no while loops.

2
  • yeah i re done it, so now we are cool, all works without blocking. thanks. Sep 19, 2021 at 12:16
  • If you are happy with your new code it would be useful to add it to your question as a sort of before and after (to give others an idea of what to change).
    – joan
    Sep 19, 2021 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.