正文  设备功能 > 传感器 >

Android模拟器学framework和driver之传感器篇2(生成测试tool)

之前我们已经写好了自己的driver,现在我们要在android下测试我们的tool。这里我使用extern下面去编译生成一个tool,在adb shell中可以执行的,来抓...

之前我们已经写好了自己的driver,现在我们要在android下测试我们的tool。
 
 
 
 
这里我使用extern下面去编译生成一个tool,在adb shell中可以执行的,来抓取我们的温度值。
 
 
 
 
这一步相对简单,可以看做是linux的应用程序,附代码:
 
/external/temperature/temperature.c
 
<strong><span style="color:#cc33cc;">#include <stdio.h> 
#include <stdlib.h>  
#include <fcntl.h>    
#include <linux/input.h>  
 
/*
struct input_event
{
    struct timeval time;
    __u16 type;
    __u16 code;
    __s32 value;
};*/ 
 
int main(void) 

    struct input_event ev_temp; 
    int fd = open("/dev/input/event2", O_RDWR); 
 
    while(1) 
    { 
        int count = read(fd, &ev_temp, sizeof(struct input_event)); 
        if(EV_ABS == ev_temp.type && ABS_PRESSURE == ev_temp.code) 
        { 
            printf("time : %ld, %d", ev_temp.time.tv_sec, ev_temp.time.tv_usec); 
            printf(" Current Temperature: %d \n", ev_temp.value); 
        } 
    } 
    return 0; 
}</span></strong> 
这边我就不多说了,大家都能看懂,接下来是android的makefile, 
<strong><span style="color:#cc33cc;">#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> 
#include <linux/input.h>
 
/*
struct input_event
{
       struct timeval time;
       __u16 type;
       __u16 code;
       __s32 value;
};*/
 
int main(void)
{
       struct input_event ev_temp;
       int fd = open("/dev/input/event2", O_RDWR);
 
       while(1)
       {
              int count = read(fd, &ev_temp, sizeof(struct input_event));
              if(EV_ABS == ev_temp.type && ABS_PRESSURE == ev_temp.code)
              {
                     printf("time : %ld, %d", ev_temp.time.tv_sec, ev_temp.time.tv_usec);
                     printf(" Current Temperature: %d \n", ev_temp.value);
              }
       }
       return 0;
}</span></strong>
这边我就不多说了,大家都能看懂,接下来是android的makefile,/external/temperature/Android.mk 
/external/temperature/Android.mk<pre name="code" class="cpp"><strong><span style="color:#cc33cc;">LOCAL_PATH := $(call my-dir) 
 
include $(CLEAR_VARS) 
 
LOCAL_MODULE_TAGS := optional 
 
LOCAL_MODULE := temperature 
 
LOCAL_SRC_FILES := $(call all-subdir-c-files) 
 
include $(BUILD_EXECUTABLE)</span></strong> 
<pre name="code" class="cpp"><strong><span style="color:#cc33cc;">LOCAL_PATH := $(call my-dir)
 
include $(CLEAR_VARS)
 
LOCAL_MODULE_TAGS := optional
 
LOCAL_MODULE := temperature
 
LOCAL_SRC_FILES := $(call all-subdir-c-files)
 
include $(BUILD_EXECUTABLE)</span></strong> 
然后编译一下之后会在out/.../generic/system/bin下生成这个tool,
 
 
进入adb shell执行就可以了如下: 
进入adb shell执行就可以了如下:<pre name="code" class="cpp"><span style="color:#cc33cc;"><strong>root@jay:/home/jay# adb shell 
#   
#   
#   
#   
# temperature  
time : 81, 292520 Current Temperature: 1  
time : 84, 40953 Current Temperature: 2  
time : 86, 61726 Current Temperature: 3  
time : 88, 42323 Current Temperature: 4  
time : 90, 61805 Current Temperature: 5  
^C 
# </strong></span> 
<pre name="code" class="cpp"><span style="color:#cc33cc;"><strong>root@jay:/home/jay# adb shell
#
#
#
#
# temperature
time : 81, 292520 Current Temperature: 1
time : 84, 40953 Current Temperature: 2
time : 86, 61726 Current Temperature: 3
time : 88, 42323 Current Temperature: 4
time : 90, 61805 Current Temperature: 5
^C
# </strong></span>
这样就完成了,接下来是我们temperature 的HAL,敬请期待。。。
 

摘自 zhangjie201412的专栏