Skip to content

Commit 9500e98

Browse files
committed
__doc__ switch, make importable, and easy to test
1 parent f94cc97 commit 9500e98

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

examples/switch.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
"""
1+
__doc__ = """
22
switch.py
33
=========
44
5-
Light up all the leds when the USR switch on the pyboard is pressed.
5+
Light up some leds when the USR switch on the pyboard is pressed.
6+
7+
Example Usage::
8+
9+
Micro Python v1.0.1 on 2014-05-12; PYBv1.0 with STM32F405RG
10+
Type "help()" for more information.
11+
>>> import switch
12+
>>> switch.run_loop([2, 3])
13+
Loop started.
14+
Press Ctrl+C to break out of the loop.
15+
616
"""
717

818
import pyb
@@ -12,18 +22,25 @@
1222
green_led = pyb.LED(2)
1323
orange_led = pyb.LED(3)
1424
blue_led = pyb.LED(4)
15-
leds = [red_led, green_led, orange_led, blue_led]
16-
17-
while 1:
18-
if switch():
19-
## red_led.on()
20-
## green_led.on()
21-
## orange_led.on()
22-
## blue_led.on()
23-
[led.on() for led in leds]
24-
else:
25-
## red_led.off()
26-
## green_led.off()
27-
## orange_led.off()
28-
## blue_led.off()
29-
[led.off() for led in leds]
25+
all_leds = [red_led, green_led, orange_led, blue_led]
26+
27+
def run_loop(use_leds=[]):
28+
"""
29+
Start the loop.
30+
31+
:param `use_leds`: Which leds to light up upon switch press.
32+
:type `use_leds`: list of integers [1-4]
33+
"""
34+
print('Loop started.\nPress Ctrl+C to break out of the loop.')
35+
leds = [all_leds[i - 1] for i in use_leds]
36+
while 1:
37+
try:
38+
if switch():
39+
[led.on() for led in leds]
40+
else:
41+
[led.off() for led in leds]
42+
except OSError: # VCPInterrupt # Ctrl+C in interpreter mode.
43+
break
44+
45+
if __name__ == '__main__':
46+
run_loop()

0 commit comments

Comments
 (0)