Skip to content

Commit f874979

Browse files
EricPobotddemidov
authored andcommitted
Move platform-specific led functions into separate files
1 parent a2f1267 commit f874979

File tree

4 files changed

+170
-120
lines changed

4 files changed

+170
-120
lines changed

ev3dev/brickpi.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# ------------------------------------------------------------------------------
4+
# Copyright (c) 2015 Eric Pascual
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
# THE SOFTWARE.
23+
# -----------------------------------------------------------------------------
24+
25+
"""
26+
An assortment of classes modeling specific features of the BrickPi.
27+
"""
28+
29+
from .core import *
30+
31+
32+
class Leds(object):
33+
"""
34+
The BrickPi LEDs.
35+
"""
36+
37+
# ~autogen led-colors platforms.brickpi.led>currentClass
38+
39+
blue_one = Led(name='brickpi1:blue:ev3dev')
40+
blue_two = Led(name='brickpi2:blue:ev3dev')
41+
42+
@staticmethod
43+
def mix_colors(blue):
44+
Leds.blue_one.brightness_pct = blue
45+
Leds.blue_two.brightness_pct = blue
46+
47+
@staticmethod
48+
def set_blue(pct):
49+
Leds.mix_colors(blue=1 * pct)
50+
51+
@staticmethod
52+
def blue_on():
53+
Leds.set_blue(1)
54+
55+
@staticmethod
56+
def all_off():
57+
Leds.blue_one.brightness = 0
58+
Leds.blue_two.brightness = 0
59+
60+
61+
# ~autogen

ev3dev/core.py

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,112 +1700,6 @@ def brightness_pct(self):
17001700
def brightness_pct(self, value):
17011701
self.brightness = value * self.max_brightness
17021702

1703-
if current_platform() == 'ev3':
1704-
# ~autogen led-colors platforms.ev3.led>currentClass
1705-
1706-
Led.red_left = Led(name='ev3-left0:red:ev3dev')
1707-
Led.red_right = Led(name='ev3-right0:red:ev3dev')
1708-
Led.green_left = Led(name='ev3-left1:green:ev3dev')
1709-
Led.green_right = Led(name='ev3-right1:green:ev3dev')
1710-
1711-
@staticmethod
1712-
def _Led_mix_colors(red, green):
1713-
Led.red_left.brightness_pct = red
1714-
Led.red_right.brightness_pct = red
1715-
Led.green_left.brightness_pct = green
1716-
Led.green_right.brightness_pct = green
1717-
Led.mix_colors = _Led_mix_colors
1718-
1719-
@staticmethod
1720-
def _Led_set_red(pct):
1721-
Led.mix_colors(red=1*pct, green=0*pct)
1722-
Led.set_red = _Led_set_red
1723-
1724-
@staticmethod
1725-
def _Led_red_on():
1726-
Led.set_red(1)
1727-
Led.red_on = _Led_red_on
1728-
1729-
@staticmethod
1730-
def _Led_set_green(pct):
1731-
Led.mix_colors(red=0*pct, green=1*pct)
1732-
Led.set_green = _Led_set_green
1733-
1734-
@staticmethod
1735-
def _Led_green_on():
1736-
Led.set_green(1)
1737-
Led.green_on = _Led_green_on
1738-
1739-
@staticmethod
1740-
def _Led_set_amber(pct):
1741-
Led.mix_colors(red=1*pct, green=1*pct)
1742-
Led.set_amber = _Led_set_amber
1743-
1744-
@staticmethod
1745-
def _Led_amber_on():
1746-
Led.set_amber(1)
1747-
Led.amber_on = _Led_amber_on
1748-
1749-
@staticmethod
1750-
def _Led_set_orange(pct):
1751-
Led.mix_colors(red=1*pct, green=0.5*pct)
1752-
Led.set_orange = _Led_set_orange
1753-
1754-
@staticmethod
1755-
def _Led_orange_on():
1756-
Led.set_orange(1)
1757-
Led.orange_on = _Led_orange_on
1758-
1759-
@staticmethod
1760-
def _Led_set_yellow(pct):
1761-
Led.mix_colors(red=0.5*pct, green=1*pct)
1762-
Led.set_yellow = _Led_set_yellow
1763-
1764-
@staticmethod
1765-
def _Led_yellow_on():
1766-
Led.set_yellow(1)
1767-
Led.yellow_on = _Led_yellow_on
1768-
1769-
@staticmethod
1770-
def _Led_all_off():
1771-
Led.red_left.brightness = 0
1772-
Led.red_right.brightness = 0
1773-
Led.green_left.brightness = 0
1774-
Led.green_right.brightness = 0
1775-
Led.all_off = _Led_all_off
1776-
1777-
1778-
# ~autogen
1779-
elif current_platform() == 'brickpi':
1780-
# ~autogen led-colors platforms.brickpi.led>currentClass
1781-
1782-
Led.blue_one = Led(name='brickpi1:blue:ev3dev')
1783-
Led.blue_two = Led(name='brickpi2:blue:ev3dev')
1784-
1785-
@staticmethod
1786-
def _Led_mix_colors(blue):
1787-
Led.blue_one.brightness_pct = blue
1788-
Led.blue_two.brightness_pct = blue
1789-
Led.mix_colors = _Led_mix_colors
1790-
1791-
@staticmethod
1792-
def _Led_set_blue(pct):
1793-
Led.mix_colors(blue=1*pct)
1794-
Led.set_blue = _Led_set_blue
1795-
1796-
@staticmethod
1797-
def _Led_blue_on():
1798-
Led.set_blue(1)
1799-
Led.blue_on = _Led_blue_on
1800-
1801-
@staticmethod
1802-
def _Led_all_off():
1803-
Led.blue_one.brightness = 0
1804-
Led.blue_two.brightness = 0
1805-
Led.all_off = _Led_all_off
1806-
1807-
1808-
# ~autogen
18091703

