Skip to content

Commit 256c9bc

Browse files
committed
Generate led color functions for the EV3 platform
1 parent 1261bca commit 256c9bc

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

ev3dev.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@
3030
import os.path
3131
import fnmatch
3232
import numbers
33+
import platform
34+
35+
#------------------------------------------------------------------------------
36+
# Guess platform we are running on
37+
def current_platform():
38+
machine = platform.machine()
39+
if machine == 'armv5tejl':
40+
return 'ev3'
41+
elif machine == 'armv6l':
42+
return 'rpi'
43+
else:
44+
return 'unsupported'
3345

3446
#------------------------------------------------------------------------------
3547
# Define the base class from which all other ev3dev classes are defined.
@@ -1586,6 +1598,94 @@ def delay_off(self, value):
15861598
self.set_attr_int( 'delay_off', value )
15871599

15881600

1601+
#~autogen
1602+
1603+
@property
1604+
def brightness_pct(self):
1605+
"""
1606+
Returns led brightness as a fraction of max_brightness
1607+
"""
1608+
return float(self.brightness) / self.max_brightness
1609+
1610+
@brightness_pct.setter
1611+
def brightness_pct(self, value):
1612+
self.brightness = value * self.max_brightness
1613+
1614+
if current_platform() == 'ev3':
1615+
#~autogen python_led-colors platforms.ev3.led>currentClass
1616+
1617+
Led.red_left = Led(name='ev3-left0:red:ev3dev')
1618+
Led.red_right = Led(name='ev3-right0:red:ev3dev')
1619+
Led.green_left = Led(name='ev3-left1:green:ev3dev')
1620+
Led.green_right = Led(name='ev3-right1:green:ev3dev')
1621+
1622+
@staticmethod
1623+
def Led_mix_colors(red, green):
1624+
Led.red_left.brightness_pct = red
1625+
Led.red_right.brightness_pct = red
1626+
Led.green_left.brightness_pct = green
1627+
Led.green_right.brightness_pct = green
1628+
Led.mix_colors = Led_mix_colors
1629+
1630+
@staticmethod
1631+
def Led_set_red(pct):
1632+
Led.mix_colors(red=1*pct, green=0*pct)
1633+
Led.set_red = Led_set_red
1634+
1635+
@staticmethod
1636+
def Led_red_on():
1637+
Led.set_red(1)
1638+
Led.red_on = Led_red_on
1639+
1640+
@staticmethod
1641+
def Led_set_green(pct):
1642+
Led.mix_colors(red=0*pct, green=1*pct)
1643+
Led.set_green = Led_set_green
1644+
1645+
@staticmethod
1646+
def Led_green_on():
1647+
Led.set_green(1)
1648+
Led.green_on = Led_green_on
1649+
1650+
@staticmethod
1651+
def Led_set_amber(pct):
1652+
Led.mix_colors(red=1*pct, green=1*pct)
1653+
Led.set_amber = Led_set_amber
1654+
1655+
@staticmethod
1656+
def Led_amber_on():
1657+
Led.set_amber(1)
1658+
Led.amber_on = Led_amber_on
1659+
1660+
@staticmethod
1661+
def Led_set_orange(pct):
1662+
Led.mix_colors(red=1*pct, green=0.5*pct)
1663+
Led.set_orange = Led_set_orange
1664+
1665+
@staticmethod
1666+
def Led_orange_on():
1667+
Led.set_orange(1)
1668+
Led.orange_on = Led_orange_on
1669+
1670+
@staticmethod
1671+
def Led_set_yellow(pct):
1672+
Led.mix_colors(red=0.5*pct, green=1*pct)
1673+
Led.set_yellow = Led_set_yellow
1674+
1675+
@staticmethod
1676+
def Led_yellow_on():
1677+
Led.set_yellow(1)
1678+
Led.yellow_on = Led_yellow_on
1679+
1680+
@staticmethod
1681+
def Led_all_off():
1682+
Led.red_left.brightness = 0
1683+
Led.red_right.brightness = 0
1684+
Led.green_left.brightness = 0
1685+
Led.green_right.brightness = 0
1686+
Led.all_off = Led_all_off
1687+
1688+
15891689
#~autogen
15901690
#~autogen python_generic-class classes.powerSupply>currentClass
15911691

templates/python_led-colors.liquid

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{% for instance in currentClass.instances %}{%
2+
assign instanceName = instance.name | downcase | underscore_spaces %}
3+
Led.{{instanceName}} = Led(name='{{instance.systemName}}'){%
4+
endfor %}
5+
6+
@staticmethod
7+
def Led_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+
Led.{{instanceName}}.brightness_pct = {{groupName}}{%
16+
endfor %}{%
17+
endfor %}
18+
Led.mix_colors = Led_mix_colors
19+
{% for color in currentClass.colors %}{%
20+
assign colorName = color.name | downcase | underscore_spaces %}
21+
@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{%
25+
unless forloop.last %}, {% endunless %}{%
26+
endfor %})
27+
Led.set_{{ colorName }} = Led_set_{{ colorName }}
28+
29+
@staticmethod
30+
def Led_{{ colorName }}_on():
31+
Led.set_{{ colorName }}(1)
32+
Led.{{ colorName }}_on = Led_{{ colorName }}_on
33+
{% endfor %}
34+
@staticmethod
35+
def Led_all_off():{%
36+
for instance in currentClass.instances %}{%
37+
assign instanceName = instance.name | downcase | underscore_spaces %}
38+
Led.{{instanceName}}.brightness = 0{%
39+
endfor %}
40+
Led.all_off = Led_all_off
41+

0 commit comments

Comments
 (0)