GPIO按键驱动,与系统上报key值

我们的项目需要用到4个gpio口用来做脚踏开关。模拟按键按下与抬起效果。所以接下里我们主要记录的是:如何利用内核自带的GPIO按键驱动来配置GPIO1-A0按键,并在Linux层添加按键描述和值。通过getevent确认按键上报后,进一步在Android framework层进行Linux到Android键值的映射,修改kl文件,并执行相应编译更新步骤,最后通过logcat观察按键日志。

一:kernel修改

1:首先在驱动的设备树中添加GPIO口

 gpio_keys: gpio-keys {
+        compatible = "gpio-keys";
+        pinctrl-names = "default";
+        pinctrl-0 = <&custom_keys_pin>;
+        autorepeat;
+        
+        // 定义4个自定义按键
+        key1 {
+            label = "Custom Key 1";
+            linux,code = <KEY_F1>;  // 自定义按键码1
+            gpios = <&gpio1 RK_PD3 GPIO_ACTIVE_LOW>; // GPIO0_A0
+            debounce-interval = <20>; // 去抖时间20ms
+        };
+        
+        key2 {
+            label = "Custom Key 2";
+            linux,code = <KEY_F2>;  // 自定义按键码2
+            gpios = <&gpio1 RK_PD0 GPIO_ACTIVE_LOW>; // GPIO0_A1
+            debounce-interval = <20>;
+        };
+        
+        key3 {
+            label = "Custom Key 3";
+            linux,code = <KEY_F3>;  // 自定义按键码3
+            gpios = <&gpio1 RK_PD2 GPIO_ACTIVE_LOW>; // GPIO0_A2
+            debounce-interval = <20>;
+        };
+        
+        key4 {
+            label = "Custom Key 4";
+            linux,code = <KEY_F4>;  // 自定义按键码4
+            gpios = <&gpio1 RK_PC2 GPIO_ACTIVE_LOW>; // GPIO0_A3
+            debounce-interval = <20>;
+        };
+    };


 &pinctrl {
+   custom-keys {
+        custom_keys_pin: custom-keys-pin {
+            rockchip,pins = 
+                <1 RK_PD3 RK_FUNC_GPIO &pcfg_pull_up>,   // KEY1
+                <1 RK_PD0 RK_FUNC_GPIO &pcfg_pull_up>,   // KEY2
+                <1 RK_PD2 RK_FUNC_GPIO &pcfg_pull_up>,   // KEY3
+                <1 RK_PC2 RK_FUNC_GPIO &pcfg_pull_up>;   // KEY4
+        };
+    };
};

2:在kernel中添加key值

diff --git a/include/dt-bindings/input/rk-input.h b/include/dt-bindings/input/rk-input.h
index 00b412927890..2f01769c81a4 100644
--- a/include/dt-bindings/input/rk-input.h
+++ b/include/dt-bindings/input/rk-input.h
@@ -620,6 +620,15 @@
 #define BTN_TRIGGER_HAPPY39		0x2e6
 #define BTN_TRIGGER_HAPPY40		0x2e7
 
+
+/*
+key-valuue
+*/
+#define key1			0x2e8
+#define key2			0x2e9
+#define key3			0x2F0
+#define key4			0x2F1
+



diff --git a/include/uapi/linux/input-event-codes.h b/include/uapi/linux/input-event-codes.h
index 1ce8a91349e9..f6ae0aec0d81 100644
--- a/include/uapi/linux/input-event-codes.h
+++ b/include/uapi/linux/input-event-codes.h
@@ -338,6 +338,8 @@
 
 #define KEY_MICMUTE		248	/* Mute / unmute the microphone */
 
+
+
 /* Code 255 is reserved for special needs of AT keyboard driver */
 
 #define BTN_MISC		0x100
@@ -801,6 +803,14 @@
 #define BTN_TRIGGER_HAPPY39		0x2e6
 #define BTN_TRIGGER_HAPPY40		0x2e7
 
+/*
+key-valuue
+*/
+#define key1			0x2e8
+#define key2			0x2e9
+#define key3			0x2F0
+#define key4			0x2F1
+

到这我们kernel就修改完成---编译后会用getevent会就会出现key值

console:/ # getevent
....
+add device 5: /dev/input/event2
 + name:     "gpio-keys"

二:framework的修改

1:添加hal键值映射到我们的kl文件中

diff --git a/data/keyboards/Generic.kl b/data/keyboards/Generic.kl
index 31092536bac5..93fd02e30cdf 100644
--- a/data/keyboards/Generic.kl
+++ b/data/keyboards/Generic.kl
@@ -429,6 +429,11 @@ key 657   MACRO_2
 key 658   MACRO_3
 key 659   MACRO_4
 
+key 744   ZHC_KEY_1
+key 745   ZHC_KEY_2
+key 746   ZHC_KEY_3
+key 747   ZHC_KEY_4
+
 # Keys defined by HID usages
 key usage 0x0c0067 WINDOW
 key usage 0x0c006F BRIGHTNESS_UP
