8.3 Android Lighting System _ Write HAL_LIGHTS.C

Source: Internet
Author: User
Tags strcmp

  

Note the properties of the file created by led_class_attrs[] defined in LED-CLASSES.C should be changed to 0666, otherwise the application does not have permission to manipulate it

At the same time ledtrig-time.c inside corresponding to the new delay_on/delay_off also to change to 0666, so as to write it

Java:frameworks/base/services/core/java/com/android/server/lights/lightsservice.java
Jni:frameworks/base/services/core/jni/com_android_server_lights_lightsservice.cpp
HAL:LIGHTS.C//Refer to the LIGHTS.C found on Google, our development is only Hal's so library, in Vendor/friendly-arm/tiny4412/proprietary/lights_ Tiny4412.so

Default color: Frameworks/base/core/res/res/values/config.xml
Battery Light: Frameworks/base/services/core/java/com/android/server/batteryservice.java
Notification Light: Frameworks/base/services/core/java/com/android/server/notification/notificationmanagerservice.java

How to write Lights HAL
A. Implementing a hw_module_t structure called the HMI
B. Implement an open function that returns a light_device_t struct based on name
C. Implement multiple light_device_t structures, each corresponding to a device
The 1th member in the light_device_t structure is the hw_device_t structure, followed by a set_light function



To set the LED status:
struct light_state_t {
/**
* The color of the LED in ARGB.
*
* Do your best here.
*-If your light can is only does red or green, if they ask for blue,
* You are should do green.
*-If you can only do a brightness ramp and then use this formula:
* unsigned char brightness = ((77* (color>>16) &0x00ff)
* + (150* (color>>8) &0x00ff) + (29* (COLOR&0X00FF))) >> 8;
*-If You can have only does on or off, 0 are off, anything else is on.
*
* The high byte should is ignored. Callers would set it to 0xFF (which
* would correspond to 255 alpha).
*/
unsigned int color; Set the color of the lamp, or set the brightness of the LCD why

/**
* See the Light_flash_* constants
*/
int Flashmode; Whether flashing, light_flash_none means no flash
int flashonms; The time of the light
int flashoffms;//time to kill

/**
* Policy used by the framework to manage the light ' s brightness.
* Currently the values are Brightness_mode_user and brightness_mode_sensor.
*/
int Brightnessmode; Indicates the backlight brightness mode of the LCD
};

LIGHTS.C file

Note the properties of the file created by led_class_attrs[] defined in LED-CLASSES.C should be changed to 0666, otherwise the application does not have permission to manipulate it

#define LOG_TAG "Lights"

#define LOG_NDEBUG 0//Plus this sentence can be printed with ALOGV, otherwise the printing information is ignored

#include

#include

...... There are many more header files ...

Hal inside the operation LED is to write the values in these files to complete

char const*const red_led_file = "sys/class/leds/led1/brightness";

char const*const green_led_file = "sys/class/leds/led2/brightness";

char const*const blue_led_file = "sys/class/leds/led3/brightness";

char const*const Red_led_file_trigger = "Sys/class/leds/led1/trigger";

char const*const Green_led_file_trigger = "Sys/class/leds/led2/trigger";

char const*const Blue_led_file_trigger = "Sys/class/leds/led3/trigger";

char const*const Red_led_file_delayon = "sys/class/leds/led1/delay_on";

char const*const Green_led_file_delayon = "sys/class/leds/led2/delay_on";

char const*const Blue_led_file_delayon = "sys/class/leds/led3/delay_on";

char const*const red_led_file_delayoff = "Sys/class/leds/led1/delay_off";

char const*const green_led_file_delayoff = "Sys/class/leds/led2/delay_off";

char const*const blue_led_file_delayoff = "Sys/class/leds/led3/delay_off";

char const*const lcd_backlight_file = "/dev/backlight-1wire";//Disassembly manufacturer provides a HAL layer so,lights_tiny4412.so that can be seen on the backlight operation is the file, Search for "Backlight-1wire" in the kernel and discover that it is a device registered in the corresponding drivers/input/touchscreen/tiny4412_1wire_host.c, which is a miscdevice that provides file_ Operation Structural Body

static pthread_one_t g_init = Pthread_once_init;

static pthread_mutex_t G_lock = Pthread_mutex_initializer;

Notification light and battery light common one hardware

static struct light_state_t g_notification;

static struct light_state_t g_battery;

static int write_int (const char *path,int value) {

int FD;

static int already_warned = 0;

FD = open (PATH,O_RDWR);

if (fd<0)

{

if (already_warned = = 0)

{

already_warned = 1;

}

Return-errno;

}

Char buffer[20];

int bytes = snprintf (buffer,sizeof (buffer), "%s\n", value);

int written = write (fd,buffer,bytes);

Close (FD);

return written = = -1?-errno:0;

}

static int write_string (const char *path,const char *value) {

int FD;

static int already_warned = 0;

FD = open (PATH,O_RDWR);

if (fd<0)

{

if (already_warned = = 0)

{

already_warned = 1;

}

Return-errno;

}

Char buffer[20];

int bytes = snprintf (buffer,sizeof (buffer), "%s\n", value);

int written = write (fd,buffer,bytes);

Close (FD);

return written = = -1?-errno:0;

}

static int is_lit (struct light_state_t const * State) {

return State->color & 0x00ffffff;

}

