summaryrefslogtreecommitdiffstats
path: root/led/led_sardine.c
blob: b6a508a924a70e72e598039bda1b7d19c5b90847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <hardware_legacy/led.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

const char* const CADENCE_FILE = "/sys/class/leds/left/cadence";
const char* const COLOR_FILE = "/sys/class/leds/left/color";
const char* const BT_WIFI_FILE = "/sys/class/leds/right/brightness";

static int
write_string(const char* file, const char* string, int len)
{
    int fd;
    ssize_t amt;

    fd = open(file, O_RDWR);
    if (fd < 0) {
        return errno;
    }

    amt = write(fd, string, len+1);

    close(fd);
    return amt >= 0 ? 0 : errno;
}

int sardine_set_led_state(unsigned colorARGB, int onMS, int offMS)
{
    int len;
    char buf[30];
    int red, green, color;

    // alpha of 0 or color of 0 (ignoring blue) means off
    if ((colorARGB & 0xff000000) == 0 || (colorARGB & 0x00ffff00) == 0) {
        onMS = 0;
        offMS = 0;
    }

    // blue means green
    if ((colorARGB & 0x00ffff00) == 0 && (colorARGB & 0x000000ff) != 0) {
        colorARGB |= (colorARGB & 0x000000ff) << 8;
    }

    // ===== color =====
    // this color conversion isn't really correct, but it's better to
    // have the light come bright on when asked for low intensity than to
    // not come on at all. we have no blue
    red = (colorARGB & 0x00ff0000) ? 1 : 0;
    green = (colorARGB & 0x0000ff00) ? 2 : 0;
    color = (red | green) - 1;

    len = sprintf(buf, "%d", color);
    write_string(COLOR_FILE, buf, len);

    // ===== color =====
    len = sprintf(buf, "%d,%d", onMS, offMS);
    write_string(CADENCE_FILE, buf, len);

    return 0;
}