功能:使用PWM来控制LED灯,通过touh按键来控制灯的亮度

1.在kernel dts中定义pwm-leds

    pwmleds {
                  status = "okay";
                  compatible = "pwm-leds";
                  front {
                          active-low;
                          label = "lamp_led";
                          max-brightness = <255>;
                          pwms = <&pwm1 0 25000 0>;
                  };
          };

2.在framework/base中拦截特定按键,控制灯的亮度变化

frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java

import android.os.FileUtils;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashSet;
import java.io.IOException;
import java.io.PrintWriter;


private static int mReadKeysDownCount = 0;
private static String mReadLedPwmControlFile = "/sys/class/leds/lamp_led/brightness";
protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) {
    /* ****************************************************************************
         * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES.
         *
         * If your key handling must happen before the app gets a crack at the event,
         * it goes in PhoneWindowManager.
         *
         * If your key handling should happen in all windows, and does not depend on
         * the state of the current application, other than that the current
         * application can override the behavior by handling the event itself, it
         * should go in PhoneFallbackEventHandler.
         *
         * Only if your handling depends on the window, and the fact that it has
         * a DecorView, should it go here.
         * ****************************************************************************/

        final KeyEvent.DispatcherState dispatcher =
                mDecor != null ? mDecor.getKeyDispatcherState() : null;
        //Log.i(TAG, "Key down: repeat=" + event.getRepeatCount()
        //        + " flags=0x" + Integer.toHexString(event.getFlags()));
        final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
        switch (keyCode) {
               case KeyEvent.KEYCODE_F1: {
                if(down /*&& SystemProperties.get("persist.touch.input", "").equals("ilitektouch")*/){
                  mReadKeysDownCount++;
                  if(mReadKeysDownCount>=4){
                        mReadKeysDownCount=0;
                        try{
                               Log.d(TAG,"max-chen key read led 0");
                               FileUtils.stringToFile(mReadLedPwmControlFile,"0");
                        }catch(IOException e){

                        }
                        return true;

                  }
                  switch(mReadKeysDownCount%3){
                          case 0:
                                  try{
                                          Log.d(TAG,"max-chen key read led 3");
                                        FileUtils.stringToFile(mReadLedPwmControlFile,"240");
                                  }catch(IOException e){

                                  }

                               break;
                          case 1://level1
                                 try{
                                         Log.d(TAG,"max-chen key read led 1");
                                  FileUtils.stringToFile(mReadLedPwmControlFile,"80");
                                 }catch(IOException e){

                                 }
                                break;
                          case 2://level2
                                try{
                                        Log.d(TAG,"max-chen key read led 2");
                                                 FileUtils.stringToFile(mReadLedPwmControlFile,"160");
                                }catch(IOException e){

                                }
                                break;
                          default:
                                break;
                  }
                }
                break;

            }

3.device/rockchip/common/init.rk30board.rc中增加权限

# lamp_led
chown system system /sys/class/leds/lamp_led/brightness
chmod 0666 /sys/class/leds/lamp_led/brightness

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