To simplify functions of the same pattern defined by button_1_callback, ..., buttono_8_callback

Asked 2 years ago, Updated 2 years ago, 40 views

Is there any way to reduce the coding using the list function in the coding below? If it is not, or inefficient, could you tell me how to use another function?

import RPi.Bringing up GPIO as GPIO #GPIO library
import time # recall time library
import datetime

#Specify as an I/O list
IO_do = [22,6,13,19,26,11,27,4]
IO_di = [17,12,16,21,20,18,23,24]
Num = [1,2,3,4,5,6,7,8]
#Remove unnecessary WARNING
GPIO.setwarnings(False)

#GPIO pin number mode setting
GPIO.setmode(GPIO.BCM)

#Set button list variable INPUT, set PULLDOWN resistance
for i in IO_di:
    GPIO.setup([i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
#Set LED list variable to OUTPUT
for j in IO_do:
    GPIO.setup([j], GPIO.OUT)

#Boolean variable settings
light_1_on = False
light_2_on = False
light_3_on = False
light_4_on = False
light_5_on = False
light_6_on = False
light_7_on = False
light_8_on = False

#Notify you to start
print("start")

#Define button callback function Apply global function
def button_1_callback(ch):
    global light_1_on
    if light_1_on == False:
        GPIO.output(IO_do[0],1)
        print("LED1 ON")
    else:
        GPIO.output(IO_do[0],0)
        print("LED1 OFF")
    light_1_on = not light_1_on

def button_2_callback(ch):
    global light_2_on
    if light_2_on == False:
        GPIO.output(IO_do[1],1)
        print("LED2 ON")
    else:
        GPIO.output(IO_do[1],0)
        print("LED2 OFF")
    light_2_on = not light_2_on

def button_3_callback(ch):
    global light_3_on
    if light_3_on == False:
        GPIO.output(IO_do[2],1)
        print("LED3 ON")
    else:
        GPIO.output(IO_do[2],0)
        print("LED3 OFF")
    light_3_on = not light_3_on

def button_4_callback(ch):
    print(ch)
    global light_4_on
    if light_4_on == False:
        GPIO.output(IO_do[3],1)
        print("LED4 ON")
    else:
        GPIO.output(IO_do[3],0)
        print("LED4 OFF")
    light_4_on = not light_4_on

def button_5_callback(ch):
    print(ch)
    global light_5_on
    if light_5_on == False:
        GPIO.output(IO_do[4],1)
        print("LED5 ON")
    else:
        GPIO.output(IO_do[4],0)
        print("LED5 OFF")
    light_5_on = not light_5_on

def button_6_callback(ch):
    print(ch)
    global light_6_on
    if light_6_on == False:
        GPIO.output(IO_do[5],1)
        print("LED6 ON")
    else:
        GPIO.output(IO_do[5],0)
        print("LED6 OFF")
    light_6_on = not light_6_on

def button_7_callback(ch):
    print(ch)
    global light_7_on
    if light_7_on == False:
        GPIO.output(IO_do[6],1)
        print("LED7 ON")
    else:
        GPIO.output(IO_do[6],0)
        print("LED7 OFF")
    light_7_on = not light_7_on

def button_8_callback(ch):
    print(ch)
    global light_8_on
    if light_8_on == False:
        GPIO.output(IO_do[7],1)
        print("LED8 ON")
    else:
        GPIO.output(IO_do[7],0)
        print("LED8 OFF")
    light_8_on = not light_8_on    
# Run callback function when event notification method detects Rising signal on button pin
 # Set the bounce time to avoid incorrect signals.
GPIO.add_event_detect(IO_di[0],GPIO.RISING,callback=button_1_callback, bouncetime=300)
GPIO.add_event_detect(IO_di[1],GPIO.RISING,callback=button_2_callback, bouncetime=300)
GPIO.add_event_detect(IO_di[2],GPIO.RISING,callback=button_3_callback, bouncetime=300)
GPIO.add_event_detect(IO_di[3],GPIO.RISING,callback=button_4_callback, bouncetime=300)
GPIO.add_event_detect(IO_di[4],GPIO.RISING,callback=button_5_callback, bouncetime=300)
GPIO.add_event_detect(IO_di[5],GPIO.RISING,callback=button_6_callback, bouncetime=300)
GPIO.add_event_detect(IO_di[6],GPIO.RISING,callback=button_7_callback, bouncetime=300)
GPIO.add_event_detect(IO_di[7],GPIO.RISING,callback=button_8_callback, bouncetime=300)

while 1:
    time.sleep(0.1)

list python

2022-09-20 19:10

1 Answers

import RPi.Bringing up GPIO as GPIO #GPIO library
import time # recall time library
import datetime

#Specify as an I/O list
IO_do = [22,6,13,19,26,11,27,4]
IO_di = [17,12,16,21,20,18,23,24]
Num = [1,2,3,4,5,6,7,8]
#Remove unnecessary WARNING
GPIO.setwarnings(False)

#GPIO pin number mode setting
GPIO.setmode(GPIO.BCM)

#Set button list variable INPUT, set PULLDOWN resistance
for i in IO_di:
    GPIO.setup([i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
#Set LED list variable to OUTPUT
for j in IO_do:
    GPIO.setup([j], GPIO.OUT)

#Boolean variable settings
light_ons = [ False for _ in range(8)]

#Notify you to start
print("start")

#Define button callback function Apply global function
def button_callback(i):
    def button_callback_(ch):
        print(ch)
        global light_ons
        if light_ons[i] == False:
            GPIO.output(IO_do[i], 1)
            print("LED%d ON"%(i+1))
        else:
            GPIO.output(IO_do[i], 0)
            print("LED%d OFF"%(i+1))
        light_ons[i] = not light_ons[i]
    return button_callback_

# Run callback function when event notification method detects Rising signal on button pin
# Set the bounce time to avoid incorrect signals.
for i in range(8):
    GPIO.add_event_detect(IO_di[0],GPIO.RISING,callback=lambda i:button_callback(i), bouncetime=300)  

while 1:
    time.sleep(0.1)

I think you can do it like this, try it. I don't know if there's any error.

Oh, I don't think I need a lambda. Try it.

for i in range(8):
    GPIO.add_event_detect(IO_di[0],GPIO.RISING,callback=button_callback(i), bouncetime=300)  


2022-09-20 19:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.