2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <hardware_legacy/led.h>
17 #include <sys/types.h>
25 #include <utils/Log.h>
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";
36 write_string(const char* file, const char* string, int len)
41 fd = open(file, O_RDWR);
43 LOGE("open failed errno: %d\n", errno);
47 amt = write(fd, string, len);
49 LOGE("write failed errno: %d\n", errno);
53 return amt >= 0 ? 0 : errno;
56 int trout_set_led_state(unsigned colorARGB, int onMS, int offMS)
60 int alpha, red, green, blue;
63 LOGV("set_led_state colorARGB=%08X, onMS=%d, offMS=%d\n", colorARGB, onMS, offMS);
65 // alpha of 0 or color of 0 means off
66 if ((colorARGB & 0xff000000) == 0 || (colorARGB & 0x00ffffff) == 0) {
71 red = (colorARGB >> 16) & 0xFF;
72 green = (colorARGB >> 8) & 0xFF;
73 blue = colorARGB & 0xFF;
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);
82 if (onMS > 0 && offMS > 0) {
83 int totalMS = onMS + offMS;
85 // the LED appears to blink about once per second if freq is 20
88 // pwm specifies the ratio of ON versus OFF
89 // pwm = 0 -> always off
90 // pwm = 255 => always on
91 pwm = (onMS * 255) / totalMS;
93 // the low 4 bits are ignored, so round up if necessary
94 if (pwm > 0 && pwm < 16)
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);
111 len = sprintf(buf, "%d", blink);
112 write_string(BLINK_ENABLE_FILE, buf, len);