#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
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 value 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
if (whichNumber == 0) :
turnOn(['A','B','C','D','E','F'], whichDigit)
elif (whichNumber == 1) :
turnOn(['B','C'], whichDigit)
elif (whichNumber == 2) :
turnOn(['A','B','D','E','G'], whichDigit)
elif (whichNumber == 3) :
turnOn(['A','B','C','D','G'], whichDigit)
elif (whichNumber == 4) :
turnOn(['B','C','F','G'], whichDigit)
elif (whichNumber == 5) :
turnOn(['A','C','D','F','G'], whichDigit)
elif (whichNumber == 6) :
turnOn(['A','C','D','E','F','G'], whichDigit)
elif (whichNumber == 7) :
turnOn(['A','B','C'], whichDigit)
elif (whichNumber == 8) :
turnOn(['A','B','C','D','E','F','G'], whichDigit)
elif (whichNumber == 9) :
turnOn(['A','B','C','D','F','G'], whichDigit)
elif (whichNumber == 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 (value and (list of digits)))
def multiplex(whichValue) :
# 500 times through the loop gives a long enough amount of time to read the display
for x in range(1,500) :
# Loop through the various values to be displayed
for value in whichValue :
# value[0] is the actual value to be displayed and value[1] is the list of digits to display the value on
display(value[0],value[1])
# We need a fast refresh so that the change/swap is not visible to the human eye
sleep(0.001)
return
# Run the sucker
# Dots are represented by the number 10
multiplex([
[1,[1,4]],
[9,[2]],
[2,[3]],
[10,[3]],
[6,[5]],
[8,[6]],
[10,[6]]
])
GPIO.cleanup()