blob: a53729fc60933022880cd98fd27f8353d1f0386e [file] [log] [blame]
The Android Open Source Projectd6054a32008-10-21 07:00:00 -07001#include <hardware/led.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <errno.h>
7#include <stdio.h>
8
9const char* const CADENCE_FILE = "/sys/class/leds/left/cadence";
10const char* const COLOR_FILE = "/sys/class/leds/left/color";
11const char* const BT_WIFI_FILE = "/sys/class/leds/right/brightness";
12
13static int
14write_string(const char* file, const char* string, int len)
15{
16 int fd;
17 ssize_t amt;
18
19 fd = open(file, O_RDWR);
20 if (fd < 0) {
21 return errno;
22 }
23
24 amt = write(fd, string, len+1);
25
26 close(fd);
27 return amt >= 0 ? 0 : errno;
28}
29
30int set_led_state(unsigned colorARGB, int onMS, int offMS)
31{
32 int len;
33 char buf[30];
34 int red, green, color;
35
36 // alpha of 0 or color of 0 (ignoring blue) means off
37 if ((colorARGB & 0xff000000) == 0 || (colorARGB & 0x00ffff00) == 0) {
38 onMS = 0;
39 offMS = 0;
40 }
41
42 // blue means green
43 if ((colorARGB & 0x00ffff00) == 0 && (colorARGB & 0x000000ff) != 0) {
44 colorARGB |= (colorARGB & 0x000000ff) << 8;
45 }
46
47 // ===== color =====
48 // this color conversion isn't really correct, but it's better to
49 // have the light come bright on when asked for low intensity than to
50 // not come on at all. we have no blue
51 red = (colorARGB & 0x00ff0000) ? 1 : 0;
52 green = (colorARGB & 0x0000ff00) ? 2 : 0;
53 color = (red | green) - 1;
54
55 len = sprintf(buf, "%d", color);
56 write_string(COLOR_FILE, buf, len);
57
58 // ===== color =====
59 len = sprintf(buf, "%d,%d", onMS, offMS);
60 write_string(CADENCE_FILE, buf, len);
61
62 return 0;
63}