Mg701 Android中背光系统架构


最主要的莫过于是了解了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.   
  3. #include <stdio.h>   
  4.   
  5. #include <stdlib.h>   
  6.   
  7. #include <fcntl.h>   
  8.   
  9. #include <sys/types.h>   
  10.   
  11. #include <sys/stat.h>   
  12.   
  13. //#include <dirent.h>     
  14.   
  15. //#include <jni.h>   
  16.   
  17. #include <string.h>   
  18.   
  19. #include <android/log.h>   
  20.   
  21.    
  22.   
  23. #include "com_yecon_CtlBL_CtlBLActivity.h"   
  24.   
  25.    
  26.   
  27. #define  LOG_TAG    "ctlbl.c"   
  28.   
  29. #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)   
  30.   
  31. //#define DEV_PATH "/sys/class/leds/lcd-backlight/brightness"   
  32.   
  33. //#define DEV_PATH "/sys/devices/platform/leds-mt65xx/leds/lcd-backlight/brightness"   
  34.   
  35.    
  36.   
  37. /** 
  38.  
  39.  * native method 
  40.  
  41.  */  
  42.   
  43. //JNIEXPORT jobjectArray JNICALL Java_com_yecon_CtlBL_CtlBLActivity_ctlbl(JNIEnv * env, jobject obj)   
  44.   
  45. JNIEXPORT jint JNICALL Java_com_yecon_CtlBL_CtlBLActivity_ctlbl(JNIEnv * env, jobject obj)  
  46.   
  47. {  
  48.   
  49.    
  50.   
  51.    
  52.   
  53.     int fd;  
  54.   
  55.     int err;  
  56.   
  57.     char *p;  
  58.   
  59.     char ctl[10]={"20"};  
  60.   
  61.     LOGI("HELLO!\n");  
  62.   
  63.     //__android_log_print("");   
  64.   
  65.     //printf("call ctlbl function succ!\n");   
  66.   
  67.     fd = open("/sys/class/leds/lcd-backlight/brightness",O_RDWR);  
  68.   
  69.     if(fd < 0)  
  70.   
  71.     {  
  72.   
  73.         //fprintf(stderr,"error: open %s\n",DEV_PATH);   
  74.   
  75.         LOGI("error: open!\n");  
  76.   
  77.         exit(1);  
  78.   
  79.     }  
  80.   
  81. #if 0   
  82.   
  83.     err = read(fd,ctl,1);  
  84.   
  85.     if(err != 1)  
  86.   
  87.     {  
  88.   
  89.         //fprintf(stderr,"error: write %d!\n",err);   
  90.   
  91.           
  92.   
  93.         exit(1);  
  94.   
  95.     }else{  
  96.   
  97.         //printf("the data is %s\n",ctl[0]);   
  98.   
  99.     }  
  100.   
  101. #endif   
  102.   
  103.     err=write(fd,ctl,2);  
  104.   
  105.     //printf("%s\n",ctl);   
  106.   
  107.     if(err != 2)  
  108.   
  109.     {  
  110.   
  111.         //fprintf(stderr,"error: write %d!\n",err);   
  112.   
  113.         LOGI("error: write !\n");  
  114.   
  115.         exit(1);  
  116.   
  117.     }  
  118.   
  119.       
  120.   
  121.     close(fd);  
  122.   
  123.    
  124.   
  125.     return 0;  
  126.   
  127.       
  128.   
  129.     //return (*env)->NewStringUTF(env, "Hello ww JNI !");   
  130.   
  131.       
  132.   
  133. }  

上层Java调用的源码如下:(只是实现了一个Button,点击,有一个消息响应,将背光调到20)

  1. package com.yecon.CtlBL;  
  2.   
  3.    
  4.   
  5. import android.app.Activity;  
  6.   
  7. import android.os.Bundle;  
  8.   
  9. import android.view.View;  
  10.   
  11. import android.view.View.OnClickListener;  
  12.   
  13. import android.widget.Button;  
  14.   
  15. import android.widget.TextView;  
  16.   
  17.    
  18.   
  19. //import com.yecon.CtlBL.ctlbljni;   
  20.   
  21.    
  22.   
  23. public class CtlBLActivity extends Activity {  
  24.   
  25.     Button b  = null;  
  26.   
  27.      
  28.   
  29. //    ctl = new ctlbljni();   
  30.   
  31.     private OnClickListener clickListener = new OnClickListener(){  
  32.   
  33.    
  34.   
  35.         @Override  
  36.   
  37.         public void onClick(View v) {  
  38.   
  39.             // TODO Auto-generated method stub   
  40.   
  41.  //           ctl.ctlbl();   
  42.   
  43.               ctlbl();  
  44.   
  45.         }  
  46.   
  47.     };  
  48.   
  49.             
  50.   
  51.     /** Called when the activity is first created. */  
  52.   
  53.     @Override  
  54.   
  55.     public void onCreate(Bundle savedInstanceState) {  
  56.   
  57.         super.onCreate(savedInstanceState);  
  58.   
  59.         setContentView(R.layout.main);  
  60.   
  61.         b = (Button) this.findViewById(R.id.BtnCancel);  
  62.   
  63.         b.setOnClickListener(clickListener);  
  64.   
  65. //        TextView  tv = new TextView(this);   
  66.   
  67.  //       tv.setText( ctlbl() );   
  68.   
  69.  //       setContentView(tv);   
  70.   
  71.     }  
  72.   
  73.       
  74.   
  75.     public native int ctlbl();//本地方法   
  76.   
  77.       
  78.   
  79.     static {  
  80.   
  81.         System.loadLibrary("ctlbl");//载入so库   
  82.   
  83.     }  
  84.   
  85. }  

看上去,没几行代码,so easy!!看看高手们的实现吧!! 

  • 1
  • 2
  • 3
  • 下一页

相关内容