diff --git a/docs/index.rst b/docs/index.rst index 7d9a2ec..fb41ce9 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -30,6 +30,7 @@ Module interface InfraredSensor RemoteControl Led + Leds PowerSupply Button Sound @@ -109,6 +110,9 @@ Other .. autoclass:: Led :members: +.. autoclass:: Leds + :members: + .. autoclass:: PowerSupply :members: diff --git a/ev3dev/brickpi.py b/ev3dev/brickpi.py index c548666..a43ed95 100644 --- a/ev3dev/brickpi.py +++ b/ev3dev/brickpi.py @@ -50,21 +50,43 @@ class Leds(object): blue_one = Led(name='brickpi1:blue:ev3dev') blue_two = Led(name='brickpi2:blue:ev3dev') - @staticmethod - def mix_colors(blue): - Leds.blue_one.brightness_pct = blue - Leds.blue_two.brightness_pct = blue + BLUE_ONE = (blue_one, ) + BLUE_TWO = (blue_two, ) + + BLUE = (1, ) @staticmethod - def set_blue(pct): - Leds.mix_colors(blue=1 * pct) + def set_color(group, color, pct=1): + """ + Sets brigthness of leds in the given group to the values specified in + color tuple. When percentage is specified, brightness of each led is + reduced proportionally. + + Example:: + + Leds.set_color(LEFT, AMBER) + """ + for l, v in zip(group, color): + l.brightness_pct = v * pct @staticmethod - def blue_on(): - Leds.set_blue(1) + def set(group, **kwargs): + """ + Set attributes for each led in group. + + Example:: + + Leds.set(LEFT, brightness_pct=0.5, trigger='timer') + """ + for led in group: + for k in kwargs: + setattr(led, k, kwargs[k]) @staticmethod def all_off(): + """ + Turn all leds off + """ Leds.blue_one.brightness = 0 Leds.blue_two.brightness = 0 diff --git a/ev3dev/ev3.py b/ev3dev/ev3.py index fc4cdc2..62ded02 100644 --- a/ev3dev/ev3.py +++ b/ev3dev/ev3.py @@ -52,55 +52,47 @@ class Leds(object): green_left = Led(name='ev3-left1:green:ev3dev') green_right = Led(name='ev3-right1:green:ev3dev') - @staticmethod - def mix_colors(red, green): - Leds.red_left.brightness_pct = red - Leds.red_right.brightness_pct = red - Leds.green_left.brightness_pct = green - Leds.green_right.brightness_pct = green + LEFT = (red_left, green_left, ) + RIGHT = (red_right, green_right, ) - @staticmethod - def set_red(pct): - Leds.mix_colors(red=1 * pct, green=0 * pct) + RED = (1, 0, ) + GREEN = (0, 1, ) + AMBER = (1, 1, ) + ORANGE = (1, 0.5, ) + YELLOW = (0.5, 1, ) @staticmethod - def red_on(): - Leds.set_red(1) - - @staticmethod - def set_green(pct): - Leds.mix_colors(red=0 * pct, green=1 * pct) - - @staticmethod - def green_on(): - Leds.set_green(1) - - @staticmethod - def set_amber(pct): - Leds.mix_colors(red=1 * pct, green=1 * pct) + def set_color(group, color, pct=1): + """ + Sets brigthness of leds in the given group to the values specified in + color tuple. When percentage is specified, brightness of each led is + reduced proportionally. - @staticmethod - def amber_on(): - Leds.set_amber(1) + Example:: - @staticmethod - def set_orange(pct): - Leds.mix_colors(red=1 * pct, green=0.5 * pct) + Leds.set_color(LEFT, AMBER) + """ + for l, v in zip(group, color): + l.brightness_pct = v * pct @staticmethod - def orange_on(): - Leds.set_orange(1) + def set(group, **kwargs): + """ + Set attributes for each led in group. - @staticmethod - def set_yellow(pct): - Leds.mix_colors(red=0.5 * pct, green=1 * pct) + Example:: - @staticmethod - def yellow_on(): - Leds.set_yellow(1) + Leds.set(LEFT, brightness_pct=0.5, trigger='timer') + """ + for led in group: + for k in kwargs: + setattr(led, k, kwargs[k]) @staticmethod def all_off(): + """ + Turn all leds off + """ Leds.red_left.brightness = 0 Leds.red_right.brightness = 0 Leds.green_left.brightness = 0 diff --git a/templates/led-colors.liquid b/templates/led-colors.liquid index 90695c7..14dbda6 100644 --- a/templates/led-colors.liquid +++ b/templates/led-colors.liquid @@ -2,34 +2,50 @@ assign instanceName = instance.name | downcase | underscore_spaces %} {{instanceName}} = Led(name='{{instance.systemName}}'){% endfor %} - - @staticmethod - def mix_colors({% -for group in currentClass.groups%}{{ group.name | downcase | underscore_spaces }}{% - unless forloop.last %}, {% endunless %}{% -endfor %}):{% -for group in currentClass.groups %}{% - assign groupName = group.name | downcase | underscore_spaces %}{% - for instance in group.entries %}{% - assign instanceName = instance | downcase | underscore_spaces %} - Leds.{{instanceName}}.brightness_pct = {{groupName}}{% - endfor %}{% +{% for group in currentClass.groups %}{% + assign groupName = group.name | upcase | underscore_spaces %} + {{ groupName }} = ({% for e in group.entries %}{% + assign instance = e | downcase | underscore_spaces +%}{{ instance }}, {% + endfor %}){% endfor %} {% for color in currentClass.colors %}{% - assign colorName = color.name | downcase | underscore_spaces %} + assign colorName = color.name | upcase | underscore_spaces %} + {{ colorName }} = ({% for v in color.value %}{{ v }}, {%endfor %}){% +endfor %} + @staticmethod - def set_{{ colorName }}(pct): - Leds.mix_colors({% - for group in color.groups %}{{ group.name | downcase | underscore_spaces }}={{ group.value }} * pct{% - unless forloop.last %}, {% endunless %}{% - endfor %}) + def set_color(group, color, pct=1): + """ + Sets brigthness of leds in the given group to the values specified in + color tuple. When percentage is specified, brightness of each led is + reduced proportionally. + + Example:: + + Leds.set_color(LEFT, AMBER) + """ + for l, v in zip(group, color): + l.brightness_pct = v * pct @staticmethod - def {{ colorName }}_on(): - Leds.set_{{ colorName }}(1) -{% endfor %} + def set(group, **kwargs): + """ + Set attributes for each led in group. + + Example:: + + Leds.set(LEFT, brightness_pct=0.5, trigger='timer') + """ + for led in group: + for k in kwargs: + setattr(led, k, kwargs[k]) + @staticmethod - def all_off():{% + def all_off(): + """ + Turn all leds off + """{% for instance in currentClass.instances %}{% assign instanceName = instance.name | downcase | underscore_spaces %} Leds.{{instanceName}}.brightness = 0{%