diff --git a/data/keyboards/qwerty.kl b/data/keyboards/qwerty.kl
index 2fd99abbb9fe..550131f382c1 100644
--- a/data/keyboards/qwerty.kl
+++ b/data/keyboards/qwerty.kl
@@ -129,3 +129,9 @@ key 581   STEM_PRIMARY
 key 582   STEM_1
 key 583   STEM_2
 key 584   STEM_3
+
+key 744   ZHC_KEY_1
+key 745   ZHC_KEY_2
+key 746   ZHC_KEY_3
+key 747   ZHC_KEY_4
+

2:在attrs中添加我们的键值对到枚举中去

diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index f0f39d160b59..a9e2e59e1151 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -2139,6 +2139,11 @@
         <enum name="KEYCODE_DEMO_APP_2" value="302" />
         <enum name="KEYCODE_DEMO_APP_3" value="303" />
         <enum name="KEYCODE_DEMO_APP_4" value="304" />
+
+        <enum name="KEYCODE_ZHC_KEY_1" value="317" />
+        <enum name="KEYCODE_ZHC_KEY_2" value="318" />
+        <enum name="KEYCODE_ZHC_KEY_3" value="319" />
+        <enum name="KEYCODE_ZHC_KEY_4" value="320" />
     </attr>

3:在我们的KeyEvent中添加按键的值(添加的代码要写注释不然可能编译不过)

--- a/core/java/android/view/KeyEvent.java
+++ b/core/java/android/view/KeyEvent.java
@@ -923,12 +923,38 @@ public class KeyEvent extends InputEvent implements Parcelable {
     public static final int KEYCODE_MACRO_4 = 316;
 
 
+   /**
+     * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
+     * @hide
+     */
+  public static final int KEYCODE_ZHC_KEY_1 = 317;
+
+   /**
+     * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
+     * @hide
+     */
+  public static final int KEYCODE_ZHC_KEY_2 = 318;
+
+   /**
+     * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
+     * @hide
+     */
+  public static final int KEYCODE_ZHC_KEY_3 = 319;
+
+   /**
+     * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
+     * @hide
+     */
+  public static final int KEYCODE_ZHC_KEY_4 = 320;
+
+
    /**
      * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
      * @hide
      */
     @TestApi
-    public static final int LAST_KEYCODE = KEYCODE_MACRO_4;
+    public static final int LAST_KEYCODE = KEYCODE_ZHC_KEY_4;
+
 

4:接下来就在native层做下映射代码如下

diff --git a/include/android/keycodes.h b/include/android/keycodes.h
index f8fb256fae..5be9cd124c 100644
--- a/include/android/keycodes.h
+++ b/include/android/keycodes.h
@@ -839,6 +839,14 @@ enum {
     AKEYCODE_MACRO_3 = 315,
     /** User customizable key #4. */
     AKEYCODE_MACRO_4 = 316,
+   /** User customizable key #4. */
+    AKEYCODE_ZHC_KEY_1 = 317,
+   /** User customizable key #4. */
+    AKEYCODE_ZHC_KEY_2 = 318,
+   /** User customizable key #4. */
+    AKEYCODE_ZHC_KEY_3 = 319,
+   /** User customizable key #4. */
+    AKEYCODE_ZHC_KEY_4 = 32000,
 
     // NOTE: If you add a new keycode here you must also add it to several other files.
     //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
diff --git a/libs/input/InputEventLabels.cpp b/libs/input/InputEventLabels.cpp
index bade68629d..de768de960 100644
--- a/libs/input/InputEventLabels.cpp
+++ b/libs/input/InputEventLabels.cpp
@@ -347,7 +347,11 @@ namespace android {
     DEFINE_KEYCODE(MACRO_1), \
     DEFINE_KEYCODE(MACRO_2), \
     DEFINE_KEYCODE(MACRO_3), \
-    DEFINE_KEYCODE(MACRO_4)
+    DEFINE_KEYCODE(MACRO_4), \
+    DEFINE_KEYCODE(ZHC_KEY_1), \
+    DEFINE_KEYCODE(ZHC_KEY_2), \
+    DEFINE_KEYCODE(ZHC_KEY_3), \
+    DEFINE_KEYCODE(ZHC_KEY_4)

5:打开log查看检测上报情况

diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index 779074a63752..e77d44c5bd96 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -254,10 +254,10 @@ import java.util.function.Supplier;
  */
 public class PhoneWindowManager implements WindowManagerPolicy {
     static final String TAG = "WindowManager";
-    static final boolean localLOGV = false;
-    static final boolean DEBUG_INPUT = false;
-    static final boolean DEBUG_KEYGUARD = false;
-    static final boolean DEBUG_WAKEUP = false;
+    static final boolean localLOGV = true;
+    static final boolean DEBUG_INPUT = true;
+    static final boolean DEBUG_KEYGUARD = true;
+    static final boolean DEBUG_WAKEUP = true;

到这所有的代码就已经完成了

接下来编译的时候先执行一下

source build/envsetup.sh
lunch rk3576_u-userdebug
make update-api
make api-stubs-docs-non-updatable-update-current-api
再编译就OK了

会看到下面打印,就代表所有流程正常了
uioverrides.QuickstepLauncher, action=0, flags=8, keyCode=131, scanCode=58, metaState=0, repeatCount=0, policyFlags=1644167168

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值