Skip to content

Commit 1d145ea

Browse files
committed
Switch to group-based interface in Leds
This is accompanied by rhempel/ev3dev-lang@28361da
1 parent d2c61ab commit 1d145ea

File tree

4 files changed

+101
-67
lines changed

4 files changed

+101
-67
lines changed

docs/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Module interface
3030
InfraredSensor
3131
RemoteControl
3232
Led
33+
Leds
3334
PowerSupply
3435
Button
3536
Sound
@@ -109,6 +110,9 @@ Other
109110
.. autoclass:: Led
110111
:members:
111112

113+
.. autoclass:: Leds
114+
:members:
115+
112116
.. autoclass:: PowerSupply
113117
:members:
114118

ev3dev/brickpi.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,43 @@ class Leds(object):
5050
blue_one = Led(name='brickpi1:blue:ev3dev')
5151
blue_two = Led(name='brickpi2:blue:ev3dev')
5252

53-
@staticmethod
54-
def mix_colors(blue):
55-
Leds.blue_one.brightness_pct = blue
56-
Leds.blue_two.brightness_pct = blue
53+
BLUE_ONE = (blue_one)
54+
BLUE_TWO = (blue_two)
55+
56+
BLUE = (1)
5757

5858
@staticmethod
59-
def set_blue(pct):
60-
Leds.mix_colors(blue=1 * pct)
59+
def set_color(group, color, pct=1):
60+
"""
61+
Sets brigthness of leds in the given group to the values specified in
62+
color tuple. When percentage is specified, brightness of each led is
63+
reduced proportionally.
64+
65+
Example::
66+
67+
Leds.set_color(LEFT, AMBER)
68+
"""
69+
for l, v in zip(group, color):
70+
l.brightness_pct = v * pct
6171

6272
@staticmethod
63-
def blue_on():
64-
Leds.set_blue(1)
73+
def set(group, **kwargs):
74+
"""
75+
Set attributes for each led in group.
76+
77+
Example::
78+
79+
Leds.set(LEFT, brightness_pct=0.5, trigger='timer')
80+
"""
81+
for led in group:
82+
for k in kwargs:
83+
setattr(led, k, kwargs[k])
6584

6685
@staticmethod
6786
def all_off():
87+
"""
88+
Turn all leds off
89+
"""
6890
Leds.blue_one.brightness = 0
6991
Leds.blue_two.brightness = 0
7092

ev3dev/ev3.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -52,55 +52,47 @@ class Leds(object):
5252
green_left = Led(name='ev3-left1:green:ev3dev')
5353
green_right = Led(name='ev3-right1:green:ev3dev')
5454

55-
@staticmethod
56-
def mix_colors(red, green):
57-
Leds.red_left.brightness_pct = red
58-
Leds.red_right.brightness_pct = red
59-
Leds.green_left.brightness_pct = green
60-
Leds.green_right.brightness_pct = green
55+
LEFT = (red_left, green_left)
56+
RIGHT = (red_right, green_right)
6157

62-
@staticmethod
63-
def set_red(pct):
64-
Leds.mix_colors(red=1 * pct, green=0 * pct)
58+
RED = (1, 0)
59+
GREEN = (0, 1)
60+
AMBER = (1, 1)
61+
ORANGE = (1, 0.5)
62+
YELLOW = (0.5, 1)
6563

6664
@staticmethod
67-
def red_on():
68-
Leds.set_red(1)
69-
70-
@staticmethod
71-
def set_green(pct):
72-
Leds.mix_colors(red=0 * pct, green=1 * pct)
73-
74-
@staticmethod
75-
def green_on():
76-
Leds.set_green(1)
77-
78-
@staticmethod
79-
def set_amber(pct):
80-
Leds.mix_colors(red=1 * pct, green=1 * pct)
65+
def set_color(group, color, pct=1):
66+
"""
67+
Sets brigthness of leds in the given group to the values specified in
68+
color tuple. When percentage is specified, brightness of each led is
69+
reduced proportionally.
8170
82-
@staticmethod
83-
def amber_on():
84-
Leds.set_amber(1)
71+
Example::
8572
86-
@staticmethod
87-
def set_orange(pct):
88-
Leds.mix_colors(red=1 * pct, green=0.5 * pct)
73+
Leds.set_color(LEFT, AMBER)
74+
"""
75+
for l, v in zip(group, color):
76+
l.brightness_pct = v * pct
8977

