android11使用gpio口控制led狀態燈

目錄

一、簡介

二、解決方法

A、底層驅動

B、上層調用

C、驗證


一、簡介

1、需求:這里是用2個gpio口來控制LED燈,開機時默認亮藍燈,按開機鍵,休眠亮紅燈,喚醒亮藍燈。

原理圖:

這里由于主板上電阻R635未貼,所以led_sleep不啟用。

2、分析:

a.一開始是想將這2個gpio口的控制寫在背光pwm驅動中,但是該設備是不接屏幕(mipi/edp/lvds)的,直接由cpu輸出信號到hdmi屏,所以無法控制背光pwm。

同理,想寫在和屏啟動相關的驅動里面,也是無法控制的。例如由i2c控制的gm8775c。

b.所以想到在底層驅動寫一個文件節點,由上層應用去控制。

二、解決方法

A、底層驅動

這里寫了一個c文件,gpio_led.c

/** Driver for keys on GPIO lines capable of generating interrupts.** Copyright (C) 2015, Fuzhou Rockchip Electronics Co., Ltd* Copyright 2005 Phil Blundell** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License version 2 as* published by the Free Software Foundation.*/#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/wakelock.h>#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>struct vanzeak_gpio_drvdata {struct gpio_desc *power_gpio;struct gpio_desc *sleep_gpio;
};static const struct of_device_id vanzeak_gpio_match[] = {{ .compatible = "vanzeak,gpio", .data = NULL},{},
};
MODULE_DEVICE_TABLE(of, vanzeak_gpio_match);static ssize_t led_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
{struct vanzeak_gpio_drvdata *ddata = dev_get_drvdata(dev);int val = val = simple_strtol(buf, NULL, 8);if(val){gpiod_direction_output(ddata->power_gpio, 1);gpiod_direction_output(ddata->sleep_gpio, 0);}else{gpiod_direction_output(ddata->power_gpio, 0);gpiod_direction_output(ddata->sleep_gpio, 1);}return size;
}static ssize_t led_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
{return 0;
}static DEVICE_ATTR(led_enable, 0644, led_enable_show, led_enable_store);static struct attribute *led_enable_attrs[] = {&dev_attr_led_enable.attr,NULL,
};static struct attribute_group led_enable_attr_group = {.name   = "led_enable",.attrs  = led_enable_attrs,
};static int vanzeak_gpio_probe(struct platform_device *pdev)
{struct vanzeak_gpio_drvdata *ddata = NULL;struct device *dev = &pdev->dev;int ret;ddata = devm_kzalloc(dev, sizeof(struct vanzeak_gpio_drvdata),GFP_KERNEL);
//	if(ddata = NULL)
//		return -1;platform_set_drvdata(pdev, ddata);dev_set_drvdata(&pdev->dev, ddata);ddata->power_gpio = devm_gpiod_get_optional(dev, "enable", 0);if (IS_ERR(ddata->power_gpio)) {ret = PTR_ERR(ddata->power_gpio);dev_err(dev, "failed to request power GPIO: %d\n", ret);goto fail0;}ddata->sleep_gpio = devm_gpiod_get_optional(dev, "sleep", 0);if (IS_ERR(ddata->sleep_gpio)) {ret = PTR_ERR(ddata->sleep_gpio);dev_err(dev, "failed to request sleep GPIO: %d\n", ret);goto fail0;}gpiod_direction_output(ddata->power_gpio, 1);gpiod_direction_output(ddata->sleep_gpio, 0);ret = sysfs_create_group(&pdev->dev.kobj, &led_enable_attr_group);if (ret) {pr_err("failed to create attr group\n");}return 0;fail0:platform_set_drvdata(pdev, NULL);return -1;
}static int vanzeak_gpio_remove(struct platform_device *pdev)
{return 0;
}#ifdef CONFIG_PM
static int vanzeak_gpio_suspend(struct device *dev)
{struct vanzeak_gpio_drvdata *ddata = dev_get_drvdata(dev);printk("DICKE printk %s : %d\n", __func__, __LINE__);gpiod_direction_output(ddata->power_gpio, 0);return 0;
}static int vanzeak_gpio_resume(struct device *dev)
{struct vanzeak_gpio_drvdata *ddata = dev_get_drvdata(dev);printk("DICKE printk %s : %d\n", __func__, __LINE__);gpiod_direction_output(ddata->power_gpio, 1);return 0;
}static const struct dev_pm_ops vanzeak_gpio_pm_ops = {.suspend	= vanzeak_gpio_suspend,.resume		= vanzeak_gpio_resume,
};
#endifstatic struct platform_driver vanzeak_gpio_device_driver = {.probe		= vanzeak_gpio_probe,.remove		= vanzeak_gpio_remove,.driver		= {.name	= "vanzeak-gpio",.owner	= THIS_MODULE,.of_match_table = vanzeak_gpio_match,
#ifdef CONFIG_PM.pm	= &vanzeak_gpio_pm_ops,
#endif}
};static int __init vanzeak_gpio_driver_init(void)
{return platform_driver_register(&vanzeak_gpio_device_driver);
}static void __exit vanzeak_gpio_driver_exit(void)
{platform_driver_unregister(&vanzeak_gpio_device_driver);
}module_init(vanzeak_gpio_driver_init);
module_exit(vanzeak_gpio_driver_exit);
B、上層調用

