1 # Example code for [charlieplexed] GPIO Xmas Tree for
2 # Raspberry Pi by Andrew Gale.
4 import RPi
.GPIO
as GPIO
7 # The tree connects to the 6 GPIO pins furthest away from the
8 # corner of Raspberry Pi i.e. *physical* pin numbers 21-26 on
9 # the model A or B and 35-40 on the B+.
11 # Some Kickstarter supporters opted to receive a 'bi-colour'
12 # LED as their stretch goal reward. This fits in the top
13 # LED position (i.e. LED_0) but actually contains a second
14 # LED that we shall call LED_7
16 # Bicolour LED fitted or not?
17 # bicolour_fitted = False # the default is False
18 bicolour_fitted
= True # the default is False
20 # The time for which each LED is illuminated.
21 # This is the place to tweak the brightness of the bicolour
22 # LEDs by altering their illumination time.
23 illumination_time_bicolour_green
= 0.004 # the ON time for the bicolour green LED
24 illumination_time_bicolour_red
= 0.004 # the ON time for the bicolour red LED
25 illumination_time_default
= 0.001 # the ON time for all the other LEDs
27 # The following constants will be configured by tree.setup()
28 # but we will set them to -1 for now.
29 A
, B
, C
, D
= -1, -1, -1, -1 # The four Charlieplexing nodes
30 total_illumination_time
= -1 # Time for one whole cycle
32 RR1
= 1 # Red LEDs on right tree
38 RBA
= 7 # Right bicolour amber
39 RBG
= 8 # Right bicolour green
40 RBR
= 9 # Right bicolour red
42 LR1
= 11 # Red LEDs on right tree
48 LBA
= 17 # Right bicolour amber
49 LBG
= 18 # Right bicolour green
50 LBR
= 19 # Right bicolour red
52 POSSIBLE_LEDS
= [RR1
, RR2
, RR3
, RR4
, RR5
, RR6
, RBG
, RBR
,
53 LR1
, LR2
, LR3
, LR4
, LR5
, LR6
, LBG
, LBR
]
56 # The following code to detect which version of Raspberry Pi
57 # you are using is courtesy of Matt Hawkins at
58 # http://www.raspberrypi-spy.co.uk
61 # Extract board revision from cpuinfo file
64 f
= open('/proc/cpuinfo','r')
66 if line
[0:8]=='Revision':
68 myrevision
= line
[11:length
-1]
79 print "***********************************************"
81 print "** ERROR: you MUST call tree.setup() first!! **"
83 print "***********************************************"
84 raise Exception('You MUST call tree.setup() first!!')
86 # First, set all the nodes to be input (effectively
87 # 'disconnecting' them from the Raspberry Pi)
88 GPIO
.setup(A
, GPIO
.IN
)
89 GPIO
.setup(B
, GPIO
.IN
)
90 GPIO
.setup(C
, GPIO
.IN
)
91 GPIO
.setup(D
, GPIO
.IN
)
92 GPIO
.setup(A2
, GPIO
.IN
)
93 GPIO
.setup(B2
, GPIO
.IN
)
94 GPIO
.setup(C2
, GPIO
.IN
)
95 GPIO
.setup(D2
, GPIO
.IN
)
97 # Now determine which nodes are connected to the anode
98 # and cathode for this LED
99 if (n
==RBG
): anode
, cathode
= C
, A
100 elif (n
==RR1
): anode
, cathode
= C
, D
101 elif (n
==RR2
): anode
, cathode
= D
, C
102 elif (n
==RR3
): anode
, cathode
= D
, B
103 elif (n
==RR4
): anode
, cathode
= B
, D
104 elif (n
==RR5
): anode
, cathode
= A
, B
105 elif (n
==RR6
): anode
, cathode
= B
, A
106 elif (n
==RBR
): anode
, cathode
= A
, C
107 elif (n
==LBG
): anode
, cathode
= C2
, A2
108 elif (n
==LR1
): anode
, cathode
= C2
, D2
109 elif (n
==LR2
): anode
, cathode
= D2
, C2
110 elif (n
==LR3
): anode
, cathode
= D2
, B2
111 elif (n
==LR4
): anode
, cathode
= B2
, D2
112 elif (n
==LR5
): anode
, cathode
= A2
, B2
113 elif (n
==LR6
): anode
, cathode
= B2
, A2
114 elif (n
==LBR
): anode
, cathode
= A2
, C2
115 else: return # invalid LED number
117 # Configure the anode and cathode nodes to be outputs
118 GPIO
.setup(anode
, GPIO
.OUT
)
119 GPIO
.setup(cathode
, GPIO
.OUT
)
121 # Make the anode high (+3.3v) and the cathode low (0v)
122 GPIO
.output(anode
, GPIO
.HIGH
)
123 GPIO
.output(cathode
, GPIO
.LOW
)
126 def leds_on_and_wait(leds
, wait_time
):
127 # This routine is passed a list of LEDs to illumnate.
129 # loops around as many times as it can in the time specified
130 # in the "wait_time" parameter, thereby creating the illusion
131 # that all LEDs are on simultaneously (due to persistence
132 # of vision) when, in reality, only one is on at a time.
134 # When used with a bicolour LED at the top of the tree, this
135 # routine is passed a 9-bit value.
136 # Bit 7 is for the red LED in the bicolour LED.
137 # Bit 8 is for the green LED in the bicolour LED.
138 # Bit 0: to maintain compatibility with code for a non-bicolour version of
139 # the tree, if bit 0 is set then we want the bicolour LED to light BOTH LEDs
140 # to mimic the yellow of the non-bicolour version of the tree.
152 if led_set
& set(POSSIBLE_LEDS
):
154 while elapsed
< wait_time
:
158 if (bicolour_fitted
and (i
== RBG
or i
== LBG
)):
159 this_wait
= illumination_time_bicolour_green
160 elif (bicolour_fitted
and (i
== RBR
or i
== LBR
)):
161 this_wait
= illumination_time_bicolour_red
163 this_wait
= illumination_time_default
165 time
.sleep(this_wait
)
168 time
.sleep(wait_time
)
183 global total_illumination_time
185 GPIO
.setmode(GPIO
.BCM
)
187 # choose the correct GPIO pins depending on model
188 revision
= getrevision()
189 print "** revision: ", revision
190 if ((revision
== "0010") or (revision
== "0012")):
191 print "Model B+ or A+"
192 A
, B
, C
, D
= 21, 19, 26, 20
194 print "Other model, probably Model A or Model B"
196 # A, B, C, D = 7, 9, 11, 8
197 A
, B
, C
, D
= 15, 3, 4, 14 # near corner
198 A2
, B2
, C2
, D2
= 21, 19, 26, 20 # near USB
201 if (bicolour_fitted
):
202 total_illumination_time
= 6 * illumination_time_default
203 total_illumination_time
+= illumination_time_bicolour_green
204 total_illumination_time
+= illumination_time_bicolour_red
206 total_illumination_time
= 8 * illumination_time_default
208 #print "total_illumination_time: ", total_illumination_time