OSDN Git Service

auto import from //branches/cupcake/...@137197
[android-x86/hardware-libhardware_legacy.git] / led / led_trout.c
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <hardware_legacy/led.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <stdio.h>
23
24 #define LOG_TAG "LED"
25 #include <utils/Log.h>
26
27 const char* const AMBER_BRIGHTNESS_FILE = "/sys/class/leds/amber/brightness";
28 const char* const RED_BRIGHTNESS_FILE = "/sys/class/leds/red/brightness";
29 const char* const GREEN_BRIGHTNESS_FILE = "/sys/class/leds/green/brightness";
30 const char* const BLUE_BRIGHTNESS_FILE = "/sys/class/leds/blue/brightness";
31 const char* const BLINK_ENABLE_FILE = "/sys/class/leds/red/device/blink";
32 const char* const BLINK_FREQ_FILE = "/sys/class/leds/red/device/grpfreq";
33 const char* const BLINK_PWM_FILE = "/sys/class/leds/red/device/grppwm";
34
35 static int
36 write_string(const char* file, const char* string, int len)
37 {
38     int fd;
39     ssize_t amt;
40
41     fd = open(file, O_RDWR);
42     if (fd < 0) {
43         LOGE("open failed errno: %d\n", errno);
44         return errno;
45     }
46
47     amt = write(fd, string, len);
48     if (amt < 0) {
49         LOGE("write failed errno: %d\n", errno);
50     }
51
52     close(fd);
53     return amt >= 0 ? 0 : errno;
54 }
55
56 int trout_set_led_state(unsigned colorARGB, int onMS, int offMS)
57 {
58     int len;
59     char buf[30];
60     int alpha, red, green, blue;
61     int blink, freq, pwm;
62     
63     LOGV("set_led_state colorARGB=%08X, onMS=%d, offMS=%d\n", colorARGB, onMS, offMS);
64     
65     // alpha of 0 or color of 0 means off
66     if ((colorARGB & 0xff000000) == 0 || (colorARGB & 0x00ffffff) == 0) {
67         onMS = 0;
68         offMS = 0;
69     }
70
71     red = (colorARGB >> 16) & 0xFF;
72     green = (colorARGB >> 8) & 0xFF;
73     blue = colorARGB & 0xFF;
74
75     len = sprintf(buf, "%d", red);
76     write_string(RED_BRIGHTNESS_FILE, buf, len);
77     len = sprintf(buf, "%d", green);
78     write_string(GREEN_BRIGHTNESS_FILE, buf, len);
79     len = sprintf(buf, "%d", blue);
80     write_string(BLUE_BRIGHTNESS_FILE, buf, len);
81
82     if (onMS > 0 && offMS > 0) {        
83         int totalMS = onMS + offMS;
84         
85         // the LED appears to blink about once per second if freq is 20
86         // 1000ms / 20 = 50
87         freq = totalMS / 50;
88         // pwm specifies the ratio of ON versus OFF
89         // pwm = 0 -> always off
90         // pwm = 255 => always on
91         pwm = (onMS * 255) / totalMS;
92         
93         // the low 4 bits are ignored, so round up if necessary
94         if (pwm > 0 && pwm < 16)
95             pwm = 16;
96
97         blink = 1;
98     } else {
99         blink = 0;
100         freq = 0;
101         pwm = 0;
102     }
103     
104     if (blink) {
105         len = sprintf(buf, "%d", freq);
106         write_string(BLINK_FREQ_FILE, buf, len);
107         len = sprintf(buf, "%d", pwm);
108         write_string(BLINK_PWM_FILE, buf, len);
109     }
110
111     len = sprintf(buf, "%d", blink);
112     write_string(BLINK_ENABLE_FILE, buf, len);
113
114     return 0;
115 }