From 1d145ea4fa601fd7d73b283c9bf78235e78d52fe Mon Sep 17 00:00:00 2001 From: Denis Demidov Date: Sat, 7 Nov 2015 09:34:36 +0300 Subject: [PATCH 1/2] Switch to group-based interface in Leds This is accompanied by rhempel/ev3dev-lang@28361dad5e4f33749ee76634eeedfc1b972b6507 --- docs/index.rst | 4 +++ ev3dev/brickpi.py | 38 ++++++++++++++++----- ev3dev/ev3.py | 66 ++++++++++++++++--------------------- templates/led-colors.liquid | 60 ++++++++++++++++++++------------- 4 files changed, 101 insertions(+), 67 deletions(-) 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..efd0b52 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..b4a2b7a 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..dcc9e27 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 }}{% unless forloop.last %}, {% endunless %}{% + 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 }}{% unless forloop.last %}, {% endunless %}{%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{% From 0f1e26de2aa4cf23aa888d7d8ce813c018e3e733 Mon Sep 17 00:00:00 2001 From: Denis Demidov Date: Sat, 7 Nov 2015 10:28:47 +0300 Subject: [PATCH 2/2] Do not omit trailing comma in case this is a one-element tuple --- ev3dev/brickpi.py | 6 +++--- ev3dev/ev3.py | 16 ++++++++-------- templates/led-colors.liquid | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ev3dev/brickpi.py b/ev3dev/brickpi.py index efd0b52..a43ed95 100644 --- a/ev3dev/brickpi.py +++ b/ev3dev/brickpi.py @@ -50,10 +50,10 @@ class Leds(object): blue_one = Led(name='brickpi1:blue:ev3dev') blue_two = Led(name='brickpi2:blue:ev3dev') - BLUE_ONE = (blue_one) - BLUE_TWO = (blue_two) + BLUE_ONE = (blue_one, ) + BLUE_TWO = (blue_two, ) - BLUE = (1) + BLUE = (1, ) @staticmethod def set_color(group, color, pct=1): diff --git a/ev3dev/ev3.py b/ev3dev/ev3.py index b4a2b7a..62ded02 100644 --- a/ev3dev/ev3.py +++ b/ev3dev/ev3.py @@ -52,14 +52,14 @@ class Leds(object): green_left = Led(name='ev3-left1:green:ev3dev') green_right = Led(name='ev3-right1:green:ev3dev') - LEFT = (red_left, green_left) - RIGHT = (red_right, green_right) - - RED = (1, 0) - GREEN = (0, 1) - AMBER = (1, 1) - ORANGE = (1, 0.5) - YELLOW = (0.5, 1) + LEFT = (red_left, green_left, ) + RIGHT = (red_right, green_right, ) + + RED = (1, 0, ) + GREEN = (0, 1, ) + AMBER = (1, 1, ) + ORANGE = (1, 0.5, ) + YELLOW = (0.5, 1, ) @staticmethod def set_color(group, color, pct=1): diff --git a/templates/led-colors.liquid b/templates/led-colors.liquid index dcc9e27..14dbda6 100644 --- a/templates/led-colors.liquid +++ b/templates/led-colors.liquid @@ -6,12 +6,12 @@ endfor %} assign groupName = group.name | upcase | underscore_spaces %} {{ groupName }} = ({% for e in group.entries %}{% assign instance = e | downcase | underscore_spaces -%}{{ instance }}{% unless forloop.last %}, {% endunless %}{% +%}{{ instance }}, {% endfor %}){% endfor %} {% for color in currentClass.colors %}{% assign colorName = color.name | upcase | underscore_spaces %} - {{ colorName }} = ({% for v in color.value %}{{ v }}{% unless forloop.last %}, {% endunless %}{%endfor %}){% + {{ colorName }} = ({% for v in color.value %}{{ v }}, {%endfor %}){% endfor %} @staticmethod