Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support a color or colour variable from tool change macros #1244

Merged
merged 5 commits into from
Feb 18, 2023

Conversation

richardjm
Copy link
Contributor

@richardjm richardjm commented Jan 21, 2023

When set the button color will use that set from the variable.

image

Example macros

############################################################################
# Complementary functions controlling variables used by mainsail for display
############################################################################

# Requires appropriately named variables in your tool change macros (these are used by mainsail)
# [gcode_macro T0]
# variable_active: 0
# variable_color: "undefined"

[gcode_macro _SET_ACTIVE_TOOL]
description: Sets the active tool in mainsail (and all others inactive)
gcode:
  {% set TOOL = params.TOOL|default(-1)|int %}
  {% for T in range(9) %}
    {% if T == TOOL %}
      SET_GCODE_VARIABLE MACRO=T{T} VARIABLE=active VALUE=1
    {% else %}
      SET_GCODE_VARIABLE MACRO=T{T} VARIABLE=active VALUE=0
    {% endif %}
  {% endfor %}

# _SET_TOOL_COLOR TOOL=<id> COLOR=<color>
# e.g. _SET_TOOL_COLOR TOOL=0 COLOR=#112233
[gcode_macro _SET_TOOL_COLOR]
description: Sets a color for mainsail extruder
gcode:
  {% set tool = params.TOOL|default(-1)|int %}
  # get color from rawparams and and remove the # char from the color code if present
  {% set ns = namespace(color="undefined") %}
  {% for param in rawparams.split(' ') %}
    {% if 'COLOR' in param|string|upper %}
      {% set rawcolor = (param.split('='))[1] %}
      {% set ns.color = rawcolor if rawcolor[0]|lower in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'] else rawcolor[1:] %}
    {% endif %}
  {% endfor %}
  SET_GCODE_VARIABLE MACRO=T{tool} VARIABLE=color VALUE='"{ns.color}"'
  # Requires [save_variables], for me this is provided by ercf
  {% if printer.save_variables is defined %}
    SAVE_VARIABLE VARIABLE=t{tool}_color VALUE='"{ns.color}"'
  {% endif %}

# Reload the colors from the [save_variables] if used
[delayed_gcode _SET_TOOL_COLORS_ON_STARTUP]
initial_duration: 1
gcode:
  {% set svv = printer.save_variables.variables %}
  {% for T in range(9) %}
    _SET_TOOL_COLOR TOOL={T} COLOR={svv['t' + (T|string) + '_color']}
  {% endfor %}

Signed-off-by: Richard Mitchell [email protected]

When set the button color will use that set from the variable

Signed-off-by: Richard Mitchell <[email protected]>
src/components/panels/ExtruderControlPanel.vue Outdated Show resolved Hide resolved
src/store/printer/getters.ts Outdated Show resolved Hide resolved
Remove support for American spelling of colour

Co-authored-by: Stefan Dej <[email protected]>
@zellneralex
Copy link
Member

zellneralex commented Feb 6, 2023

# _SET_TOOL_COLOR TOOL=<id> COLOR=<color>
# e.g. _SET_TOOL_COLOR TOOL=0 COLOR=#112233
[gcode_macro _SET_TOOL_COLOR]
description: Sets a color for mainsail extruder
gcode:
  {% set tool = params.TOOL|default(-1)|int %}
  # get color from rawparams and remove the # char from the color code
  {% set ns = namespace(color="undefined") %}
  {% for param in rawparams.split(' ') %}
    {% if 'COLOR' in param|string %}
      {% set ns.color = param[7:] %}
    {% endif %}
  {% endfor %}
  SET_GCODE_VARIABLE MACRO=T{tool} VARIABLE=color VALUE='"{ns.color}"'
  # Requires [save_variables], for me this is provided by ercf
  {% if printer.save_variables is defined %}
    SAVE_VARIABLE VARIABLE=t{tool}_color VALUE='"{ns.color}"'
  {% endif %}

avoids the need of a post processing of the gcode. We can not add the # in the description

Thanks to @pedrolamas who invents the rawparams

@zellneralex
Copy link
Member

zellneralex commented Feb 6, 2023

That version will handle both correct so you can use #12345 or 12345 as COLOR

[gcode_macro _SET_TOOL_COLOR]
description: Sets a color for mainsail extruder
gcode:
  {% set tool = params.TOOL|default(-1)|int %}
  # get color from rawparams and and remove the # char from the color code if present
  {% set ns = namespace(color="undefined") %}
  {% for param in rawparams.split(' ') %}
    {% if 'COLOR' in param|string %}
      {% set rawcolor = (param.split('='))[1] %}
      {% set ns.color = rawcolor if rawcolor[0]|lower in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'] else rawcolor[1:] %}
    {% endif %}
  {% endfor %}
  SET_GCODE_VARIABLE MACRO=T{tool} VARIABLE=color VALUE='"{ns.color}"'
  # Requires [save_variables], for me this is provided by ercf
  {% if printer.save_variables is defined %}
    SAVE_VARIABLE VARIABLE=t{tool}_color VALUE='"{ns.color}"'
  {% endif %}

That version is needed otherwise his delayed_gcode would not work anymore

@richardjm
Copy link
Contributor Author

Slight modification to the macro as it wasn't working with _SET_TOOL_COLOR tool=1 color=FF0000 (lower case COLOR argument)

# _SET_TOOL_COLOR TOOL=<id> COLOR=<color>
# e.g. _SET_TOOL_COLOR TOOL=0 COLOR=#112233
[gcode_macro _SET_TOOL_COLOR]
description: Sets a color for mainsail extruder
gcode:
  {% set tool = params.TOOL|default(-1)|int %}
  # get color from rawparams and and remove the # char from the color code if present
  {% set ns = namespace(color="undefined") %}
  {% for param in rawparams.split(' ') %}
    {% if 'COLOR' in param|string|upper %}
      {% set rawcolor = (param.split('='))[1] %}
      {% set ns.color = rawcolor if rawcolor[0]|lower in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'] else rawcolor[1:] %}
    {% endif %}
  {% endfor %}
  SET_GCODE_VARIABLE MACRO=T{tool} VARIABLE=color VALUE='"{ns.color}"'
  # Requires [save_variables], for me this is provided by ercf
  {% if printer.save_variables is defined %}
    SAVE_VARIABLE VARIABLE=t{tool}_color VALUE='"{ns.color}"'
  {% endif %}

Change to always using "color" spelling
Modify style setting

Signed-off-by: Richard Mitchell <[email protected]>
Fix boolean to int comparison flagged by editor

Signed-off-by: Richard Mitchell <[email protected]>
@zellneralex
Copy link
Member

zellneralex commented Feb 7, 2023

I promise that will be the last iteration. Now we also insure that only COLOR will work as parameter and not anything that includes COLOR in the parameter name

# _SET_TOOL_COLOR TOOL=<id> COLOR=<color>
# e.g. _SET_TOOL_COLOR TOOL=0 COLOR=#112233
[gcode_macro _SET_TOOL_COLOR]
description: Sets a color for mainsail extruder
gcode:
  {% set tool = params.TOOL|default(-1)|int %}
  # get color from rawparams and and remove the # char from the color code if present
  {% set ns = namespace(color="undefined") %}
  {% for param in rawparams.split(' ') %}
    {% if (param.split('='))[0]|lower == 'color' %}
      {% set rawcolor = (param.split('='))[1] %}
      {% set ns.color = rawcolor if rawcolor[0]|lower in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'] else rawcolor[1:] %}
    {% endif %}
  {% endfor %}
  SET_GCODE_VARIABLE MACRO=T{tool} VARIABLE=color VALUE='"{ns.color}"'
  # save value to file if [save_variables] is defined in printer.cfg
  {% if printer.save_variables is defined %}
    SAVE_VARIABLE VARIABLE=t{tool}_color VALUE='"{ns.color}"'
  {% endif %}

@zellneralex
Copy link
Member

zellneralex commented Feb 7, 2023

klipper can be ... sometimes
image

# _SET_TOOL_COLOR TOOL=<id> COLOR=<color>
# e.g. _SET_TOOL_COLOR TOOL=0 COLOR=#112233
[gcode_macro _SET_TOOL_COLOR]
description: Sets a color for mainsail extruder
gcode:
  # get color and tool from rawparams and remove the # char from the color code if present
  {% set ns = namespace(color="undefined", tool=-1) %}
  {% for param in rawparams.split(' ') %}
    {% set pname, pval = param.split('=')[0]|lower, param.split('=')[1] %}
    {% if pname == 'color' %}
      {% set ns.color = pval if pval[0]|lower in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'] else pval[1:] %}
    {% elif pname == 'tool' %}
      {% set ns.tool = pval|int %}
    {% endif %}
  {% endfor %}
  # set color if all is ok or error out
  {% if ns.tool == -1 %} 
    {action_raise_error("TOOL parameter missing")}
  {% elif printer['gcode_macro T' + ns.tool|string] is not defined %}
    {action_raise_error("gcode_macro T%d not defined" % (ns.tool))}
  {% elif printer['gcode_macro T' + ns.tool|string].color is not defined %}
    {action_raise_error("variable_color missing at gcode_macro T%d" % (ns.tool))}
  {% else %}
    SET_GCODE_VARIABLE MACRO=T{ns.tool} VARIABLE=color VALUE='"{ns.color}"'
    # save value to file if [save_variables] is defined in printer.cfg
    {% if printer.save_variables is defined %}
      SAVE_VARIABLE VARIABLE=t{ns.tool}_color VALUE='"{ns.color}"'
    {% endif %}
  {% endif %}

That are the possible error outputs if the user is doing something wrong. I find that more intuitive as that the SET_GCODE_VARIABLE errors out
image

Support alternate spelling of colour as a macro variable

Signed-off-by: Richard Mitchell <[email protected]>
@meteyou meteyou merged commit e0e90bc into mainsail-crew:develop Feb 18, 2023
@meteyou meteyou mentioned this pull request Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants