More on Leds

From WebOS Internals
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

As mentioned in Controlling LEDs from the Shell, there are some sysfs endpoints for controlling the LEDs. For a small example of a native program twiddling the LEDs using these endpoints, check out:

http://www.jasonlebrun.info/files/leds_demo.gz

Just copy the program to your Palm Pre, and then:

gunzip leds_demo.gz
chmod u+x leds_demo
./leds_demo


The LEDS are controlled by a low power special-purpose LED driver IC, the LP8501. It's controlled by the i2c bus. It allows preset programs to do things like fade-ins and fade-outs without requiring CPU intervention. I'm investigating it more, stay tuned.

Since the source is short, I'll post it right here:

#include "stdio.h"
#include "math.h"

int main(int argc, char** argv) {

    int i = 0, val = 0, phase1=120, phase2=240, s=1000;
    FILE *left_led, *right_led, *center_led; 
    char buf[10];
    float PI = 3.14159;

    if(argc < 4) {
        puts("Usage: leds_demo delay(us) led2_offset led3_offset");
        puts("Phase is in degrees around a circle. 120, 240 for perfect spacing");
        puts("I.E.: leds_demo 1000 120 240");
        puts("This example will be run now");
    } else {
        s = atoi(argv[1]);
        phase1 = atoi(argv[2]);
        phase2 = atoi(argv[3]);
    }
    left_led =  fopen("/sys/class/leds/core_navi_left/brightness", "w");
    right_led =  fopen("/sys/class/leds/core_navi_right/brightness", "w");
    center_led =  fopen("/sys/class/leds/core_navi_center/brightness", "w");
    while(1) {
        val = sin(i*2*PI/360.0)*32+32;
        sprintf(buf, "%d\n", val); 
        fwrite(buf, strlen(buf), 1, left_led);
        rewind(left_led);

        val = sin((i+phase1)*2*PI/360.0)*32+32;
        sprintf(buf, "%d\n", val); 
        fwrite(buf, strlen(buf), 1, center_led);
        rewind(right_led);
        
        val = sin((i+phase2)*2*PI/360.0)*32+32;
        sprintf(buf, "%d\n", val); 
        fwrite(buf, strlen(buf), 1, right_led);
        rewind(center_led);
        
        i++;
        usleep(s);
    }


}