嵌入式QT APP實現按下電源按鍵休眠,再按一下電源按鍵即喚醒的功能---實現產品低功耗
阿新 • • 發佈:2019-01-10
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、成功實現產品的低功耗
參考: