The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | #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 | |
| 9 | #define LOG_TAG "LED" |
| 10 | #include <utils/Log.h> |
| 11 | |
| 12 | const char* const AMBER_BRIGHTNESS_FILE = "/sys/class/leds/amber/brightness"; |
| 13 | const char* const RED_BRIGHTNESS_FILE = "/sys/class/leds/red/brightness"; |
| 14 | const char* const GREEN_BRIGHTNESS_FILE = "/sys/class/leds/green/brightness"; |
| 15 | const char* const BLUE_BRIGHTNESS_FILE = "/sys/class/leds/blue/brightness"; |
| 16 | const char* const BLINK_ENABLE_FILE = "/sys/class/leds/red/device/blink"; |
| 17 | const char* const BLINK_FREQ_FILE = "/sys/class/leds/red/device/grpfreq"; |
| 18 | const char* const BLINK_PWM_FILE = "/sys/class/leds/red/device/grppwm"; |
| 19 | |
| 20 | static int |
| 21 | write_string(const char* file, const char* string, int len) |
| 22 | { |
| 23 | int fd; |
| 24 | ssize_t amt; |
| 25 | |
| 26 | fd = open(file, O_RDWR); |
| 27 | if (fd < 0) { |
| 28 | LOGE("open failed errno: %d\n", errno); |
| 29 | return errno; |
| 30 | } |
| 31 | |
| 32 | amt = write(fd, string, len); |
| 33 | if (amt < 0) { |
| 34 | LOGE("write failed errno: %d\n", errno); |
| 35 | } |
| 36 | |
| 37 | close(fd); |
| 38 | return amt >= 0 ? 0 : errno; |
| 39 | } |
| 40 | |
| 41 | int set_led_state(unsigned colorARGB, int onMS, int offMS) |
| 42 | { |
| 43 | int len; |
| 44 | char buf[30]; |
| 45 | int alpha, red, green, blue; |
| 46 | int blink, freq, pwm; |
| 47 | |
| 48 | LOGV("set_led_state colorARGB=%08X, onMS=%d, offMS=%d\n", colorARGB, onMS, offMS); |
| 49 | |
| 50 | // alpha of 0 or color of 0 means off |
| 51 | if ((colorARGB & 0xff000000) == 0 || (colorARGB & 0x00ffffff) == 0) { |
| 52 | onMS = 0; |
| 53 | offMS = 0; |
| 54 | } |
| 55 | |
| 56 | red = (colorARGB >> 16) & 0xFF; |
| 57 | green = (colorARGB >> 8) & 0xFF; |
| 58 | blue = colorARGB & 0xFF; |
| 59 | |
| 60 | len = sprintf(buf, "%d", red); |
| 61 | write_string(RED_BRIGHTNESS_FILE, buf, len); |
| 62 | len = sprintf(buf, "%d", green); |
| 63 | write_string(GREEN_BRIGHTNESS_FILE, buf, len); |
| 64 | len = sprintf(buf, "%d", blue); |
| 65 | write_string(BLUE_BRIGHTNESS_FILE, buf, len); |
| 66 | |
| 67 | if (onMS > 0 && offMS > 0) { |
| 68 | int totalMS = onMS + offMS; |
| 69 | |
| 70 | // the LED appears to blink about once per second if freq is 20 |
| 71 | // 1000ms / 20 = 50 |
| 72 | freq = totalMS / 50; |
| 73 | // pwm specifies the ratio of ON versus OFF |
| 74 | // pwm = 0 -> always off |
| 75 | // pwm = 255 => always on |
| 76 | pwm = (onMS * 255) / totalMS; |
| 77 | |
| 78 | // the low 4 bits are ignored, so round up if necessary |
| 79 | if (pwm > 0 && pwm < 16) |
| 80 | pwm = 16; |
| 81 | |
| 82 | blink = 1; |
| 83 | } else { |
| 84 | blink = 0; |
| 85 | freq = 0; |
| 86 | pwm = 0; |
| 87 | } |
| 88 | |
| 89 | if (blink) { |
| 90 | len = sprintf(buf, "%d", freq); |
| 91 | write_string(BLINK_FREQ_FILE, buf, len); |
| 92 | len = sprintf(buf, "%d", pwm); |
| 93 | write_string(BLINK_PWM_FILE, buf, len); |
| 94 | } |
| 95 | |
| 96 | len = sprintf(buf, "%d", blink); |
| 97 | write_string(BLINK_ENABLE_FILE, buf, len); |
| 98 | |
| 99 | return 0; |
| 100 | } |