static int rgb_to_brightness (struct light_state_t const * State) {

int color = State->color & 0x00ffffff;

Return ((77* ((color>>16) &0x00ff) + (150* ((color>>8) &0x00ff)) + (29* (COLOR&0X00FF))) > >

}

static int set_light_backlight (struct light_device_t *dev,,struct light_state_t *state) {

int brightness = rgb_to_brightness (state);

Pthread_mutex_lock (&g_lock);

The range of brightness here is 0-255, but the device file can accept 0-127.

Write_int (LCD_BACKLIGHT_FILR,BRIGHTNESS/2);

Pthread_mutex_unlock (&g_lock);

return 0;

}

static void set_shared_light_locked (struct light_device_t *dev,struct light_state_t *state) {

int r,g,b;

int Delayon,delayoff;

R = (state->color>>16) &0xff;

g = (state->color>>8) &0xff;

b = (State->color) &0xff;

Delayon = state->flashonms;

Delayoff = state->flashoffms;

if (state->flashmode! = Light_flash_none) {

Write_string (Red_led_file_trigger, "TRIGGER");

Write_string (Green_led_file_trigger, "TRIGGER");

Write_string (Blue_led_file_trigger, "TRIGGER");

Write_int (Red_led_filr_delayon,delayon);

Write_int (Green_led_filr_delayon,delayon);

Write_int (Blue_led_filr_delayon,delayon);

Write_int (Red_led_filr_delayoff,delayoff);

Write_int (Green_led_filr_delayoff,delayoff);

Write_int (Blue_led_filr_delayoff,delayoff);

}else{

Write_string (Red_led_file_trigger, "none");

Write_string (Green_led_file_trigger, "none");

Write_string (Blue_led_file_trigger, "none");

    

}

Write_int (RED_LED_FILE,R);

Write_int (GREEN_LED_FILE,G);

Write_int (BLUE_LED_FILE,B)

}

static void handle_shared_battery_locked (struct light_device_t *dev) {

if (Is_lit (&g_notification)) {//Priority setting notification light

Set_shared_light_locked (dev,&g_notification);

}else{

Set_shared_light_locked (Dev,&g_battery);

}

}

static int set_light_battery (struct light_device_t *dev,struct light_state_t *state) {

Pthread_mutex_lock (&g_lock);

G_battery = *state;

handle_shared_battery_locked (Dev);

Pthread_mutex_unlock (&g_lock);

return 0;

}

static int set_light_notifications (struct light_device_t *dev,struct light_state_t *state) {

Pthread_mutex_lock (&g_lock);

G_notification = *state;

handle_shared_battery_locked (Dev);

Pthread_mutex_unlock (&g_lock);

return 0;

}

void Init_globals () {

Pthread_mutex_init (&g_lock,null);

}

static int close_lights (struct light_device_t* dev) {

}

static int open_lights (const struct hw_module_t *module,const char *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_battery,name))

Set_light = Set_light_battery;

}else if (0 = = strcmp (light_id_notifications,name))

Set_light = set_light_notifications;

}else{

Return-einval;

}

Pthread_once (&g_init,init_globals);

struct light_device_t *dev = malloc (sizeof (struct light_device_t), gpkernel);

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;

*device = &led_dev//led_dev's first member is hw_device_t, their address is the same

}

static struct hw_module_methods_t Lights_module_methods {

. open = Open_lights,

};

struct hw_module_t Hal_module_info_sym = {

. Tag = Hardware_module_tag,

. Version_major = 1,

. Version_minor = 0,

. id = lights_hardware_module_id,

. Name = "Snoy_lights_module",

. methods = Lights_module_methods;

}

Add Android.mk under LIGHTS.C

ANDROID.MK content is as follows:

Local_path: = $ (call My-dir)

Include $ (clear_vars)

Local_module: = lights.tiny4412;

Local_module_relative_path: =HW//The directory specified so exists under/SYSTEM/LIB/HW

Local_c_includes: = hardware/libhardware//header file

Local__src_files: = LIGHTS.C//source file

Local_shared_libraries: = Liblog

Local_module_tags: = Eng

Include $ (build_shared_library)

Upload HAL:LIGHTS.C

Hardware/libhardware/modules/lights/lights.c

Hardware/libhardware/modules/lights/android.mk

Modify:

VI Vendor FRIENDLY-ARM/TINY4412/DEVICE-TINY4412.MK

Comment out the inside of the device-tiny4412.mk (should be removed from the factory provided HAL)

Ifeq ($ (board_uses_pwmlights), false)

#PRODUCT_COPY_FILES +=\

# $ (Vendor_path)/proprietary/lights.tiny4412.so:system/lib/hw/lights.tiny4412.so

endif

Compile:

. setenv//Set Environment variables first

lunch//after execution select the Full_tiny4412_eng here eng corresponds to the android.mk inside the set of ENG

MMM Hardware/libhardware/modules/lights-b//forced compilation C library

Make Snod

./gen-img.sh

In contrast to the manufacturer's HAL and our own generated HAL, it will be found differently, and the command will ensure that the lights.c we provide have been programmed into system.img

Diff vendor/friendly-arm/tiny4412/proprietary/lights.tiny4412.so Out/target/product/tiny4412/system/lib/hw/ Lights.tiny4412.so

Update system.img and the Zimage obtained in the previous section

8.3 Android Lighting System _ Write HAL_LIGHTS.C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.