Added snowman file
[pi-xmas.git] / xmas_tree / tree2.py
1 # Example code for [charlieplexed] GPIO Xmas Tree for
2 # Raspberry Pi by Andrew Gale.
3
4 import RPi.GPIO as GPIO
5 import time
6
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+.
10
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
15
16 # Bicolour LED fitted or not?
17 # bicolour_fitted = False # the default is False
18 bicolour_fitted = True # the default is False
19
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
26
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
31
32 RR1 = 1 # Red LEDs on right tree
33 RR2 = 2
34 RR3 = 3
35 RR4 = 4
36 RR5 = 5
37 RR6 = 6
38 RBA = 7 # Right bicolour amber
39 RBG = 8 # Right bicolour green
40 RBR = 9 # Right bicolour red
41
42 LR1 = 11 # Red LEDs on right tree
43 LR2 = 12
44 LR3 = 13
45 LR4 = 14
46 LR5 = 15
47 LR6 = 16
48 LBA = 17 # Right bicolour amber
49 LBG = 18 # Right bicolour green
50 LBR = 19 # Right bicolour red
51
52 POSSIBLE_LEDS = [RR1, RR2, RR3, RR4, RR5, RR6, RBG, RBR,
53 LR1, LR2, LR3, LR4, LR5, LR6, LBG, LBR]
54
55
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
59
60 def getrevision():
61 # Extract board revision from cpuinfo file
62 myrevision = "0000"
63 try:
64 f = open('/proc/cpuinfo','r')
65 for line in f:
66 if line[0:8]=='Revision':
67 length=len(line)
68 myrevision = line[11:length-1]
69 f.close()
70 except:
71 myrevision = "0000"
72
73 return myrevision
74
75
76
77 def single_led_on(n):
78 if (A==-1):
79 print "***********************************************"
80 print "** **"
81 print "** ERROR: you MUST call tree.setup() first!! **"
82 print "** **"
83 print "***********************************************"
84 raise Exception('You MUST call tree.setup() first!!')
85
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)
96
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
116
117 # Configure the anode and cathode nodes to be outputs
118 GPIO.setup(anode, GPIO.OUT)
119 GPIO.setup(cathode, GPIO.OUT)
120
121 # Make the anode high (+3.3v) and the cathode low (0v)
122 GPIO.output(anode, GPIO.HIGH)
123 GPIO.output(cathode, GPIO.LOW)
124
125
126 def leds_on_and_wait(leds, wait_time):
127 # This routine is passed a list of LEDs to illumnate.
128 # The whole routine
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.
133
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.
141
142 led_set = set(leds)
143 if RBA in led_set:
144 led_set.discard(RBA)
145 led_set.add(RBG)
146 led_set.add(RBR)
147 if LBA in led_set:
148 led_set.discard(LBA)
149 led_set.add(LBG)
150 led_set.add(LBR)
151
152 if led_set & set(POSSIBLE_LEDS):
153 elapsed = 0
154 while elapsed < wait_time:
155 for i in led_set:
156 single_led_on(i)
157
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
162 else:
163 this_wait = illumination_time_default
164 elapsed += this_wait
165 time.sleep(this_wait)
166 else:
167 all_leds_off()
168 time.sleep(wait_time)
169
170
171 def all_leds_off():
172 single_led_on(0)
173
174 def setup():
175 global A
176 global B
177 global C
178 global D
179 global A2
180 global B2
181 global C2
182 global D2
183 global total_illumination_time
184
185 GPIO.setmode(GPIO.BCM)
186
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
193 else:
194 print "Other model, probably Model A or Model B"
195 print "Pi 2"
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
199
200
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
205 else:
206 total_illumination_time = 8 * illumination_time_default
207
208 #print "total_illumination_time: ", total_illumination_time
209
210 def cleanup():
211 GPIO.cleanup()
212