Linux--V4L2应用程序开发(二)改变亮度

一、思路流程

创建一个新线程用来控制亮度,线程通过读取用户输入来增加或减少亮度值,并使用 ioctl 函数将新亮度值设置到视频设备。

二、代码

复制代码
/*创建线程来控制亮度*/
pthread_t thread;
pthread_create(&thread, NULL, thread_brightness_contrl,(void*)fd);



static void*thread_brightness_contrl(void* args)
{
    int fd = (int)args;
    unsigned char c;
    int brightness;
    int delta;

    struct v4l2_queryctrl qctrl;
    memset(&qctrl,0,sizeof(qctrl));
    qctrl.id = V4L2_CID_BRIGHTNESS;
    if(0 != ioctl(fd,VIDIOC_QUERYCTRL,&qctrl))
    {
        printf("can not query brightness\n");
        return NULL;
    }
    printf("brightness min = %d,max = %d\n",qctrl.minimum,qctrl.maximum);

    struct v4l2_control ctl;
    ctl.id = V4L2_CID_BRIGHTNESS;
    ioctl(fd, VIDIOC_G_CTRL,&ctl);

    while(1)
    {
        c= getchar();
        if (c=='u'||c=='U')
        {
            ctl.value += delta;
        }
        else if(c =='D'||c=='d')
        {
            ctl.value -=delta;
        }
        if(ctl.value >= qctrl.maximum) ctl.value = qctrl.maximum;
        if(ctl.value <= qctrl.minimum) ctl.value = qctrl.minimum;

        ioctl(fd,VIDIOC_S_CTRL,&ctl);
    }


    return NULL;
};

三、知识点补充

while循环关键点

  1. 阻塞等待 :当执行 c = getchar(); 时,如果没有输入,程序会在这里阻塞。这意味着线程会在此暂停,直到用户输入一个字符。

  2. 循环继续 :一旦用户输入一个字符,getchar() 返回字符并继续执行循环的剩余部分。循环并未终止,它只是被阻塞等待输入。

  3. 线程挂起 :由于 getchar() 阻塞等待输入,线程会被挂起,等待输入字符。挂起状态并不意味着 while 循环终止,而是线程暂停在 getchar() 这行代码。

相关推荐
snoopyfly~2 分钟前
Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
java·服务器·ubuntu
独行soc9 分钟前
#渗透测试#批量漏洞挖掘#HSC Mailinspector 任意文件读取漏洞(CVE-2024-34470)
linux·科技·安全·网络安全·面试·渗透测试
BD_Marathon13 分钟前
Ubuntu下Tomcat的配置
linux·ubuntu·tomcat
饥饿的半导体37 分钟前
Linux快速入门
linux·运维
BD_Marathon1 小时前
Ubuntu:Tomcat里面的catalina.sh
linux·ubuntu·tomcat
BD_Marathon1 小时前
设置LInux环境变量的方法和区别_Ubuntu/Centos
linux·ubuntu·centos
Me4神秘1 小时前
Linux国产与国外进度对垒
linux·服务器·安全
zhaowangji1 小时前
ubuntu 20.04 安装中文输入法 (sougou pin yin)
linux·ubuntu
两斤半2 小时前
Debian TTY环境乱码
linux·debian
还是奇怪3 小时前
Linux - 安全排查 2
linux·运维·安全