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


Messages In This Thread
Setting GPIO on Raspberry PI using a function - by jmjasper1 - Dec-28-2017, 10:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  function return boolean based on GPIO pin reading caslor 2 1,192 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 799 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 996 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  try function working on PC but not raspberry pi AnotherSam 1 1,543 Oct-11-2021, 04:51 AM
Last Post: bowlofred
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,941 Feb-19-2021, 06:29 AM
Last Post: LouF
  Picture changing triggered by GPIO q_nerk 2 2,590 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 2,349 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  raspberry pi tank gpio help jatgm1 1 2,421 May-06-2020, 09:00 PM
Last Post: Larz60+
  Where should I place GPIO.cleanup() shallanq 2 2,178 Apr-11-2020, 05:02 AM
Last Post: shallanq
  Orange PI Win steering LEDs connect to GPIO djmcg 0 1,544 Dec-27-2019, 08:26 AM
Last Post: djmcg

Forum Jump:

User Panel Messages

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