【转】Android LCD和键盘 背光亮度设置
原地址: http://www.linuxidc.com/Linux/2011-03/33311p2.htm
亮度设置
应用设计
1.1 设置进度条范围
背光设置是在:设置->声音和显示->亮度,通过进度条来设置的。
文件:
packages/apps/Settings/src/com/Android/settings/BrightnessPreference.java
private static final int MINIMUM_BACKLIGHT = Android.os.Power.BRIGHTNESS_DIM + 10;private static final int MAXIMUM_BACKLIGHT = Android.os.Power.BRIGHTNESS_ON;mSeekBar.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT);
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); if (!isChecked) { setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT); } }private void setBrightness(int brightness) { try { IPowerManager power = IPowerManager.Stub.asInterface( ServiceManager.getService("power")); if (power != null) { power.setBacklightBrightness(brightness); } } catch (RemoteException doe) { } }/** * sets the brightness of the backlights (screen, keyboard, button). * * @param brightness value from 0 to 255 * * {@hide} */ public void setBacklightBrightness(int brightness) { try { mService.setBacklightBrightness(brightness); } catch (RemoteException e) { }}public void setBacklightBrightness(int brightness) { mContext.enforceCallingOrSelfPermission(Android.Manifest.permission.DEVICE_POWER, null); // Don't let applications turn the screen all the way off brightness = Math.max(brightness, Power.BRIGHTNESS_DIM); mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_BACKLIGHT, brightness, HardwareService.BRIGHTNESS_MODE_USER); mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_KEYBOARD, (mKeyboardVisible ? brightness : 0), HardwareService.BRIGHTNESS_MODE_USER); mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_BUTTONS, brightness, HardwareService.BRIGHTNESS_MODE_USER); long identity = Binder.clearCallingIdentity(); try { mBatteryStats.noteScreenBrightness(brightness); } catch (RemoteException e) { Log.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e); } finally { Binder.restoreCallingIdentity(identity); } // update our animation state if (ANIMATE_SCREEN_LIGHTS) { mScreenBrightness.curValue = brightness; mScreenBrightness.animating = false; mScreenBrightness.targetValue = -1; } if (ANIMATE_KEYBOARD_LIGHTS) { mKeyboardBrightness.curValue = brightness; mKeyboardBrightness.animating = false; mKeyboardBrightness.targetValue = -1; } if (ANIMATE_BUTTON_LIGHTS) { mButtonBrightness.curValue = brightness; mButtonBrightness.animating = false; mButtonBrightness.targetValue = -1; } }void setLightBrightness_UNCHECKED(int light, int brightness, int brightnessMode) { int b = brightness & 0x000000ff; b = 0xff000000 | (b << 16) | (b << 8) | b; setLight_native(mNativePointer, light, b, LIGHT_FLASH_NONE, 0, 0, brightnessMode); }
| b;enum { LIGHT_INDEX_BACKLIGHT = 0, LIGHT_INDEX_KEYBOARD = 1, LIGHT_INDEX_BUTTONS = 2, LIGHT_INDEX_BATTERY = 3, LIGHT_INDEX_NOTIFICATIONS = 4, LIGHT_INDEX_ATTENTION = 5, LIGHT_COUNT};#define LIGHTS_HARDWARE_MODULE_ID "lights"static jint init_native(JNIEnv *env, jobject clazz){ int err; hw_module_t* module; Devices* devices; devices = (Devices*)malloc(sizeof(Devices)); err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module); if (err == 0) { devices->lights[LIGHT_INDEX_BACKLIGHT] = get_device(module, LIGHT_ID_BACKLIGHT); devices->lights[LIGHT_INDEX_KEYBOARD] = get_device(module, LIGHT_ID_KEYBOARD); devices->lights[LIGHT_INDEX_BUTTONS] = get_device(module, LIGHT_ID_BUTTONS); devices->lights[LIGHT_INDEX_BATTERY] = get_device(module, LIGHT_ID_BATTERY); devices->lights[LIGHT_INDEX_NOTIFICATIONS] = get_device(module, LIGHT_ID_NOTIFICATIONS); devices->lights[LIGHT_INDEX_ATTENTION] = get_device(module, LIGHT_ID_ATTENTION); } else { memset(devices, 0, sizeof(Devices)); } return (jint)devices;}#define HAL_LIBRARY_PATH "/system/lib/hw"static const char *variant_keys[] = { "ro.hardware", /* This goes first so that it can pick up a different file on the emulator. */ "ro.product.board", "ro.board.platform", "ro.arch"};static const int HAL_VARIANT_KEYS_COUNT = (sizeof(variant_keys)/sizeof(variant_keys[0]));int hw_get_module(const char *id, const struct hw_module_t **module){ int status; int i; const struct hw_module_t *hmi = NULL; char prop[PATH_MAX]; char path[PATH_MAX]; /* * Here we rely on the fact that calling dlopen multiple times on * the same .so will simply increment a refcount (and not load * a new copy of the library). * We also assume that dlopen() is thread-safe. */ /* Loop through the configuration variants looking for a module */ for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) { if (i < HAL_VARIANT_KEYS_COUNT) { if (property_get(variant_keys[i], prop, NULL) == 0) { continue; } snprintf(path, sizeof(path), "%s/%s.%s.so", HAL_LIBRARY_PATH, id, prop); } else { snprintf(path, sizeof(path), "%s/%s.default.so", HAL_LIBRARY_PATH, id); } if (access(path, R_OK)) { continue; } /* we found a library matching this id/variant */ break; } status = -ENOENT; if (i < HAL_VARIANT_KEYS_COUNT+1) { /* load the module, if this fails, we're doomed, and we should not try * to load a different variant. */ status = load(id, path, module); } return status;}static void setLight_native(JNIEnv *env, jobject clazz, int ptr, int light, int colorARGB, int flashMode, int onMS, int offMS, int brightnessMode){ Devices* devices = (Devices*)ptr; light_state_t state; if (light < 0 || light >= LIGHT_COUNT || devices->lights[light] == NULL) { return ; } memset(&state, 0, sizeof(light_state_t)); state.color = colorARGB; state.flashMode = flashMode; state.flashOnMS = onMS; state.flashOffMS = offMS; state.brightnessMode = brightnessMode; devices->lights[light]->set_light(devices->lights[light], &state);}/** Open a new instance of a lights device using name */static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device){ int (*set_light)(struct light_device_t* dev, struct light_state_t const* state); if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) { set_light = set_light_backlight; } else if (0 == strcmp(LIGHT_ID_KEYBOARD, name)) { set_light = set_light_keyboard; } else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) { set_light = set_light_buttons; } else if (0 == strcmp(LIGHT_ID_BATTERY, name)) { set_light = set_light_battery; } else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) { set_light = set_light_notifications; } else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) { set_light = set_light_attention; } else { return -EINVAL; } pthread_once(&g_init, init_globals); struct light_device_t *dev = malloc(sizeof(struct light_device_t)); memset(dev, 0, sizeof(*dev)); dev->common.tag = HARDWARE_DEVICE_TAG; dev->common.version = 0; dev->common.module = (struct hw_module_t*)module; dev->common.close = (int (*)(struct hw_device_t*))close_lights; dev->set_light = set_light; *device = (struct hw_device_t*)dev; return 0;}static struct hw_module_methods_t lights_module_methods = { .open = open_lights,};static int __init leds_init(void){ leds_class = class_create(THIS_MODULE, "leds"); if (IS_ERR(leds_class)) return PTR_ERR(leds_class); leds_class->suspend = led_suspend; leds_class->resume = led_resume; return 0;}static ssize_t led_brightness_show(struct device *dev, struct device_attribute *attr, char *buf){ struct led_classdev *led_cdev = dev_get_drvdata(dev); /* no lock needed for this */ led_update_brightness(led_cdev); return sprintf(buf, "%u\n", led_cdev->brightness);}static ssize_t led_brightness_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size){ struct led_classdev *led_cdev = dev_get_drvdata(dev); ssize_t ret = -EINVAL; char *after; unsigned long state = simple_strtoul(buf, &after, 10); size_t count = after - buf; if (*after && isspace(*after)) count++; if (count == size) { ret = count; if (state == LED_OFF) led_trigger_remove(led_cdev); led_set_brightness(led_cdev, state); } return ret;}static DEVICE_ATTR(brightness, 0644, led_brightness_show, led_brightness_store);/** * led_classdev_register - register a new object of led_classdev class. * @parent: The device to register. * @led_cdev: the led_classdev structure for this device. */int led_classdev_register(struct device *parent, struct led_classdev *led_cdev){ int rc; led_cdev->dev = device_create(leds_class, parent, 0, led_cdev, "%s", led_cdev->name); if (IS_ERR(led_cdev->dev)) return PTR_ERR(led_cdev->dev); /* register the attributes */ rc = device_create_file(led_cdev->dev, &dev_attr_brightness); if (rc) goto err_out;#ifdef CONFIG_LEDS_TRIGGERS init_rwsem(&led_cdev->trigger_lock);#endif /* add to the list of leds */ down_write(&leds_list_lock); list_add_tail(&led_cdev->node, &leds_list); up_write(&leds_list_lock); led_update_brightness(led_cdev);#ifdef CONFIG_LEDS_TRIGGERS rc = device_create_file(led_cdev->dev, &dev_attr_trigger); if (rc) goto err_out_led_list; led_trigger_set_default(led_cdev);#endif printk(KERN_INFO "Registered led device: %s\n", led_cdev->name); return 0;#ifdef CONFIG_LEDS_TRIGGERSerr_out_led_list: device_remove_file(led_cdev->dev, &dev_attr_brightness); list_del(&led_cdev->node);#endiferr_out: device_unregister(led_cdev->dev); return rc;}EXPORT_SYMBOL_GPL(led_classdev_register);static int lcd_backlight_registered;static void msm_fb_set_bl_brightness(struct led_classdev *led_cdev, enum led_brightness value){ struct msm_fb_data_type *mfd = dev_get_drvdata(led_cdev->dev->parent); int bl_lvl; if (value > MAX_BACKLIGHT_BRIGHTNESS) value = MAX_BACKLIGHT_BRIGHTNESS; /* This maps Android backlight level 0 to 255 into driver backlight level 0 to bl_max with rounding */ bl_lvl = (2 * value * mfd->panel_info.bl_max + MAX_BACKLIGHT_BRIGHTNESS) /(2 * MAX_BACKLIGHT_BRIGHTNESS); if (!bl_lvl && value) bl_lvl = 1; msm_fb_set_backlight(mfd, bl_lvl, 1);}static struct led_classdev backlight_led = { .name = "lcd-backlight", .brightness = MAX_BACKLIGHT_BRIGHTNESS, .brightness_set = msm_fb_set_bl_brightness,}; if (!lcd_backlight_registered) { if (led_classdev_register(&pdev->dev, &backlight_led)) printk(KERN_ERR "led_classdev_register failed\n"); else lcd_backlight_registered = 1; }#define MAX_KEYPAD_BL_LEVEL 16static void msm_keypad_bl_led_set(struct led_classdev *led_cdev, enum led_brightness value){ int ret; ret = pmic_set_led_intensity(LED_KEYPAD, value / MAX_KEYPAD_BL_LEVEL); if (ret) dev_err(led_cdev->dev, "can't set keypad backlight\n");}static struct led_classdev msm_kp_bl_led = { .name = "keyboard-backlight", .brightness_set = msm_keypad_bl_led_set, .brightness = LED_OFF,};static int msm_pmic_led_probe(struct platform_device *pdev){ int rc; rc = led_classdev_register(&pdev->dev, &msm_kp_bl_led); if (rc) { dev_err(&pdev->dev, "unable to register led class driver\n"); return rc; } msm_keypad_bl_led_set(&msm_kp_bl_led, LED_OFF); return rc;}static int __devexit msm_pmic_led_remove(struct platform_device *pdev){ led_classdev_unregister(&msm_kp_bl_led); return 0;}#ifdef CONFIG_PMstatic int msm_pmic_led_suspend(struct platform_device *dev, pm_message_t state){ led_classdev_suspend(&msm_kp_bl_led); return 0;}static int msm_pmic_led_resume(struct platform_device *dev){ led_classdev_resume(&msm_kp_bl_led); return 0;}#else#define msm_pmic_led_suspend NULL#define msm_pmic_led_resume NULL#endifstatic struct platform_driver msm_pmic_led_driver = { .probe = msm_pmic_led_probe, .remove = __devexit_p(msm_pmic_led_remove), .suspend = msm_pmic_led_suspend, .resume = msm_pmic_led_resume, .driver = { .name = "pmic-leds", .owner = THIS_MODULE, },};static int __init msm_pmic_led_init(void){ return platform_driver_register(&msm_pmic_led_driver);}module_init(msm_pmic_led_init);static void __exit msm_pmic_led_exit(void){ platform_driver_unregister(&msm_pmic_led_driver);}module_exit(msm_pmic_led_exit);