#  v4l2 control settings ar0144 with v4l-ctrl on STM32MP1 BSP
#  PHYTEC 2022, V0.1

# show all v4l Controls
# ======================
v4l2-ctl [CAM_DEVICE] --all (list all)
v4l2-ctl [CAM_DEVICE] -L (list all detail)
v4l2-ctl [CAM_DEVICE] -H (help)

[CAM_DEVICE] = capture device (/dev/videoX). Default = /dev/video0
for example: 
v4l2-ctl -d /dev/video0 --all
or
v4l2-ctl --all


# set AR0144 FLIP
# ================
# Set readout flip row and/or column, 
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=horizontal_flip=0
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=vertical_flip=0


# set AR0144 AEC on/off
# ======================
# 0 = AEC on / 1 = AEC = off
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=auto_exposure=0
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=auto_exposure=0


# set AR0144 AGC ANALOG on/off
# =============================
# 0 = AGCa off / 1 = AGCa = on
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=autogain_analogue=1


# set AR0144 AGC DIGITAL on/off
# ==============================
# 0 = AGCd off / 1 = AGCd = on
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=autogain_digital=1


# set AR0144 SHUTTER
# ===================
# Number of Rows (if AEC off)
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=exposure=800


# set AR0144 SHUTTER FINE
# ========================
# Delay the Shutter in Pixel (if AEC off)
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=exposure_fine=10


# set AR0144 AEC MIN/MAX
# =======================
# Min/Max for AEC if AEC is on
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=auto_exposure_min=1
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=auto_exposure_max=800


# set AR0144 AEC AVERAGE LUMA
# ============================
# Average for AEC is on
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=auto_exposure_tgt=20480


# get AR0144 CURRENT AEC VALUE
# =============================
# Read Current Exposure Value
v4l2-ctl -d [CAM_DEVICE] --get-ctrl=auto_exposure_cur


# set AR0144 AGC ANALOG MIN
# =========================
# Minimum analog gain to be used by AE Algorithm, 0=1x, 1=2x, 2=4x, 3=8x
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=analogue_gain_auto_min=0


# set AR0144 EMBEDDED DATA
# =========================
# Set readout of embedded data, Range [1,3]
# 1 = off, 3 = on, Note: Used only sensor is stopped
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=embedded_data=1


# set AR0144 ROW BINNING
# =======================
# Set readout with row binning, Range [0,1,2]
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=row_binning=0


# set AR0144 COL BINNING
# =======================
# Set readout with col binning, Range [0,1]
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=col_binning=0


# set AR0144 HORIZONTAL SKIPPING
# ===============================
# Set readout skip horizontal, Range [0-4]
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=horizontal_skipping=0


# set AR0144 VERTICAL SKIPPING
# =============================
# Set readout skip vertical, Range [0-4]
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=vertical_skipping=0


# set AR0144 DIGITAL GAIN
# =======================
# Value/1000 = Digital Gain, Range [1000..15999]
# digital gain global
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=digital_gain=1000
# digital gain color components (e.g. for white balance)
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=digital_gain_red=1000
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=digital_gain_green_red=1000
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=digital_gain_blue=1000
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=digital_gain_green_blue=1000


# set AR0144 TEST PATTERN
# ========================
# testpattern range [0-4], xxx_pixel_value range [0-65535]
# for test_pattern=1 set the color via v4l2-ctl xxx_pixel_value 
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=test_pattern=0
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=red_pixel_value=0
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=green_red_pixel_value=0
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=blue_pixel_value=0
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=green_blue_pixel_value=0


# set AR0144 COMPANDING
# =====================
# activate A-Law compression, Note: Effect only in MIPI mode, Range [0,1]
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=companding=0


# set AR0144 BLACK LEVEL CORRECTION
# =================================
# 0 = BLC off / 1 = BLC = on
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=black_level_correction=1


# set AR0144 FLASH/STROBE
# =======================
# 0 = FLASH off / 1 = FLASH = on (Note: embedded_data must be off)
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=led_mode=0


# set AR0144 START POSITION OF FLASH/STROBE
# =========================================
# start position of flash based on "start of integration" in row_time/2, Range [-128..127]
# row_time/2 for default line_length (1488) and PixCLK (74.25MHz) = 10,02us  
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=flash_delay=0


# set AR0144 VERTICAL BLANKING
# =============================
# number of blank rows, Note: Used only sensor is stopped
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=vertical_blanking=27


# set AR0144 HORIZONTAL BLANKING
# ===============================
# number of blank columns, Note: Used only sensor is stopped
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=horizontal_blanking=208


# set AR0144 ANALOG GAIN
# ======================
# Value/1000 = Analog Gain, Range [1000..16000]
v4l2-ctl -d [CAM_DEVICE] --set-ctrl=analogue_gain=2000


# get AR0144 CURRENT LINK FREQUENCY
# ==================================
# Read Current link frequency (only for MIPI importent)
v4l2-ctl -d [CAM_DEVICE] --get-ctrl=link_frequency
