#!/usr/bin/python import RPi.GPIO as GPIO #from time import sleep import socket GPIO.setmode(GPIO.BOARD) # Define Pins # These segments define the LED that will be on or off (High/Low) segmentA = 16 segmentB = 18 segmentC = 38 segmentD = 36 segmentE = 15 segmentF = 12 segmentG = 22 segmentH = 40 # These digits define which of the seven segment displays will be used (in this case we are using a 6 digit multi-display) digit1 = 7 digit2 = 11 digit3 = 13 digit4 = 29 digit5 = 31 digit6 = 33 # Set up Pins GPIO.setup(segmentA,GPIO.OUT) GPIO.setup(segmentB,GPIO.OUT) GPIO.setup(segmentC,GPIO.OUT) GPIO.setup(segmentD,GPIO.OUT) GPIO.setup(segmentE,GPIO.OUT) GPIO.setup(segmentF,GPIO.OUT) GPIO.setup(segmentG,GPIO.OUT) GPIO.setup(segmentH,GPIO.OUT) GPIO.setup(digit1,GPIO.OUT) GPIO.setup(digit2,GPIO.OUT) GPIO.setup(digit3,GPIO.OUT) GPIO.setup(digit4,GPIO.OUT) GPIO.setup(digit5,GPIO.OUT) GPIO.setup(digit6,GPIO.OUT) # Define functions # Turn on an individual LED Segment # Called by display() # Param: segmentList - A list of individual LED segments to turn on # digitList - A list of displays in the range 1-6 to display the value on def turnOn(segmentList, digitList) : # Provide common Annode to the display chosen (1 through 6) for x in digitList : whichDigit = 'digit' + str(x) GPIO.output(eval(whichDigit),GPIO.LOW) # Light up the individual segments that will produce the value passed for x in segmentList : whichSegment = 'segment' + x GPIO.output(eval(whichSegment),GPIO.HIGH) return # Turns off the individual LED Segments # Called by display() # Param: segmentList - A list of individual LED segments to turn off # digitList - A list of displays in the range 1-6 to turn off def turnOff(segmentList, digitList) : # Turn off the display by bringing the common Annode to high for x in digitList : whichDigit = 'digit' + str(x) GPIO.output(eval(whichDigit),GPIO.HIGH) # Turn off the individual LED segment by bringing the LED to low for x in segmentList : whichSegment = 'segment' + x GPIO.output(eval(whichSegment),GPIO.LOW) return # Calls turnOff() to reset the display, then turnOn() to light up the correct data # Called by multiplex() to turn on an particular value for one or multiple of the displays # Param: whichNumber - The list of values to be displayed # whichDigit - A list of segments in the range 1-6 to display the value def display(whichNumber,whichDigit) : # Turn off all segments on all digits (Start from a clean slate) turnOff(['A','B','C','D','E','F','G','H'],[1,2,3,4,5,6]) # Turn on the appropriate segments for the value passed (whichNumber) on each of the digits defined in whichDigit for value in whichNumber : if (value == 0) : turnOn(['A','B','C','D','E','F'], whichDigit) elif (value == 1) : turnOn(['B','C'], whichDigit) elif (value == 2) : turnOn(['A','B','D','E','G'], whichDigit) elif (value == 3) : turnOn(['A','B','C','D','G'], whichDigit) elif (value == 4) : turnOn(['B','C','F','G'], whichDigit) elif (value == 5) : turnOn(['A','C','D','F','G'], whichDigit) elif (value == 6) : turnOn(['A','C','D','E','F','G'], whichDigit) elif (value == 7) : turnOn(['A','B','C'], whichDigit) elif (value == 8) : turnOn(['A','B','C','D','E','F','G'], whichDigit) elif (value == 9) : turnOn(['A','B','C','D','F','G'], whichDigit) elif (value == 10) : turnOn(['H'], whichDigit) return # Calls display() to turn on a sequence of values. This is a wrapper function to loop through multiple different values # Param: whichValue - A list of (list of values and digit) # Return: Null def multiplex(whichValue) : # Loop through each sequence in the scroll for sequence in whichValue : #values = whichValue # 120 times through the loop gives a long enough amount of time to read the display. Adjust speed here. for x in range(1,120) : # Loop through the various values to be displayed for each scroll sequence for values in sequence : display(values[0],values[1]) return # Parses an IP address into individual digits or digit and dot. # Called by setupSequence() # Param: IP - An IP Address (String) # Return: An array of individual digits or digits and . def parseAddress (IP) : parsedIP = [] # Parse the IP address into a multiplexable array index = 0 while (index < len(IP)) : # Grab two digtis at a time segment = (IP[index:index+2]) # If the second digit is a ., then add both digits to the array as they are on the same digit on the display if (segment[-1] == ".") : parsedIP.append([int(segment[0]),10]) # Skip ahead so the next time through the loop we aren't evaluating the decimal as the first element index +=1 else : # The second digit is not a . so just add the first digit to the array parsedIP.append([int(segment[0])]) index += 1 #print(parsedIP) return parsedIP # Sets up the sequence of characters to be displayed in each scroll frame in an array # Param: IP - An IP Address (String) # howManyDigits is the number of digits on the segment display you are using. # Return: An array of sequences to display def setupSequence (IP,howManyDigits) : # Parse the IP address into individual digits or digits and dots. parsedIP = parseAddress(IP) # We'll need a variable to store each sequence in as our return value sequence = [] # Start the iterateCounter howManyDigits before the text begins to start the scrolling from the right side. iterateCounter = (-1*howManyDigits) # Iterate through the list till you get to the end of the IP address. while(iterateCounter < (len(parsedIP)+1) ) : # Define the array we will use to send value and position valueAndPosition = [] valuesInRange = [] # starting at the front, add blanks until you reach the first digit if (iterateCounter < 0) : # Start at the beginning. Spacer will keep track of how many blanks we need spacer = iterateCounter while (spacer < 0) : valuesInRange.append([-1]) spacer += 1 for position in range(0, iterateCounter+howManyDigits) : valuesInRange.append(parsedIP[position]) # add either a digit, or a blank space at the end depending on the iterateCounter position else : for position in range(iterateCounter, (iterateCounter+howManyDigits) ) : if (position < len(parsedIP)) : valuesInRange.append(parsedIP[position]) else : valuesInRange.append([-1]) #print str(valuesInRange) #print ("iterateCounter is : "+str(iterateCounter)) #print('my values are: ') # I probably could have dealt with this all at once above, but I'm using valuesInRange to list the howManyDigits number of characters then assigning positions here for position in range (0,howManyDigits) : #print (valuesInRange[position]) valueAndPosition.append([valuesInRange[position],[position+1]]) iterateCounter += 1 sequence.append(valueAndPosition) #print(str(sequence)) return sequence def getMyIP() : return [l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0] mySequence = setupSequence(getMyIP(),6) for cycles in range (0,3) : multiplex(mySequence) GPIO.cleanup()