########################################
# Fans Control
########################################

[respond]

[duplicate_pin_override]
pins: PC0, PC5, PB2, PC6, ADC_TEMPERATURE


[temperature_fan chamber_fan]
pin: PC0
cycle_time: 0.0100
hardware_pwm: false
max_power: 1
shutdown_speed: 0
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PC5
min_temp: 0
max_temp: 70
control: watermark
max_delta: 2
target_temp: 35.0
max_speed: 1.0
min_speed: 0.0


[temperature_fan soc_fan]
pin: PB2
cycle_time: 0.0100
hardware_pwm: false
max_power: 1
shutdown_speed: 0
sensor_type: temperature_mcu
min_temp: 0
max_temp: 100
control: watermark
max_delta: 2
target_temp: 50.0
max_speed: 1.0
min_speed: 0.0
tachometer_pin:PC6


[output_pin soc_fan]
pin: PB2
pwm: True
cycle_time: 0.0100
hardware_pwm: false
value: 0.00
scale: 255
shutdown_value: 0.0


[gcode_macro M141]
description: Set Chamber Temperature with slicers
gcode:
  {% if 'S' in params|upper %}
    {% if printer["temperature_fan chamber_fan"].speed > 0.0 %}
      SET_PIN PIN=fan1 VALUE=255
    {% else %}
      SET_PIN PIN=fan1 VALUE=0
    {% endif %}
    {% if params.S|int > 0 %}
      SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=chamber_fan TARGET={params.S|default(35)}
    {% else %}
      SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=chamber_fan TARGET=35
      {% if params.S|int == 0 %}
        SET_PIN PIN=fan1 VALUE=0
      {% endif %}
    {% endif %}
  {% endif %}


[gcode_macro M191]
description: Wait for Chamber Temperature to heat up
gcode:
  {% set s = params.S|float %}
  {% set chamber_temp = printer["temperature_sensor chamber_temp"].temperature|float %}
  {% if s > 0 %}
    M141 S{s}
  {% endif %}
  {% if s > chamber_temp and s <= 90 %}
    M140 S100
    RESPOND TYPE=command MSG="Waiting for the bed to heat up the chamber..."
    TEMPERATURE_WAIT SENSOR="temperature_fan chamber_fan" MINIMUM={s-1}
    RESPOND TYPE=command MSG="Chamber target temperature reached: {s}°C"
    M140 S{s}
  {% endif %}


[gcode_macro M106]
gcode:
  {% set fans = printer["gcode_macro PRINTER_PARAM"].fans|int %}
  {% set fan = 0 %}
  {% set value = 0 %}
  {% if params.P is defined %}
    {% set tmp = params.P|int %}
    {% if tmp < fans %}
      {% set fan = tmp %}
    {% endif %}
  {% endif %}
  {% if params.S is defined %}
    {% set tmp = params.S|float %}
  {% else %}
    {% set tmp = 255 %}
  {% endif %}
  {% if tmp > 0 %}
    {% if fan == 0 %}
      {% set value = (255 - printer["gcode_macro PRINTER_PARAM"].fan0_min) / 255 * tmp %}
      {% if printer['gcode_macro Qmode'].flag | int == 1 %}
        SET_GCODE_VARIABLE MACRO=Qmode VARIABLE=fan0_value VALUE={printer["gcode_macro PRINTER_PARAM"].fan0_min + value}
        {% if value > (255 - printer['gcode_macro PRINTER_PARAM'].fan0_min) / 2  %}
          {% set value = printer["gcode_macro PRINTER_PARAM"].fan0_min + (255 - printer['gcode_macro PRINTER_PARAM'].fan0_min) / 2 %}
        {% else %}
          {% set value = printer["gcode_macro PRINTER_PARAM"].fan0_min + value %}
        {% endif %}
      {% else %}
        {% set value = printer["gcode_macro PRINTER_PARAM"].fan0_min + value %}
      {% endif %}
    {% endif %}
    {% if fan == 1 %}
      {% set value = (255 - printer["gcode_macro PRINTER_PARAM"].fan1_min) / 255 * tmp %}
      {% if printer['gcode_macro Qmode'].flag | int == 1 %}
        SET_GCODE_VARIABLE MACRO=Qmode VARIABLE=fan1_value VALUE={printer["gcode_macro PRINTER_PARAM"].fan1_min + value}
        {% if value > (255 - printer['gcode_macro PRINTER_PARAM'].fan1_min) / 2  %}
          {% set value = printer["gcode_macro PRINTER_PARAM"].fan1_min + (255 - printer['gcode_macro PRINTER_PARAM'].fan1_min) / 2 %}
        {% else %}
          {% set value = printer["gcode_macro PRINTER_PARAM"].fan1_min + value %}
        {% endif %}
      {% else %}
        {% set value = printer["gcode_macro PRINTER_PARAM"].fan1_min + value %}
      {% endif %}
    {% endif %}
    {% if fan == 2 %}
      {% set value = (255 - printer["gcode_macro PRINTER_PARAM"].fan2_min) / 255 * tmp %}
      {% if printer['gcode_macro Qmode'].flag | int == 1 %}
        SET_GCODE_VARIABLE MACRO=Qmode VARIABLE=fan2_value VALUE={printer["gcode_macro PRINTER_PARAM"].fan2_min + value}
        {% if value > (255 - printer['gcode_macro PRINTER_PARAM'].fan2_min) / 2  %}
          {% set value = printer["gcode_macro PRINTER_PARAM"].fan2_min + (255 - printer['gcode_macro PRINTER_PARAM'].fan2_min) / 2 %}
        {% else %}
          {% set value = printer["gcode_macro PRINTER_PARAM"].fan2_min + value %}
        {% endif %}
      {% else %}
        {% set value = printer["gcode_macro PRINTER_PARAM"].fan2_min + value %}
      {% endif %}
    {% endif %}
  {% endif %}
  {% if value >= 255 %}
    {% set value = 255 %}
  {% endif %}
  {% if params.P is defined and params.P|int == 3 %}
    {% set fan = 1 %}
  {% endif %}
  SET_PIN PIN=fan{fan} VALUE={value}
