########################################
# Camera Settings Control
########################################

[delayed_gcode LOAD_CAM_SETTINGS]
initial_duration: 2
gcode:
  CAM_BRIGHTNESS BRIGHTNESS=0
  CAM_CONTRAST CONTRAST=32
  CAM_SATURATION SATURATION=56
  CAM_HUE HUE=0
  CAM_WHITE_BALANCE_TEMPERATURE_AUTO WHITE_BALANCE_TEMPERATURE_AUTO=1
  CAM_GAMMA GAMMA=80
  CAM_GAIN GAIN=0
  CAM_POWER_LINE_FREQUENCY POWER_LINE_FREQUENCY=1
  CAM_SHARPNESS SHARPNESS=3
  CAM_BACKLIGHT_COMPENSATION BACKLIGHT_COMPENSATION=1
  CAM_EXPOSURE_AUTO EXPOSURE_AUTO=3
  CAM_EXPOSURE_AUTO_PRIORITY EXPOSURE_AUTO_PRIORITY=0


[gcode_shell_command v4l2-ctl]
command: v4l2-ctl
timeout: 5.0
verbose: True

[gcode_macro CAM_SETTINGS]
description: Show current camera settings
gcode:
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 -l"

[gcode_macro CAM_BRIGHTNESS]
description: min=-64 / max=64
gcode:
    {% set brightness = params.BRIGHTNESS|default(0) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl brightness="{brightness}

[gcode_macro CAM_CONTRAST]
description: min=0 / max=64
gcode:
    {% set contrast = params.CONTRAST|default(32) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl contrast="{contrast}

[gcode_macro CAM_SATURATION]
description: min=0 / max=128
gcode:
    {% set saturation = params.SATURATION|default(56) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl saturation="{saturation}

[gcode_macro CAM_HUE]
description: min=-40 / max=40
gcode:
    {% set hue = params.HUE|default(0) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl hue="{hue}

[gcode_macro CAM_WHITE_BALANCE_TEMPERATURE_AUTO]
description: disable=0 / enable=1
gcode:
    {% set white_balance_temperature_auto = params.WHITE_BALANCE_TEMPERATURE_AUTO|default(1) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl white_balance_temperature_auto="{white_balance_temperature_auto}

[gcode_macro CAM_GAMMA]
description: min=72 / max=500
gcode:
    {% set gamma = params.GAMMA|default(80) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl gamma="{gamma}

[gcode_macro CAM_GAIN]
description: min=0 / max=100
gcode:
    {% set gain = params.GAIN|default(0) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl gain="{gain}

[gcode_macro CAM_POWER_LINE_FREQUENCY]
description: min=0 / max=2
gcode:
    {% set power_line_frequency = params.POWER_LINE_FREQUENCY|default(1) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl power_line_frequency="{power_line_frequency}

[gcode_macro CAM_WHITE_BALANCE_TEMPERATURE]
description: min=2800 / max=6500
gcode:
    {% set white_balance_temperature = params.WHITE_BALANCE_TEMPERATURE|default(4600) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl white_balance_temperature="{white_balance_temperature}

[gcode_macro CAM_SHARPNESS]
description: min=0 / max=6
gcode:
    {% set sharpness = params.SHARPNESS|default(3) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl sharpness="{sharpness}

[gcode_macro CAM_BACKLIGHT_COMPENSATION]
description: min=0 / max=2
gcode:
    {% set backlight_compensation = params.BACKLIGHT_COMPENSATION|default(1) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl backlight_compensation="{backlight_compensation}
    
[gcode_macro CAM_EXPOSURE_AUTO]
description: manual=1 / auto=3
gcode:
    {% set exposure_auto = params.EXPOSURE_AUTO|default(3) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl exposure_auto="{exposure_auto}

[gcode_macro CAM_EXPOSURE_AUTO_PRIORITY]
description: disable=0 / enable=1
gcode:
    {% set exposure_auto_priority = params.EXPOSURE_AUTO_PRIORITY|default(0) %}
    RUN_SHELL_COMMAND CMD=v4l2-ctl PARAMS="-d /dev/video4 --set-ctrl exposure_auto_priority="{exposure_auto_priority}
