File tree Expand file tree Collapse file tree 1 file changed +34
-17
lines changed Expand file tree Collapse file tree 1 file changed +34
-17
lines changed Original file line number Diff line number Diff line change 1
- """
1
+ __doc__ = """
2
2
switch.py
3
3
=========
4
4
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
+
6
16
"""
7
17
8
18
import pyb
12
22
green_led = pyb .LED (2 )
13
23
orange_led = pyb .LED (3 )
14
24
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.\n Press 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 ()
You can’t perform that action at this time.
0 commit comments