[gcode_macro lighten_rainbow]
gcode:
    SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=1  TRANSMIT=0 SYNC=0
    SET_LED LED=ledjes RED=1 GREEN=0.5 BLUE=0 INDEX=2  TRANSMIT=0 SYNC=0
    SET_LED LED=ledjes RED=1 GREEN=1 BLUE=0 INDEX=3  TRANSMIT=0 SYNC=0
    SET_LED LED=ledjes RED=0 GREEN=1 BLUE=0 INDEX=4  TRANSMIT=0 SYNC=0
    SET_LED LED=ledjes RED=0 GREEN=0 BLUE=1 INDEX=5  TRANSMIT=0 SYNC=0
    SET_LED LED=ledjes RED=0.3 GREEN=0 BLUE=0.5 INDEX=6  TRANSMIT=0 SYNC=0
#    SET_LED LED=ledjes RED=0.6 GREEN=0 BLUE=0.8 INDEX=7  TRANSMIT=1 SYNC=0

[gcode_macro led_update_temperatures]
gcode:
    {% set TRANSMIT = params.TRANSMIT|default(1) %}
    
    {% set extr_temp = printer.extruder.temperature %}
    #if the extruder is off
    {% if printer.extruder.target == 0 %}
        #Set the LED to red if the extruder is off but is still hot, otherwise 
        # set the color to OFF
        {% if extr_temp > 60.0 %}
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=3 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=4 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=5 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=6 TRANSMIT=0 SYNC=0
        {% else %}
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=3 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=4 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=5 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=6 TRANSMIT=0 SYNC=0
        {% endif %}     
    {% else %}
        {% set red = 1 %}
        {% set green = 0.003 * extr_temp %}
        {% set green = 1 - green %}
        {% if green < 0 %}
            {% set green = 0 %}
        {% endif %}
        {% if extr_temp < 61 %}
            {% set blue = 1 %}
        {% elif extr_temp < 121 %}
            {% set blue = 0.8 %}
        {% elif extr_temp < 181 %}
            {% set blue = 0.7 %}
        {% elif extr_temp < 215 %}
            {% set blue = 0.5 %}
        {% else %}
            {% set blue = 0.3 %}
        {% endif %}
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=3 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=4 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=5 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=6 TRANSMIT=0 SYNC=0
    {% endif %}

    {% set bed_temp = printer.heater_bed.temperature %}
    #if the hotbed is off
    {% if printer.heater_bed.target == 0 %}
        #Set the LED to red if the extruder is off but is still hot, otherwise 
        # set the color to OFF
        {% if bed_temp > 34.0 %}
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=1 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=2 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=3 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=4 TRANSMIT={ TRANSMIT } SYNC=0
        {% else %}
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=1 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=2 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=3 TRANSMIT=0 SYNC=0
            SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=4 TRANSMIT={ TRANSMIT } SYNC=0
        {% endif %}     
    {% else %}
        {% set red = 1 %}
        {% set green = 0.003 * bed_temp %}
        {% set green = 0,55 - green %}
        {% if green < 0 %}
            {% set green = 0 %}
        {% endif %}
        {% if bed_temp < 42 %}
            {% set blue = 1 %}
        {% elif bed_temp < 55 %}
            {% set blue = 0.6 %}
        {% elif bed_temp < 68 %}
            {% set blue = 0.5 %}
        {% else %}
            {% set blue = 0.3 %}
        {% endif %}
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=1 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=2 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=3 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED={ red|float * 1 } GREEN={ '%0.1f'| format(green|float) } BLUE={ '%0.1f'| format(blue|float) } INDEX=4 TRANSMIT={ TRANSMIT } SYNC=0
    {% endif %}

[gcode_macro led_update_printer_status]
gcode:
    {% set TRANSMIT = params.TRANSMIT|default(1) %}

    {% if printer.idle_timeout.state == "Idle" %}
        SET_LED LED=ledjes RED=0 GREEN=1 BLUE=0 INDEX=1 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=5 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=6 TRANSMIT=0 SYNC=0
    {% elif printer.idle_timeout.state == "Ready" %}
        SET_LED LED=ledjes RED=0 GREEN=1 BLUE=0 INDEX=1 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED=1 GREEN=1 BLUE=0 INDEX=5 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED=0 GREEN=0 BLUE=0 INDEX=6 TRANSMIT=0 SYNC=0
    {% elif printer.idle_timeout.state == "Printing" %}
        SET_LED LED=ledjes RED=0 GREEN=1 BLUE=0 INDEX=1 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED=1 GREEN=1 BLUE=0 INDEX=5 TRANSMIT=0 SYNC=0
        SET_LED LED=ledjes RED=1 GREEN=0 BLUE=0 INDEX=6 TRANSMIT=0 SYNC=0
    {% endif %}

    SET_LED LED=ledjes RED=1 GREEN=1 BLUE=1 INDEX=2 TRANSMIT={ TRANSMIT } SYNC=0
    
[delayed_gcode led_update]
initial_duration: 5
gcode:
    led_update_temperatures TRANSMIT=0
    led_update_printer_status TRANSMIT=1
    UPDATE_DELAYED_GCODE ID=led_update DURATION=5
