OSDN Git Service

b6a508a924a70e72e598039bda1b7d19c5b90847
[android-x86/hardware-libhardware_legacy.git] / led / led_sardine.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 const char* const CADENCE_FILE = "/sys/class/leds/left/cadence";
25 const char* const COLOR_FILE = "/sys/class/leds/left/color";
26 const char* const BT_WIFI_FILE = "/sys/class/leds/right/brightness";
27
28 static int
29 write_string(const char* file, const char* string, int len)
30 {
31     int fd;
32     ssize_t amt;
33
34     fd = open(file, O_RDWR);
35     if (fd < 0) {
36         return errno;
37     }
38
39     amt = write(fd, string, len+1);
40
41     close(fd);
42     return amt >= 0 ? 0 : errno;
43 }
44
45 int sardine_set_led_state(unsigned colorARGB, int onMS, int offMS)
46 {
47     int len;
48     char buf[30];
49     int red, green, color;
50
51     // alpha of 0 or color of 0 (ignoring blue) means off
52     if ((colorARGB & 0xff000000) == 0 || (colorARGB & 0x00ffff00) == 0) {
53         onMS = 0;
54         offMS = 0;
55     }
56
57     // blue means green
58     if ((colorARGB & 0x00ffff00) == 0 && (colorARGB & 0x000000ff) != 0) {
59         colorARGB |= (colorARGB & 0x000000ff) << 8;
60     }
61
62     // ===== color =====
63     // this color conversion isn't really correct, but it's better to
64     // have the light come bright on when asked for low intensity than to
65     // not come on at all. we have no blue
66     red = (colorARGB & 0x00ff0000) ? 1 : 0;
67     green = (colorARGB & 0x0000ff00) ? 2 : 0;
68     color = (red | green) - 1;
69
70     len = sprintf(buf, "%d", color);
71     write_string(COLOR_FILE, buf, len);
72
73     // ===== color =====
74     len = sprintf(buf, "%d,%d", onMS, offMS);
75     write_string(CADENCE_FILE, buf, len);
76
77     return 0;
78 }