Python Forum
Setting GPIO on Raspberry PI using a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting GPIO on Raspberry PI using a function
#1
Hello all,

I am new to Python, hardware guy trying to learn something new and help cross the gap. I decided to make a cable checker that tests 156 pins from one connector to another and off of the user input see if the pins are connected as the user intends (i.e Pin 1 on left connector goes to Pin 30 on right connector, test and verify)

I am using MUX's to switch between SIPO shift registers and three GPIOs have to be set in order to choose each shift register (well right now the first 8 shift registers as I only have 1 MUX connected) and the same on the output.

So here is my question, I have this code:

def MUX1shift1_Y0():
    GPIO.output(17,GPIO.LOW)
    GPIO.output(27,GPIO.LOW)
    GPIO.output(22,GPIO.LOW)

def MUX1shift2_Y1():
    GPIO.output(17,GPIO.HIGH)
    GPIO.output(27,GPIO.LOW)
    GPIO.output(22,GPIO.LOW)

def MUX1shift3_Y2():
    GPIO.output(17,GPIO.LOW)
    GPIO.output(27,GPIO.HIGH)
    GPIO.output(22,GPIO.LOW)

def MUX1shift4_Y3():
    GPIO.output(17,GPIO.HIGH)
    GPIO.output(27,GPIO.HIGH)
    GPIO.output(22,GPIO.LOW)

def MUX1shift5_Y4():
    GPIO.output(17,GPIO.LOW)
    GPIO.output(27,GPIO.LOW)
    GPIO.output(22,GPIO.HIGH)

def MUX1shift6_Y5():
    GPIO.output(17,GPIO.HIGH)
    GPIO.output(27,GPIO.LOW)
    GPIO.output(22,GPIO.HIGH)

def MUX1shift7_Y6():
    GPIO.output(17,GPIO.LOW)
    GPIO.output(27,GPIO.HIGH)
    GPIO.output(22,GPIO.HIGH)

def MUX1shift8_Y7():
    GPIO.output(17,GPIO.HIGH)
    GPIO.output(27,GPIO.HIGH)
    GPIO.output(22,GPIO.HIGH)
Now as per each pin input by the user I have to set each of these by the range of numbers (i.e. pins  1-8 need to be set to MUX1shift1_Y0)

