嵌入式QT APP实现按下电源按键休眠,再按一下电源按键即唤醒的功能---实现产品低功耗

版权声明:本文为博主原创文章,如有需要,请注明转载地址:http://blog.csdn.net/morixinguan。若是侵权用于商业用途,请联系博主,否则将追究责任 https://blog.csdn.net/morixinguan/article/details/83864260

1、修改设备树

button1 {
                     label ="return";
                     linux,code=<KEY_1>;
                     gpios=<&gpio2 1 GPIO_ACTIVE_LOW>;
                     gpio-key,wakeup;
               };

添加gpio-key,wakeup字段,表示当该按键按下后会唤醒。

2、编写C程序power_test.c

首先了解下Linux下的电源状态

- On(on)                   S0 -  Working
- Standby (standby)        S1 -  CPU and RAM are powered but not executed
- Suspend to RAM(mem)       S3 -  RAM is powered and the running content is saved to RAM
- Suspend to Disk,Hibernation(disk)   S4 -  All content is saved to Disk and power down

为了使效率高,我们采用休眠的时候写mem的方式,直接写进内存,唤醒的时候从内存读出,相对于标准方式和Disk方式要好 

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int main(void)
{
    int fd;
    char *cmd = "mem";
    fd = open("/sys/power/state", O_WRONLY, 0666);
    if(fd < 0)
    {
        printf("power off failed\n");
        return -1;
    }
    write(fd, cmd, strlen(cmd));
    close(fd);

    return 0;
}

其实程序的含义就是 echo  mem > /sys/power/state,其实是一个意思。

将power_test.c交叉编译为power_test,并将power_test拷贝到/bin目录下。

3、QT APP上添加按键休眠的方法

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    ....
    //电源键
       case  Qt::Key_PowerDown:
            //进入休眠状态
            QProcess::startDetached("power_test");
            setFocus();
            break ;
    ...
}

4、成功实现产品的低功耗

参考:

https://blog.csdn.net/u014525494/article/details/80610364?utm_source=blogxgwz6

https://bbs.csdn.net/topics/390810878

猜你喜欢

转载自blog.csdn.net/morixinguan/article/details/83864260
今日推荐