http://blog.csdn.net/zhangjie201412/article/details/7340165
ok,我们已经把battery和backlight在android中的相关代码都已经介绍的差不多的,除了应用层没有涉及到,这里我们来完成一个小功能,在最初的时候也有讲到过,就是说我们的android模拟器没有真正的battery和backlight设备,我们把这2个玩意结合起来演示下,做法很简单,当我们在setting中设置背光亮度的时候我们来改变电池的电量来show给大家看,这里我们涉及到的地方只有kernel 中的driver,我们的做法是这样的:
在battery驱动中开一个thread一直push电池信息给上层,在这个thread中我们设置一个完成量来同步,也就是说当backlight发生变化的时候我们才去push电池的信息,然后我们把背光的值转换成电池电量,这样我们就可以来观察电池图标的变化,废话不说,贴代码:
首先是在battery中开一个thread:
- #include <asm/io.h>
- //+++++
- #include <linux/delay.h>
- #include <linux/kthread.h>
- struct completion priv_event;
- EXPORT_SYMBOL(pri_event);
- struct task_struct *priv_thread;
- //---------
#include <asm/io.h>
//+++++
#include <linux/delay.h>
#include <linux/kthread.h>
struct completion priv_event;
EXPORT_SYMBOL(pri_event);
struct task_struct *priv_thread;
//---------
头文件和一些变量的定义,还有就是我们完成量的定义,
- //+++++++
- init_completion(&priv_event);
- priv_thread = kthread_run(priv_handler , data, "android-thread");
//+++++++
init_completion(&priv_event);
priv_thread = kthread_run(priv_handler , data, "android-thread");
在probe函数中初始化完成量以及初始化线程。
- static int priv_handler(void *param)
- {
- struct goldfish_battery_data *data = param;
- msleep(2000);
- while(1) {
- printk("here battery thread...\n");
- wait_for_completion(&priv_event);
- msleep(1000);
- power_supply_changed(&data->battery);
- if(kthread_should_stop())
- break;
- }
- return 0;
- }
static int priv_handler(void *param)
{
struct goldfish_battery_data *data = param;
msleep(2000);
while(1) {
printk("here battery thread...\n");
wait_for_completion(&priv_event);
msleep(1000);
power_supply_changed(&data->battery);
if(kthread_should_stop())
break;
}
return 0;
}
只有到我们等到了完成量被释放的时候才会去执行power_supply_changed函数去push电池的信息给user space,
- case POWER_SUPPLY_PROP_CAPACITY:
- val->intval = global_brightness/3;
- // val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
- break;
case POWER_SUPPLY_PROP_CAPACITY:
val->intval = global_brightness/3;
// val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
break;
最终把背光值做个换算显示电池电量。
这个global_brightness是在backlight驱动中export出来的,
- static int android_backlight_update_status(struct backlight_device *bl)
- {
- struct android_pwm_data *pd = dev_get_drvdata(&bl->dev);
- int brightness = bl->props.brightness;
- int max=bl->props.max_brightness;
- /* if (bl->props.power != FB_BLANK_UNBLANK)
- brightness = 0;
- if (bl->props.fb_blank != FB_BLANK_UNBLANK)
- brightness = 0;
- */
- printk(KERN_INFO "update brightness...\n");
- if (pd->notify)
- brightness = pd->notify(pd->dev, brightness);
- //+++add
- global_brightness = brightness;
- complete(&priv_event);
- printk(KERN_INFO "complete event....\n");
- return 0;
- }
static int android_backlight_update_status(struct backlight_device *bl)
{
struct android_pwm_data *pd = dev_get_drvdata(&bl->dev);
int brightness = bl->props.brightness;
int max=bl->props.max_brightness;
/* if (bl->props.power != FB_BLANK_UNBLANK)
brightness = 0;
if (bl->props.fb_blank != FB_BLANK_UNBLANK)
brightness = 0;
*/
printk(KERN_INFO "update brightness...\n");
if (pd->notify)
brightness = pd->notify(pd->dev, brightness);
//+++add
global_brightness = brightness;
complete(&priv_event);
printk(KERN_INFO "complete event....\n");
return 0;
}
看到了吧,好了,我们重新编译下kernel,然后run emulator
进入setting中的display然后来调节brightness,观察电池图标是否跟着变化:
达到目的,结束。
-----------------------------------------------------------------------------