So to follow DRY (Don't Repeat Yourself) I decided to write a function that set them accordingly, but my lack of knowledge caught up with me. I need the GPIO's to be returned from the function and set when I hit the start button in my GUI, right now I get a return of NONE or if I place this in a class I get the placeholder returned:

(pinIn is a list of values 1 - 156)

def setMUXvalue():
    for i in range (0,156):
        if pinIn[i] in range (1,8):
            return MUX1shift1_Y0()
        elif pinIn[i] in range (9,16):
            return MUX1shift2_Y1()
        elif pinIn[i] in range (17,24):
            return MUX1shift3_Y2()
        elif pinIn[i] in range (25,32):
            return MUX1shift4_Y3()
        elif pinIn[i] in range (33,40):
            return MUX1shift5_Y4()
        elif pinIn[i] in range (41,48):
            return MUX1shift6_Y5()
        elif pinIn[i] in range (49,56):
            return MUX1shift7_Y6()
        elif pinIn[i] in range (57,64):
            return MUX1shift8_Y7()
        elif pinIn[i] in range (65,72):
            return MUX2shift9_Y0()
        elif pinIn[i] in range (73,80):
            return MUX2shift10_Y1()
        elif pinIn[i] in range (81,88):
            return MUX2shift11_Y2()
        elif pinIn[i] in range (89,96):
            return MUX2shift12_Y3()
        elif pinIn[i] in range (97,104):
            return MUX2shift13_Y4()
        elif pinIn[i] in range (105,112):
            return MUX2shift14_Y5()
        elif pinIn[i] in range (113,120):
            return MUX2shift15_Y6()
        elif pinIn[i] in range (121,128):
            return MUX2shift16_Y7()
        elif pinIn[i] in range (129,136):
            return MUX3shift17_Y0()
        elif pinIn[i] in range (137,144):
            return MUX3shift18_Y1()
        elif pinIn[i] in range (145,152):
            return MUX3shift19_Y2()
        elif pinIn[i] in range (153,156):
            return MUX3shift20_Y3()
Any help and explanation would be great and I am extremely grateful for any helpful pointers.

Thank you
[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
Reply
#2
This is unrelated to your code, but all those functions look veeeeery similar to me, so I'd probably just turn them all into a single function.  Maybe something like:
def set_pins(first=True, second=True, third=True):
  GPIO.output(17, GPIO.HIGH if first else GPIO.LOW)
  GPIO.output(27, GPIO.HIGH if second else GPIO.LOW)
  GPIO.output(22, GPIO.HIGH if third else GPIO.LOW)
Then, maybe I'd have some helper functions for each combination:
MUX1shift1_Y0 = lambda: set_pins(False, False, False)
# etc
The helpers could probably all be generated in a loop, instead of copy-paste (but I'm a little too busy to think that hard right now, lol).

As to your actual question, what does this mean?
Quote: I need the GPIO's to be returned from the function
Given one of the combinations, what, exactly, should be the return value?
Reply
#3
nilamo,

Thank you for your reply, I think you inadvertently answered my question by helping me minimize my code in the example you provided

def set_pins(first=True, second=True, third=True):
  GPIO.output(17, GPIO.HIGH if first else GPIO.LOW)
  GPIO.output(27, GPIO.HIGH if second else GPIO.LOW)
  GPIO.output(22, GPIO.HIGH if third else GPIO.LOW)
 

I am going to see if I can get this to work and then I don't need the function anymore that looks at pinIn and then sets the function to be called.

So what do I mean by:
I need the GPIO's to be returned from the function.

I was trying to look at the values in the pinIn list at each location pinIn[0] thru pinIn[155] and then when a match occurs it sends out the function to set the GPIOs.
So when the setMUXValue() function was called it would cycle through a for loop checking each pinIn location ---- for instance when I call this in my "def start" it would look at pinIn[0] (which equals 1) then go to the function run down the if statements see the "in range (1,8):" and send back to the "def start" the function "to call" to set the GPIO, in this instance it would send MUX1shift1_Y0() and that would setGPIO.output(17,GPIO.LOW)   GPIO.output(27,GPIO.LOW)   GPIO.output(22,GPIO.LOW).

My hope was this would call the MUX1shift1_Y0() and the GPIO's would be set without me having to "if/elif" each situation.

i.e.

for i in range (0,156):
    if pinIn[i] == 1:
        return MUX1shift1_Y0
    elif pinIn[i] == 2:
        return MUX1shift1_Y0
    elif pinIn[i] == 3:
        return MUX1shift1_Y0
    elif pinIn[i] == 4:
        return MUX1shift1_Y0
    elif pinIn[i] == 5:
        return MUX1shift1_Y0
    elif pinIn[i] == 6:
        return MUX1shift1_Y0
    elif pinIn[i] == 7:
        return MUX1shift1_Y0
    elif pinIn[i] == 8:
        return MUX1shift1_Y0
    elif pinIn[i] == 9:
        return MUX1shift2_Y1
    elif pinIn[i] == 10:
        return MUX1shift2_Y1
    elif pinIn[i] == 11:
        return MUX1shift2_Y1
    elif pinIn[i] == 12:
        return MUX1shift2_Y1
    elif pinIn[i] == 13:
        return MUX1shift2_Y1
    elif pinIn[i] == 14:
        return MUX1shift2_Y1
    elif pinIn[i] == 15:
        return MUX1shift2_Y1
    elif pinIn[i] == 16:
        return MUX1shift2_Y1
    elif pinIn[i] == 17:
        return MUX1shift3_Y2
    elif pinIn[i] == 18:
        return MUX1shift3_Y2
    elif pinIn[i] == 19:
        return MUX1shift3_Y2
    elif pinIn[i] == 20:
        return MUX1shift3_Y2
    elif pinIn[i] == 21:
        return MUX1shift3_Y2
    elif pinIn[i] == 22:
        return MUX1shift3_Y2
    elif pinIn[i] == 23:
        return MUX1shift3_Y2
    elif pinIn[i] == 24:
        return MUX1shift3_Y2
    elif pinIn[i] == 25:
        return MUX1shift4_Y3
    elif pinIn[i] == 26:
        return MUX1shift4_Y3
    elif pinIn[i] == 27:
        return MUX1shift4_Y3
    elif pinIn[i] == 28:
        return MUX1shift4_Y3
    elif pinIn[i] == 29:
        return MUX1shift4_Y3
    elif pinIn[i] == 30:
        return MUX1shift4_Y3
    elif pinIn[i] == 31:
        return MUX1shift4_Y3
    elif pinIn[i] == 32:
        return MUX1shift4_Y3
    elif pinIn[i] == 33:
        return MUX1shift5_Y4
    elif pinIn[i] == 34:
        return MUX1shift5_Y4
    elif pinIn[i] == 35:
        return MUX1shift5_Y4
    elif pinIn[i] == 36:
        return MUX1shift5_Y4
    elif pinIn[i] == 37:
        return MUX1shift5_Y4
    elif pinIn[i] == 38:
        return MUX1shift5_Y4
    elif pinIn[i] == 39:
        return MUX1shift5_Y4
    elif pinIn[i] == 40:
        return MUX1shift5_Y4
    elif pinIn[i] == 41:
        return MUX1shift6_Y5        
    elif pinIn[i] == 42:
        return MUX1shift6_Y5
    elif pinIn[i] == 43:
        return MUX1shift6_Y5
    elif pinIn[i] == 44:
        return MUX1shift6_Y5
    elif pinIn[i] == 45:
        return MUX1shift6_Y5
    elif pinIn[i] == 46:
        return MUX1shift6_Y5
    elif pinIn[i] == 47:
        return MUX1shift6_Y5
    elif pinIn[i] == 48:
        return MUX1shift6_Y5
    elif pinIn[i] == 49:
        return MUX1shift7_Y6
    elif pinIn[i] == 50:
        return MUX1shift7_Y6
    elif pinIn[i] == 51:
        return MUX1shift7_Y6
    elif pinIn[i] == 52:
        return MUX1shift7_Y6
    elif pinIn[i] == 53:
        return MUX1shift7_Y6
    elif pinIn[i] == 54:
        return MUX1shift7_Y6
    elif pinIn[i] == 55:
        return MUX1shift7_Y6
    elif pinIn[i] == 56:
        return MUX1shift7_Y6
    elif pinIn[i] == 57:
        return MUX1shift8_Y7
    elif pinIn[i] == 58:
        return MUX1shift8_Y7
    elif pinIn[i] == 59:
        return MUX1shift8_Y7
    elif pinIn[i] == 60:
        return MUX1shift8_Y7
    elif pinIn[i] == 61:
        return MUX1shift8_Y7
    elif pinIn[i] == 62:
        return MUX1shift8_Y7
    elif pinIn[i] == 63:
        return MUX1shift8_Y7
    elif pinIn[i] == 64:
        return MUX1shift8_Y7
    elif pinIn[i] == 65:
        return MUX2shift9_Y0
    elif pinIn[i] == 66:
        return MUX2shift9_Y0
    elif pinIn[i] == 67:
        return MUX2shift9_Y0
    elif pinIn[i] == 68:
        return MUX2shift9_Y0
    elif pinIn[i] == 69:
        return MUX2shift9_Y0
    elif pinIn[i] == 70:
        return MUX2shift9_Y0
    elif pinIn[i] == 71:
        return MUX2shift9_Y0
    elif pinIn[i] == 72:
        return MUX2shift9_Y0
    elif pinIn[i] == 73:
        return MUX2shift10_Y1
    elif pinIn[i] == 74:
        return MUX2shift10_Y1
    elif pinIn[i] == 75:
        return MUX2shift10_Y1
    elif pinIn[i] == 76:
        return MUX2shift10_Y1
    elif pinIn[i] == 77:
        return MUX2shift10_Y1
    elif pinIn[i] == 78:
        return MUX2shift10_Y1
    elif pinIn[i] == 79:
        return MUX2shift10_Y1
    elif pinIn[i] == 80:
        return MUX2shift10_Y1
    elif pinIn[i] == 81:
        return MUX2shift11_Y2
    elif pinIn[i] == 82:
        return MUX2shift11_Y2
    elif pinIn[i] == 83:
        return MUX2shift11_Y2
    elif pinIn[i] == 84:
        return MUX2shift11_Y2
    elif pinIn[i] == 85:
        return MUX2shift11_Y2
    elif pinIn[i] == 86:
        return MUX2shift11_Y2
    elif pinIn[i] == 87:
        return MUX2shift11_Y2
    elif pinIn[i] == 88:
        return MUX2shift11_Y2
    elif pinIn[i] == 89:
        return MUX2shift12_Y3
    elif pinIn[i] == 90:
        return MUX2shift12_Y3
    elif pinIn[i] == 91:
        return MUX2shift12_Y3
    elif pinIn[i] == 92:
        return MUX2shift12_Y3
    elif pinIn[i] == 93:
        return MUX2shift12_Y3
    elif pinIn[i] == 94:
        return MUX2shift12_Y3
    elif pinIn[i] == 95:
        return MUX2shift12_Y3
    elif pinIn[i] == 96:
        return MUX2shift12_Y3
    elif pinIn[i] == 97:
        return MUX2shift13_Y4
    elif pinIn[i] == 98:
        return MUX2shift13_Y4
    elif pinIn[i] == 99:
        return MUX2shift13_Y4
    elif pinIn[i] == 100:
        return MUX2shift13_Y4
    elif pinIn[i] == 101:
        return MUX2shift13_Y4    
    elif pinIn[i] == 102:
        return MUX2shift13_Y4
    elif pinIn[i] == 103:
        return MUX2shift13_Y4
    elif pinIn[i] == 104:
        return MUX2shift13_Y4
    elif pinIn[i] == 105:
        return MUX2shift14_Y5
    elif pinIn[i] == 106:
        return MUX2shift14_Y5
    elif pinIn[i] == 107:
        return MUX2shift14_Y5
    elif pinIn[i] == 108:
        return MUX2shift14_Y5
    elif pinIn[i] == 109:
        return MUX2shift14_Y5
    elif pinIn[i] == 110:
        return MUX2shift14_Y5
    elif pinIn[i] == 111:
        return MUX2shift14_Y5
    elif pinIn[i] == 112:
        return MUX2shift14_Y5
    elif pinIn[i] == 113:
        return MUX2shift15_Y6
    elif pinIn[i] == 114:
        return MUX2shift15_Y6
    elif pinIn[i] == 115:
        return MUX2shift15_Y6
    elif pinIn[i] == 116:
        return MUX2shift15_Y6
    elif pinIn[i] == 117:
        return MUX2shift15_Y6
    elif pinIn[i] == 118:
        return MUX2shift15_Y6
    elif pinIn[i] == 119:
        return MUX2shift15_Y6
    elif pinIn[i] == 120:
        return MUX2shift15_Y6
    elif pinIn[i] == 121:
        return MUX2shift16_Y7
    elif pinIn[i] == 122:
        return MUX2shift16_Y7
    elif pinIn[i] == 123:
        return MUX2shift16_Y7
    elif pinIn[i] == 124:
        return MUX2shift16_Y7
    elif pinIn[i] == 125:
        return MUX2shift16_Y7
    elif pinIn[i] == 126:
        return MUX2shift16_Y7
    elif pinIn[i] == 127:
        return MUX2shift16_Y7
    elif pinIn[i] == 128:
        return MUX2shift16_Y7
    elif pinIn[i] == 129:
        return MUX3shift17_Y0
    elif pinIn[i] == 130:
        return MUX3shift17_Y0
    elif pinIn[i] == 131:
        return MUX3shift17_Y0
    elif pinIn[i] == 132:
        return MUX3shift17_Y0
    elif pinIn[i] == 133:
        return MUX3shift17_Y0
    elif pinIn[i] == 134:
        return MUX3shift17_Y0
    elif pinIn[i] == 135:
        return MUX3shift17_Y0
    elif pinIn[i] == 136:
        return MUX3shift17_Y0
    elif pinIn[i] == 137:
        return MUX3shift18_Y1
    elif pinIn[i] == 138:
        return MUX3shift18_Y1
    elif pinIn[i] == 139:
        return MUX3shift18_Y1
    elif pinIn[i] == 140:
        return MUX3shift18_Y1
    elif pinIn[i] == 141:
        return MUX3shift18_Y1
    elif pinIn[i] == 142:
        return MUX3shift18_Y1
    elif pinIn[i] == 143:
        return MUX3shift18_Y1
    elif pinIn[i] == 144:
        return MUX3shift18_Y1
    elif pinIn[i] == 145:
        return MUX3shift19_Y2
    elif pinIn[i] == 146:
        return MUX3shift19_Y2
    elif pinIn[i] == 147:
        return MUX3shift19_Y2
    elif pinIn[i] == 148:
        return MUX3shift19_Y2
    elif pinIn[i] == 149:
        return MUX3shift19_Y2
    elif pinIn[i] == 150:
        return MUX3shift19_Y2
    elif pinIn[i] == 151:
        return MUX3shift19_Y2
    elif pinIn[i] == 152:
        return MUX3shift19_Y2
    elif pinIn[i] == 153:
        return MUX3shift20_Y3
    elif pinIn[i] == 154:
        return MUX3shift20_Y3
    elif pinIn[i] == 155:
        return MUX3shift20_Y3
    elif pinIn[i] == 156:
        return MUX3shift20_Y3
 

 [/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
Reply
#4
Ok so nilamo's response gave me a way to clean up my redundancy code but I still have the problem that when I call any of those variables MUX1shift1_Y0 - MUX3shift20_Y3 none of my GPIO are being set, if I use print (MUX1shift1_Y0) I get the print out of <function <lambda> at 0x73a53810>:

How do I use that <function <lambda> at 0x73a53810> to set my GPIO? I am back where I was yesterday

I have a list pinIn (in this list from pinIn[0] = 1 and it increments by 1 all the way to pinIn[155] = 156), I need to compare this list to values.
def set_GPIOs():
    if pinIn[i] in range (1,8): #If pinIn value at any point in the list is in the range 1 through 8 set the GPIOs
        MUX1shift1_Y0      #This being the GPIO so GPIOs 17,27, and 22 would all be set LOW if pinIn[0] = 1 - 8, pinIn[2] = 1-8, and so on and so on
def set_MUX_pins(first=True, second=True, third=True,fourth=True, fifth=True, sixth=True):
    GPIO.output(17, GPIO.HIGH if first else GPIO.LOW)
    GPIO.output(27, GPIO.HIGH if second else GPIO.LOW)
    GPIO.output(22, GPIO.HIGH if third else GPIO.LOW)
    GPIO.output(23, GPIO.HIGH if fourth else GPIO.LOW)
    GPIO.output(24, GPIO.HIGH if fifth else GPIO.LOW)
    GPIO.output(25, GPIO.HIGH if sixth else GPIO.LOW)

MUX1shift1_Y0 = lambda: set_MUX_pins(False,False,False,False,True,True)
MUX1shift2_Y1 = lambda: set_MUX_pins(True,False,False,False,True,True)
MUX1shift3_Y2 = lambda: set_MUX_pins(False,True,False,False,True,True)
MUX1shift4_Y3 = lambda: set_MUX_pins(True,True,False,False,True,True)
MUX1shift5_Y4 = lambda: set_MUX_pins(False,False,True,False,True,True)
MUX1shift6_Y5 = lambda: set_MUX_pins(True,False,True,False,True,True)
MUX1shift7_Y6 = lambda: set_MUX_pins(False,True,True,False,True,True)
MUX1shift8_Y7 = lambda: set_MUX_pins(True,True,True,False,True,True)
MUX2shift9_Y0 = lambda: set_MUX_pins(False,False,False,True,False,True)
MUX2shift10_Y1 = lambda: set_MUX_pins(True,False,False,True,False,True)
MUX2shift11_Y2 = lambda: set_MUX_pins(False,True,False,True,False,True)
MUX2shift12_Y3 = lambda: set_MUX_pins(True,True,False,True,False,True)
MUX2shift13_Y4 = lambda: set_MUX_pins(False,False,True,True,False,True)
MUX2shift14_Y5 = lambda: set_MUX_pins(True,False,True,True,False,True)
MUX2shift15_Y6 = lambda: set_MUX_pins(False,True,True,True,False,True)
MUX2shift16_Y7 = lambda: set_MUX_pins(True,True,True,True,False,True)
MUX3shift17_Y0 = lambda: set_MUX_pins(False,False,False,True,True,False)
MUX3shift18_Y1 = lambda: set_MUX_pins(True,False,False,True,True,False)
MUX3shift19_Y2 = lambda: set_MUX_pins(False,True,False,True,True,False)
MUX3shift20_Y3 = lambda: set_MUX_pins(True,True,False,True,True,False)
All help is appreciated, this is kicking my butt.

Also at the risk of opening a can of worms can anyone explain lambda to me and why I was told to stay away from it?

Thank you[/i]
Reply
#5
Quote: none of my GPIO are being set, if I use print (MUX1shift1_Y0) I get the print out of <function <lambda> at 0x73a53810>:

Those are functions.  They don't do anything, unless you call them.  ie: MUX1shift1_Y0 does nothing (like, literal nothing), while MUX1shift1_Y0() will call the function.

Quote:Also at the risk of opening a can of worms can anyone explain lambda to me and why I was told to stay away from it?
They're just anonymous functions.  Whoever told you to stay away from them probably said so because they can be confusing to newbies.  In python, they're a little underpowered compared to most languages which have them, but for this case, they're perfectly capable.
Reply
#6
Ugh I can't believe I missed that. Might need a day of not looking at the computer screens.

Thank you, your right it works.
Reply
#7
Don't worry about it, it's a classic mistake newcomers make.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Picture changing triggered by GPIO q_nerk 2 159 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 261 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  raspberry pi tank gpio help jatgm1 1 433 May-06-2020, 09:00 PM
Last Post: Larz60+
  Where should I place GPIO.cleanup() shallanq 2 495 Apr-11-2020, 05:02 AM
Last Post: shallanq
  Orange PI Win steering LEDs connect to GPIO djmcg 0 375 Dec-27-2019, 08:26 AM
Last Post: djmcg
  GPIO time in HIGH LOW boris_za 1 522 Dec-07-2019, 01:48 PM
Last Post: Larz60+
  RPi GPIO Flashing LED nickayres 1 907 Jun-07-2019, 01:32 PM
Last Post: gontajones
  GPIO output timing help needed skid 5 1,652 Jan-23-2018, 04:12 PM
Last Post: skid
  Use kivy app to call a php website to change the state of a gpio pin on rasperbery pi Battillo 1 1,575 Jun-05-2017, 10:33 PM
Last Post: Ofnuts
  Setting Byte Count in pyVisa Read Function mike505 0 2,935 Nov-17-2016, 07:43 PM
Last Post: mike505

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020