歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 系統開發之背光

Android 系統開發之背光

日期:2017/3/1 10:46:23   编辑:Linux編程
忽略了Linux下的權限問題,讓我糾結了好幾個小時啊。首先說明一下,這次學習中讓我學到的東西:

最主要的莫過於是了解了Android中jni編程,游蕩整個Android源碼,可以看到很多直接操作底層驅動接口,封裝成so庫,供Java調用的例子哦。

這次學習,也正是出於這樣的想法,沒想到這個設想高手們早就實現了哦,菜鳥現在也只能算是驗證了。诶,菜鳥就是菜鳥,有蟲子吃,就興奮的不得了。

驅動架構略,這裡只討論jni接口的實現。

一、我的設想

其實設想很簡單,找到背光驅動提供給上層的API接口,人家Android還不是一樣需要一層一層的抽象(HAL、Framework),高手們考慮的東東很多,所以才一層層抽象封裝,既然這樣,咱菜鳥不就一根筋,有蟲吃就是王道啊,我為什麼不能直接將這個驅動接口封裝成jni提供給Java呢?其實這想法很早就有了,只是到現在才驗證,確實可以啊。其實Android中還是有N多這樣的例子的。

背光驅動提供的接口是:/sys/class/leds/lcd-backlight/brightness。至於這個接口是怎麼來的??那就要去看驅動結構了。驅動注冊此接口的源碼位於:

Kernel/driver/leds/led-class.c中。

這個文件只是實現了提供上層的接口,至於真正操作硬件的驅動程序,可以給出其源碼路徑為:(硬件操作其實就是脈寬調制(PWM)),mediatek\source\kernel\drivers\leds

二、設想驗證

這裡關鍵就是要清楚jni的接口實現規則咯,不過環境搭建也比較麻煩(ndk編譯環境)。

環境搭建另外給出日志。

Jni接口的源碼如下:

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. //#include <dirent.h>
  8. //#include <jni.h>
  9. #include <string.h>
  10. #include <android/log.h>
  11. #include "com_yecon_CtlBL_CtlBLActivity.h"
  12. #define LOG_TAG "ctlbl.c"
  13. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
  14. //#define DEV_PATH "/sys/class/leds/lcd-backlight/brightness"
  15. //#define DEV_PATH "/sys/devices/platform/leds-mt65xx/leds/lcd-backlight/brightness"
  16. /**
  17. * native method
  18. */
  19. //JNIEXPORT jobjectArray JNICALL Java_com_yecon_CtlBL_CtlBLActivity_ctlbl(JNIEnv * env, jobject obj)
  20. JNIEXPORT jint JNICALL Java_com_yecon_CtlBL_CtlBLActivity_ctlbl(JNIEnv * env, jobject obj)
  21. {
  22. int fd;
  23. int err;
  24. char *p;
  25. char ctl[10]={"20"};
  26. LOGI("HELLO!\n");
  27. //__android_log_print("");
  28. //printf("call ctlbl function succ!\n");
  29. fd = open("/sys/class/leds/lcd-backlight/brightness",O_RDWR);
  30. if(fd < 0)
  31. {
  32. //fprintf(stderr,"error: open %s\n",DEV_PATH);
  33. LOGI("error: open!\n");
  34. exit(1);
  35. }
  36. #if 0
  37. err = read(fd,ctl,1);
  38. if(err != 1)
  39. {
  40. //fprintf(stderr,"error: write %d!\n",err);
  41. exit(1);
  42. }else{
  43. //printf("the data is %s\n",ctl[0]);
  44. }
  45. #endif
  46. err=write(fd,ctl,2);
  47. //printf("%s\n",ctl);
  48. if(err != 2)
  49. {
  50. //fprintf(stderr,"error: write %d!\n",err);
  51. LOGI("error: write !\n");
  52. exit(1);
  53. }
  54. close(fd);
  55. return 0;
  56. //return (*env)->NewStringUTF(env, "Hello ww JNI !");
  57. }

上層Java調用的源碼如下:(只是實現了一個Button,點擊,有一個消息響應,將背光調到20)

  1. package com.yecon.CtlBL;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. //import com.yecon.CtlBL.ctlbljni;
  9. public class CtlBLActivity extends Activity {
  10. Button b = null;
  11. // ctl = new ctlbljni();
  12. private OnClickListener clickListener = new OnClickListener(){
  13. @Override
  14. public void onClick(View v) {
  15. // TODO Auto-generated method stub
  16. // ctl.ctlbl();
  17. ctlbl();
  18. }
  19. };
  20. /** Called when the activity is first created. */
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. b = (Button) this.findViewById(R.id.BtnCancel);
  26. b.setOnClickListener(clickListener);
  27. // TextView tv = new TextView(this);
  28. // tv.setText( ctlbl() );
  29. // setContentView(tv);
  30. }
  31. public native int ctlbl();//本地方法
  32. static {
  33. System.loadLibrary("ctlbl");//載入so庫
  34. }
  35. }

看上去,沒幾行代碼,so easy!!看看高手們的實現吧!!

Copyright © Linux教程網 All Rights Reserved