9078
@staticmethod
91-
def orange_on():
92-
Leds.set_orange(1)
79+
def set(group, **kwargs):
80+
"""
81+
Set attributes for each led in group.
9382
94-
@staticmethod
95-
def set_yellow(pct):
96-
Leds.mix_colors(red=0.5 * pct, green=1 * pct)
83+
Example::
9784
98-
@staticmethod
99-
def yellow_on():
100-
Leds.set_yellow(1)
85+
Leds.set(LEFT, brightness_pct=0.5, trigger='timer')
86+
"""
87+
for led in group:
88+
for k in kwargs:
89+
setattr(led, k, kwargs[k])
10190

10291
@staticmethod
10392
def all_off():
93+
"""
94+
Turn all leds off
95+
"""
10496
Leds.red_left.brightness = 0
10597
Leds.red_right.brightness = 0
10698
Leds.green_left.brightness = 0

templates/led-colors.liquid

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,50 @@
22
assign instanceName = instance.name | downcase | underscore_spaces %}
33
{{instanceName}} = Led(name='{{instance.systemName}}'){%
44
endfor %}
5-
6-
@staticmethod
7-
def mix_colors({%
8-
for group in currentClass.groups%}{{ group.name | downcase | underscore_spaces }}{%
9-
unless forloop.last %}, {% endunless %}{%
10-
endfor %}):{%
11-
for group in currentClass.groups %}{%
12-
assign groupName = group.name | downcase | underscore_spaces %}{%
13-
for instance in group.entries %}{%
14-
assign instanceName = instance | downcase | underscore_spaces %}
15-
Leds.{{instanceName}}.brightness_pct = {{groupName}}{%
16-
endfor %}{%
5+
{% for group in currentClass.groups %}{%
6+
assign groupName = group.name | upcase | underscore_spaces %}
7+
{{ groupName }} = ({% for e in group.entries %}{%
8+
assign instance = e | downcase | underscore_spaces
9+
%}{{ instance }}{% unless forloop.last %}, {% endunless %}{%
10+
endfor %}){%
1711
endfor %}
1812
{% for color in currentClass.colors %}{%
19-
assign colorName = color.name | downcase | underscore_spaces %}
13+
assign colorName = color.name | upcase | underscore_spaces %}
14+
{{ colorName }} = ({% for v in color.value %}{{ v }}{% unless forloop.last %}, {% endunless %}{%endfor %}){%
15+
endfor %}
16+
2017
@staticmethod
21-
def set_{{ colorName }}(pct):
22-
Leds.mix_colors({%
23-
for group in color.groups %}{{ group.name | downcase | underscore_spaces }}={{ group.value }} * pct{%
24-
unless forloop.last %}, {% endunless %}{%
25-
endfor %})
18+
def set_color(group, color, pct=1):
19+
"""
20+
Sets brigthness of leds in the given group to the values specified in
21+
color tuple. When percentage is specified, brightness of each led is
22+
reduced proportionally.
23+
24+
Example::
25+
26+
Leds.set_color(LEFT, AMBER)
27+
"""
28+
for l, v in zip(group, color):
29+
l.brightness_pct = v * pct
2630

2731
@staticmethod
28-
def {{ colorName }}_on():
29-
Leds.set_{{ colorName }}(1)
30-
{% endfor %}
32+
def set(group, **kwargs):
33+
"""
34+
Set attributes for each led in group.
35+
36+
Example::
37+
38+
Leds.set(LEFT, brightness_pct=0.5, trigger='timer')
39+
"""
40+
for led in group:
41+
for k in kwargs:
42+
setattr(led, k, kwargs[k])
43+
3144
@staticmethod
32-
def all_off():{%
45+
def all_off():
46+
"""
47+
Turn all leds off
48+
"""{%
3349
for instance in currentClass.instances %}{%
3450
assign instanceName = instance.name | downcase | underscore_spaces %}
3551
Leds.{{instanceName}}.brightness = 0{%

0 commit comments

Comments
 (0)