#!/bin/bash
# $1 fhd60/uhd30
# $2 8:yuv 8bit 10:yuv 10bit 888:rgb888 565:rgb565 555:rgb555


#set PASSWORD "radxa"
/usr/bin/expect << EOF

spawn sudo chmod 777 /sys/module/rk628_csi/parameters/resolution
expect {
    "password" {
        send "radxa\r"
    }
}
expect eof
EOF

if [ $1 = "fhd60" ]
then
	WIDTH=1920
	HEIGHT=1080
	echo 1 > /sys/module/rk628_csi/parameters/resolution
elif [ $1 = "uhd30" ]
then
	WIDTH=3840
	HEIGHT=2160
	echo 2 > /sys/module/rk628_csi/parameters/resolution
elif [ $1 = "uhd60" ]
then
    WIDTH=3840
	HEIGHT=2160
	echo 3 > /sys/module/rk628_csi/parameters/resolution
fi

if [ $1 = "fhd60" ] && [ $2 = "8" ]
then
	media-ctl -d /dev/media0 --set-v4l2 ' "m00_b_rk628-csi 3-0050":0[fmt:UYVY8_2X8/1920x1080]'
fi

if [ $1 = "fhd60" ] && [ $2 = "10" ]
then
        media-ctl -d /dev/media0 --set-v4l2 ' "m00_b_rk628-csi 3-0050":0[fmt:UYVY10_2X10/1920x1080]'
fi

if [ $1 = "fhd60" ] && [ $2 = "888" ]
then
        media-ctl -d /dev/media0 --set-v4l2 ' "m00_b_rk628-csi 3-0050":0[fmt:RGB888_1X24/1920x1080]'
fi


if [[ ($1 = "uhd30" || $1 = "uhd60")   &&  $2 = "8" ]]
then
        media-ctl -d /dev/media0 --set-v4l2 ' "m00_b_rk628-csi 3-0050":0[fmt:UYVY8_2X8/3840x2160]'
fi

if [[ ($1 = "uhd30" || $1 = "uhd60")   &&  $2 = "10" ]]
then
        media-ctl -d /dev/media0 --set-v4l2 ' "m00_b_rk628-csi 3-0050":0[fmt:UYVY10_2X10/3840x2160]'
fi

if [ $1 = "uhd30" ] && [ $2 = "888" ]
then
        media-ctl -d /dev/media0 --set-v4l2 ' "m00_b_rk628-csi 3-0050":0[fmt:RGB888_1X24/3840x2160]'
fi

#directly preview
gst-launch-1.0 v4l2src device=/dev/video0 ! videoconvert ! video/x-raw,format=NV12,width=${WIDTH},height=${HEIGHT} ! xvimagesink &

#preview with real-time fps
#gst-launch-1.0 v4l2src device=/dev/video0 ! videoconvert ! video/x-raw,format=NV12,width=${WIDTH},height=${HEIGHT} ! fpsdisplaysink video-sink="autovideosink"

#turn on camera and take a picture
#gst-launch-1.0 v4l2src device=/dev/video0 ! videoconvert ! video/x-raw,format=NV12,width=${WIDTH},height=${HEIGHT} ! jpegenc ! multifilesink location=$1_$2.jpg

#sleep 12

#gnome-screenshot -f $1_$2.png

# get 10 frames(drop the first 9 frames, save 10th)
#gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=10 ! \
#videoconvert ! \
#video/x-raw,format=NV12,width=1920,height=1080 ! \
#jpegenc ! \
#multifilesink location="file.%02d.jpg"

# delete 9 frames
#rm file.00.jpg file.01.jpg ... file.08.jpg



#wait