18101704
class ButtonBase(object):
18111705
"""

ev3dev/ev3.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# ------------------------------------------------------------------------------
4+
# Copyright (c) 2015 Eric Pascual
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
# THE SOFTWARE.
23+
# -----------------------------------------------------------------------------
24+
25+
"""
26+
An assortment of classes modeling specific features of the EV3 brick.
27+
"""
28+
29+
from .core import *
30+
31+
32+
class Leds(object):
33+
"""
34+
The EV3 LEDs.
35+
"""
36+
37+
# ~autogen led-colors platforms.ev3.led>currentClass
38+
39+
red_left = Led(name='ev3-left0:red:ev3dev')
40+
red_right = Led(name='ev3-right0:red:ev3dev')
41+
green_left = Led(name='ev3-left1:green:ev3dev')
42+
green_right = Led(name='ev3-right1:green:ev3dev')
43+
44+
@staticmethod
45+
def mix_colors(red, green):
46+
Leds.red_left.brightness_pct = red
47+
Leds.red_right.brightness_pct = red
48+
Leds.green_left.brightness_pct = green
49+
Leds.green_right.brightness_pct = green
50+
51+
@staticmethod
52+
def set_red(pct):
53+
Leds.mix_colors(red=1 * pct, green=0 * pct)
54+
55+
@staticmethod
56+
def red_on():
57+
Leds.set_red(1)
58+
59+
@staticmethod
60+
def set_green(pct):
61+
Leds.mix_colors(red=0 * pct, green=1 * pct)
62+
63+
@staticmethod
64+
def green_on():
65+
Leds.set_green(1)
66+
67+
@staticmethod
68+
def set_amber(pct):
69+
Leds.mix_colors(red=1 * pct, green=1 * pct)
70+
71+
@staticmethod
72+
def amber_on():
73+
Leds.set_amber(1)
74+
75+
@staticmethod
76+
def set_orange(pct):
77+
Leds.mix_colors(red=1 * pct, green=0.5 * pct)
78+
79+
@staticmethod
80+
def orange_on():
81+
Leds.set_orange(1)
82+
83+
@staticmethod
84+
def set_yellow(pct):
85+
Leds.mix_colors(red=0.5 * pct, green=1 * pct)
86+
87+
@staticmethod
88+
def yellow_on():
89+
Leds.set_yellow(1)
90+
91+
@staticmethod
92+
def all_off():
93+
Leds.red_left.brightness = 0
94+
Leds.red_right.brightness = 0
95+
Leds.green_left.brightness = 0
96+
Leds.green_right.brightness = 0
97+
98+
99+
# ~autogen

templates/led-colors.liquid

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
{% for instance in currentClass.instances %}{%
22
assign instanceName = instance.name | downcase | underscore_spaces %}
3-
Led.{{instanceName}} = Led(name='{{instance.systemName}}'){%
3+
{{instanceName}} = Led(name='{{instance.systemName}}'){%
44
endfor %}
55

66
@staticmethod
7-
def _Led_mix_colors({%
7+
def mix_colors({%
88
for group in currentClass.groups%}{{ group.name | downcase | underscore_spaces }}{%
99
unless forloop.last %}, {% endunless %}{%
1010
endfor %}):{%
1111
for group in currentClass.groups %}{%
1212
assign groupName = group.name | downcase | underscore_spaces %}{%
1313
for instance in group.entries %}{%
1414
assign instanceName = instance | downcase | underscore_spaces %}
15-
Led.{{instanceName}}.brightness_pct = {{groupName}}{%
15+
Leds.{{instanceName}}.brightness_pct = {{groupName}}{%
1616
endfor %}{%
1717
endfor %}
18-
Led.mix_colors = _Led_mix_colors
1918
{% for color in currentClass.colors %}{%
2019
assign colorName = color.name | downcase | underscore_spaces %}
2120
@staticmethod
22-
def _Led_set_{{ colorName }}(pct):
23-
Led.mix_colors({%
24-
for group in color.groups %}{{ group.name | downcase | underscore_spaces }}={{ group.value }}*pct{%
21+
def set_{{ colorName }}(pct):
22+
Leds.mix_colors({%
23+
for group in color.groups %}{{ group.name | downcase | underscore_spaces }}={{ group.value }} * pct{%
2524
unless forloop.last %}, {% endunless %}{%
2625
endfor %})
27-
Led.set_{{ colorName }} = _Led_set_{{ colorName }}
2826

2927
@staticmethod
30-
def _Led_{{ colorName }}_on():
31-
Led.set_{{ colorName }}(1)
32-
Led.{{ colorName }}_on = _Led_{{ colorName }}_on
28+
def {{ colorName }}_on():
29+
Leds.set_{{ colorName }}(1)
3330
{% endfor %}
3431
@staticmethod
35-
def _Led_all_off():{%
32+
def all_off():{%
3633
for instance in currentClass.instances %}{%
3734
assign instanceName = instance.name | downcase | underscore_spaces %}
38-
Led.{{instanceName}}.brightness = 0{%
35+
Leds.{{instanceName}}.brightness = 0{%
3936
endfor %}
40-
Led.all_off = _Led_all_off
4137

0 commit comments

Comments
 (0)