由上層的休眠喚醒來控制LED的亮滅。

diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java
index af7d91cf7ba6..1bbc51a9ed91 100644
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -119,6 +119,14 @@ import java.util.Arrays;import java.util.List;import java.util.Objects;+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.BufferedWriter;
+/*** The power manager service is responsible for coordinating power management* functions on the device.
@@ -1598,6 +1606,46 @@ public final class PowerManagerService extends SystemService}}+   private void closeLed(int i){
+        String path = "/sys/devices/platform/vanzeak-gpio/led_enable/led_enable";
+        String value;
+	if(i == 1)
+		value = "1";
+	else if(i == 0)
+		value = "0";
+	else{
+		Slog.e(TAG, "data error");
+		return;
+	}
+        //  Log.i(TAG,"setGpioValue, path = [" + path + "] value = [" + value + "]");
+        File file = new File(path); 
+        if (!file.exists()) {
+            Slog.i("dxb","initOpenGpio , file is not exist!!!!");
+            return;
+        }
+        FileOutputStream fileOutputStream = null;
+        BufferedWriter bufferedWriter = null;
+        try {
+            fileOutputStream = new FileOutputStream(file);
+            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8")); 
+            bufferedWriter.write(value);
+            bufferedWriter.flush();
+            bufferedWriter.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+            Slog.i("dxb","input data error " + e.getMessage());
+        } finally {
+            if (bufferedWriter != null) {
+                try {
+                    bufferedWriter.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+    }
+private boolean userActivityNoUpdateLocked(long eventTime, int event, int flags, int uid) {if (DEBUG_SPEW) {Slog.d(TAG, "userActivityNoUpdateLocked: eventTime=" + eventTime
@@ -1690,6 +1738,8 @@ public final class PowerManagerService extends SystemServiceTrace.traceBegin(Trace.TRACE_TAG_POWER, "wakeUp");try {
+		Slog.i(TAG, "ctrol led to working mode");
+		closeLed(1);Slog.i(TAG, "Waking up from "+ PowerManagerInternal.wakefulnessToString(getWakefulnessLocked())+ " (uid=" + reasonUid
@@ -1748,6 +1798,8 @@ public final class PowerManagerService extends SystemServicetry {reason = Math.min(PowerManager.GO_TO_SLEEP_REASON_MAX,Math.max(reason, PowerManager.GO_TO_SLEEP_REASON_MIN));
+		Slog.i(TAG, "ctrol led to sleeping mode");
+		closeLed(0);Slog.i(TAG, "Going to sleep due to " + PowerManager.sleepReasonToString(reason)+ " (uid " + uid + ")...");
C、驗證

按開機鍵,休眠亮紅燈,喚醒亮藍燈。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/896986.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/896986.shtml
英文地址,請注明出處:http://en.pswp.cn/news/896986.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

windows 利用nvm 管理node.js 2025最新版

1.首先在下載nvm 下載鏈接 2. 下載最新版本的nvm 3. 同意協議 注意&#xff1a;選擇安裝路徑 之后一直下一步即可 可以取消勾選 open with Powershell 勾選后它會自動打開Powershell 這里選用cmd 輸入以下命令查看是否安裝成功 nvm version 查看已經安裝的版本 我之前自…

深入淺出:UniApp 從入門到精通全指南

https://juejin.cn/post/7440119937644101684 uni-app官網 本文是關于 UniApp 從入門到精通的全指南&#xff0c;涵蓋基礎入門&#xff08;環境搭建、創建項目、項目結構、編寫運行&#xff09;、核心概念與進階知識&#xff08;組件與開發、頁面路由與導航、數據綁定與響應式…

MySQL ——數據的增刪改查

一、DML語言 1.1 insert插入數據 語法&#xff1a;insert [into] 表名 [字段名] values(值列表)&#xff1b; 插入一行數據 第一種&#xff1a;insert into file1(id,name,age) values (1,‘aa’,11); 第二種&#xff1a;insert into file1 values(1,‘aa’,11); 插入多行數…

【CF記錄】貪心——A. Scrambled Scrabble

https://codeforces.com/contest/2045/problem/A 思路&#xff1a; 由于Y有兩種選擇&#xff0c;NG也是&#xff0c;那我們可以枚舉以下情況&#xff1a;選i個Y做輔音&#xff0c;j個NG做輔音 然后貪心選擇最長的即可&#xff0c;觀察到S最長為5000&#xff0c;即使是也不會…

C語言【指針篇】(四)

前言&#xff1a;正文1. 字符指針變量2. 數組指針變量2.1 數組指針變量是什么?2.2 數組指針變量怎么初始化 3. 二維數組傳參的本質4. 函數指針變量4.1 函數指針變量的創建4.2 函數指針變量的使用4.3 兩段有趣的代碼4.3.1 typedef關鍵字 5. 函數指針數組6. 轉移表 總結 前言&am…

React + TypeScript 實戰指南:用類型守護你的組件

TypeScript 為 React 開發帶來了強大的類型安全保障&#xff0c;這里解析常見的一些TS寫法&#xff1a; 一、組件基礎類型 1. 函數組件定義 // 顯式聲明 Props 類型并標注返回值 interface WelcomeProps {name: string;age?: number; // 可選屬性 }const Welcome: React.FC…

【玩轉正則表達式】將正則表達式中的分組(group)與替換進行結合使用

在文本處理和數據分析領域&#xff0c;正則表達式&#xff08;Regular Expressions&#xff0c;簡稱regex&#xff09;是一種功能強大的工具。它不僅能夠幫助我們匹配和搜索字符串中的特定模式&#xff0c;還能通過分組&#xff08;Grouping&#xff09;和替換&#xff08;Subs…

Flutter 學習之旅 之 flutter 不使用插件,簡單實現一個 Toast 功能

Flutter 學習之旅 之 flutter 不使用插件&#xff0c;簡單實現一個 Toast 功能 目錄 Flutter 學習之旅 之 flutter 不使用插件&#xff0c;簡單實現一個 Toast 功能 一、簡單介紹 二、簡單介紹 Toast 1. 確保正確配置 navigatorKey 2. 避免重復顯示 Toast 3. 確保 Toast …

《OpenCV》——dlib(人臉應用實例)

文章目錄 dlib庫dlib庫——人臉應用實例——表情識別dlib庫——人臉應用實例——疲勞檢測 dlib庫 dlib庫的基礎用法介紹可以參考這篇文章&#xff1a;https://blog.csdn.net/lou0720/article/details/145968062?spm1011.2415.3001.5331&#xff0c;故此這篇文章只介紹dlib的人…

學習日記-250305

閱讀論文&#xff1a;Leveraging Pedagogical Theories to Understand Student Learning Process with Graph-based Reasonable Knowledge Tracing ps:代碼邏輯最后一點還沒理順&#xff0c;明天繼續 4.2 Knowledge Memory & Knowledge Tracing 代碼研究&#xff1a; 一般…

【AI大模型】DeepSeek + Kimi 高效制作PPT實戰詳解

目錄 一、前言 二、傳統 PPT 制作問題 2.1 傳統方式制作 PPT 2.2 AI 大模型輔助制作 PPT 2.3 適用場景對比分析 2.4 最佳實踐與推薦 三、DeepSeek Kimi 高效制作PPT操作實踐 3.1 Kimi 簡介 3.2 DeepSeek Kimi 制作PPT優勢 3.2.1 DeepSeek 優勢 3.2.2 Kimi 制作PPT優…

【ESP-ADF】在 VSCode 安裝 ESP-ADF 注意事項

1.檢查網絡 如果您在中國大陸安裝&#xff0c;請使用魔法上網&#xff0c;避免無法 clone ESP-ADF 倉庫。 2.VSCode 安裝 ESP-ADF 在 VSCode 左側活動欄選擇 ESP-IDF:explorer&#xff0c;展開 advanced 并點擊 Install ESP-ADF 然后會出現選擇 ESP-ADF 安裝目錄。 如果出現…

關于2023新版PyCharm的使用

考慮到大家AI編程的需要&#xff0c;建議大家安裝新版Python解釋器和新版PyCharm&#xff0c;下載地址都可以官網進行&#xff1a; Python&#xff1a;Download Python | Python.org&#xff08;可以根據需要自行選擇&#xff0c;建議選擇3.11&#xff0c;保持交流版本一致&am…

輕松部署 Stable Diffusion WebUI 并實現局域網共享訪問:解決 Conda Python 版本不為 3.10.6 的難題

這篇博文主要為大家講解關于sd webui的部署問題&#xff0c;大家有什么不懂的可以隨時問我&#xff0c;如果沒有及時回復&#xff0c;可聯系&#xff1a;1198965922 如果后續大家需要了解怎么用代碼調用部署好的webui的接口&#xff0c;可以在評論區留言哦&#xff0c;博主可以…

Leetcode 103: 二叉樹的鋸齒形層序遍歷

Leetcode 103: 二叉樹的鋸齒形層序遍歷 問題描述&#xff1a; 給定一個二叉樹&#xff0c;返回其節點值的鋸齒形層序遍歷&#xff08;即第一層從左到右&#xff0c;第二層從右到左&#xff0c;第三層從左到右&#xff0c;依此類推&#xff09;。 適合面試的解法&#xff1a;廣…

Linux中的進程間通信的方式及其使用場景

在 Linux 系統中&#xff0c;進程間通信&#xff08;Inter-Process Communication, IPC&#xff09;是指不同進程之間傳遞數據、共享信息的機制。Linux 提供了多種進程間通信的方式&#xff0c;每種方式都有不同的特點和使用場景。以下是常見的幾種進程間通信方式及其應用場景&…

springBoot集成emqx 實現mqtt消息的發送訂閱

介紹 我們可以想象這么一個場景&#xff0c;我們java應用想要采集到電表a的每小時的用電信息&#xff0c;我們怎么拿到電表的數據&#xff1f;一般我們會想 直接 java 后臺發送請求給電表&#xff0c;然后讓電表返回數據就可以了&#xff0c;事實上&#xff0c;我們java應用發…

vue Table 表格自適應窗口高度,表頭固定

當表格內縱向內容過多時&#xff0c;可選擇固定表頭。 代碼很簡單&#xff0c;其實就是在table 里面定一個 height 屬性即可。 <template><el-table:data"tableData"height"250"borderstyle"width: 100%"><el-table-columnprop…

多線程-JUC

簡介 juc&#xff0c;java.util.concurrent包的簡稱&#xff0c;java1.5時引入。juc中提供了一系列的工具&#xff0c;可以更好地支持高并發任務 juc中提供的工具 可重入鎖 ReentrantLock 可重入鎖&#xff1a;ReentrantLock&#xff0c;可重入是指當一個線程獲取到鎖之后&…

【每日學點HarmonyOS Next知識】Web Header更新、狀態變量嵌套問題、自定義彈窗、stack圓角、Flex換行問題

【每日學點HarmonyOS Next知識】Web Header更新、狀態變量嵌套問題、自定義彈窗、stack圓角、Flex換行問題 1、HarmonyOS 有關webview Header無法更新的問題&#xff1f; 業務A頁面 打開 webivew B頁面&#xff0c;第一次打開帶了header請求&#xff0c;然后退出webview B頁面…