Working on a Raspberry Python app that has a interrupt that not working as expected.
When I start the app (before calling the main function) the push button interrupt works great and as expected. When the main function is called and returns the push button interrupt stops working. In the main function, I change the interrupt callback from the push button function to the smack abort function (not working). Then, just before the main function returns, the callback changed back to the push button function
The main function works as expected.
I think I am not handling the callback function change correctly.
Callback change code
GPIO.remove_event_detect(pushButton_Pin)
GPIO.add_event_detect(pushButton_Pin , GPIO.RISING,
callback = SmackAbort_Function,
bouncetime = 100)
.
The following code is the main function of the app.
def SmackBottomSelected():
global hitNum
global Abort_Flag
# global IntensitySelect_Pins
if SmackBottom_Button.cget("text") == "Stop":
print("Smack Bottom Stop Selected")
Abort_Flag = True;
GPIO.output( Abort_Out_Pin, True )
return;
else:
print("Smack Bottom Start Selected")
# if Abort_Flag == True;
# GPIO.output( Abort_Out_Pin, True )
# return
# Update GUI Elements
SmackBottom_Button.configure(text = "Stop")
SmackBottom_Button.configure(bg = "red")
# Set LED off
Set_Ready_LED_Off()
Disable_Buttons()
#Debug point
print("Disabling the push button interupt")
#Next disable the button interrup so we do not have a interrupt while runing the function code
# GPIO.remove_event_detect(pushButton_Pin)
ClickBuzzer()
# Update Spank Delay
# SmackDelay_EntryBox.get is in Seconds floating number so we need to convert the floating number to
# INT becuase the Sleep function paramter is an int number of milimSeconds (1000mS = 1 Second)
if SmackDelay_EntryBox.get() == "":
SmackDelay_Val = 0.5 #Delay half a second
else:
SmackDelay_Val = float(SmackDelay_EntryBox.get()) # Change from seconds to mil-seconds
if SmackDelay_Val == 0:
SmackDelay = 1
#Debug Point
print(f" Smack Delay Val = {SmackDelay_Val}mS")
# Update Number of impacts
if NumberOfSmack_EntryBox.get() == "":
NumberOfSmack_Val = 1
else:
NumberOfSmack_Val = int(NumberOfSmack_EntryBox.get())
#Debug Point
print(f"NumberOfSmack_Val = {NumberOfSmack_Val}")
# if button_override != None:
# NumberOfSmack_Val = 1
# SmackDelay_Val = 1
intensity = intensity_options.get() # Get Currently Selected Intensity string
#Debug Point
print('Intensity >> ' , intensity)
#OK now set up the stepper motor driver/controler with proper swig intensity
GPIO.output(IntensitySelect_Pins,Task_Num[intensity_options.get()])
#Debug Point
# print(' Smack Bottom Selected -->> IntensitySelect_Pins >> ' , (GPIO.input(IntensitySelect_Pins)))
Tk.update(root)
#Debug Point
print("Enableing the abort interupt")
#Next disable the all interrupt so we do not have a interrupt while runing the function code
GPIO.remove_event_detect(pushButton_Pin)
GPIO.add_event_detect(pushButton_Pin , GPIO.RISING,
callback = SmackAbort_Function,
bouncetime = 100)
for hitnum in range(1,NumberOfSmack_Val+1):
if Abort_Flag:
break
# Debug Point
print(f'Smack Bottom Selected ==>> Hit Number= {hitnum}')
Run_Task()
statusbar.config(text=f"Status: Smack Number > {hitnum}")
root.update()
sleep(SmackDelay_Val)
if Abort_Flag:
statusbar.config(text="Spanking Aborted after %s! >> Ready..." %(hitNum - 1))
while GPIO.input(pushButton_Pin): # Wait for user to release the Abort button
pass
else:
statusbar.config(text="Spanking Concludedd || Smacks %d" % (hitNum - 1))
Set_Ready_LED_On()
SmackBottom_Button.configure(text = "Smack Bottom")
hitNum = 1
GPIO.output(Run_Pin, False)
GPIO.output( Abort_Out_Pin, False)
Abort_Flag = False
#Debug Point
print('')
print(' Spanking Completed')
GPIO.output( Abort_Out_Pin, False)
Restore_Buttons()
# Spanking Complete
Set_Ready_LED_On()
hitNum = 1
Restore_Buttons()
statusbar.config(text="Status: Spanking Complete >> Ready...")
SmackBottom_Button.configure(text = "Smack Bottom")
#Next disable the all interrupt so we do not have a interrupt while runing the function code
GPIO.remove_event_detect(pushButton_Pin)
#Debuug Point
print(' Renabling the push button interrupt')
GPIO.add_event_detect(pushButton_Pin ,
GPIO.RISING ,
callback = Push_Button_Function,
bouncetime = 100)
#Debug Point
print('Exiting Smack Bottom Selected')
print('==============================')
print('')
#********************************************
This is the push button interrupt function
def Push_Button_Function(channel):
# global override
# override = 1
x=1
#Debug Point
print('>>>PUSH BOTTON DETECTED <<<')
#Next disable the button interrup so we do not have a interrupt while runing the function code
GPIO.remove_event_detect(pushButton_Pin)
statusbar.config(text="Push Button Detected")
Disable_Buttons()
#First turn off the Ready LED
Set_Ready_LED_Off()
print("Telling controler the intensity value")
GPIO.output(IntensitySelect_Pins,Task_Num[intensity_options.get()])
print("Calling Run Task")
Run_Task()
print(" Back in Push Button Function")
#Wait for push button to be realsed
while GPIO.input(pushButton_Pin):
pass
# while x <= 10:
# if not (GPIO.input(pushButton_Pin)):
# x = +1
# else:
# x = 0
# sleep(0.01)
Set_Ready_LED_On()
# Renabling the push botton interuot
print(" Reinabling Push Buttom interupt")
# Renable push botton interrup so we do not have a interrupt while runing the function code
GPIO.add_event_detect(pushButton_Pin,GPIO.RISING ,callback = Push_Button_Function,bouncetime = 100)
statusbar.config(text="Ready")
Restore_Buttons()
print("Push Botton function exiting")
print("^^^^^^^^^^^^^^^^^^^^")
This is the abort interrupt function
def SmackAbort_Function(channel):
global Abort_Flag;
x = 1
#Debut Point
print("In Smack Abort Function");
if Abort_Flag:
#Debug Point
print("Abort Flag already set to true returning")
return
else:
#Debug Point
#Debug Point
print("Seting Abort Flag to true");
Abort_Flag = True;
ClickBuzzer()
GPIO.remove_event_detect(pushButton_Pin)
GPIO.output( Abort_Out_Pin, True) # Tell logic controler to abort current task
# Wait for push botton to be realsed
# while GPIO.input(pushButton_Pin):
# pass;
while GPIO.input(pushButton_Pin):
pass
#Wait to make sure the button is release
# while x <= 20:
# if not (GPIO.input(pushButton_Pin)):
# x = +1;
# else:
# x = 0;
# sleep(0.050);
# Smack
# Renable push botton interrup so we do not have a interrupt while runing the function code
# GPIO.add_event_detect(pushButton_Pin ,
# GPIO.RISING ,
# callback = Push_Button_Function,
# bouncetime = 100)
#Debut Point
print("Existimg Smack ABORT Function");
Thank you for sharing your wisdom