Author Topic: Controlling the CubieTruck LEDs from an Android App  (Read 3063 times)

Offline BaronW

  • Newbie
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Controlling the CubieTruck LEDs from an Android App
« on: April 20, 2016, 08:10:43 pm »
I wanted to turn the CubieTruck's built-in LEDs on and off. I didn't find information on how this is done documented anywhere, and so after finding a way to do this on my own, I decided to give everyone here details on how it's done from within an Android app.

We use ADB for a lot of this. First, before doing anything with ADB, connect your CubieTruck to your PC via USB, and then after it boots up use this ADB command to remount the CubieTruck file system so that we can modify it:

adb remount

The LEDs can be accessed as files once we set their permissions as follows using ADB:

adb shell chmod 0666 /sys/class/leds/blue:pd25:led1/brightness
adb shell chmod 0666 /sys/class/leds/orange:pd26:led2/brightness
adb shell chmod 0666 /sys/class/leds/white:pd27:led3/brightness
adb shell chmod 0666 /sys/class/leds/green:pe04:led4/brightness


The blue LED is found in /sys/class/leds/blue:pd25:led1

We can turn the blue LED off by setting it's brightness to 0 using this action from ADB:

adb shell "echo 0 > /sys/class/leds/blue:pd25:led1/brightness"

We can turn the blue LED on by setting it's brightness to 1 using this action from ADB:

adb shell "echo 1 > /sys/class/leds/blue:pd25:led1/brightness"

Ok, so now how do we do this from within an Android app?

Here's a simple class that can do it. Just add this class to an Android app and from a button press in an Android Activity you can call CubieTruck.setLedBrightness (1,0) to turn off LED 1, which is the blue LED. Call CubieTruck.setLedBrightness (1,1) to turn on LED 1.

Code: [Select]
public class CubieTruck {
    static final String HARDWARE_LEDS = "/sys/class/leds/";
    static final String HARDWARE_LED1 = HARDWARE_LEDS + "blue:pd25:led1/";
    static final String HARDWARE_LED2 = HARDWARE_LEDS + "orange:pd26:led2/";
    static final String HARDWARE_LED3 = HARDWARE_LEDS + "white:pd27:led3/";
    static final String HARDWARE_LED4 = HARDWARE_LEDS + "green:pe04:led4/";

    public static void setLedBrightness ( int ledIndex, int brightness ) {
        switch ( ledIndex ) {
            case 1:setValue ( HARDWARE_LED1, "brightness", "" + brightness ); break;
            case 2: setValue ( HARDWARE_LED2, "brightness", "" + brightness ); break;
            case 3: setValue ( HARDWARE_LED3, "brightness", "" + brightness ); break;
            case 4: setValue ( HARDWARE_LED4, "brightness", "" + brightness ); break;
        }
    }

    public static void setValue ( String device, String property, String value ) {
        try {
            File root = new File(device, property);
            FileWriter writer = new FileWriter(root);
            writer.write(value);
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

One thing that you'll notice is that when you reboot, the code won't work anymore because the file permissions for the LEDs will be reset, banning our application from setting them. Why is this the default? It's a development board. Access should be granted by default, but it isn't.>:( But we can fix it.:)

To automatically fix the file permissions when the CubieTruck reboots we add our own custom script commands to this file on the CubieTruck:

/system/bin/insmod_dp.sh

I copied the file to my PC using this adb command:

adb pull /system/bin/insmod_dp.sh

The file contains this:

Code: [Select]
#! /system/bin/sh

insmod /system/vendor/modules/switch_dp.ko

Using a text editor (Notepad++), I made these modifications to that file to allow me to set the LED file permissions properly at boot time.

Code: [Select]
#! /system/bin/sh

# Added to allow access to the LEDs from an Android app
chmod 0666 /sys/class/leds/blue:pd25:led1/brightness
chmod 0666 /sys/class/leds/orange:pd26:led2/brightness
chmod 0666 /sys/class/leds/white:pd27:led3/brightness
chmod 0666 /sys/class/leds/green:pe04:led4/brightness

insmod /system/vendor/modules/switch_dp.ko

After editing the file and saving it, I used this command to push it back to the CubieTruck:

adb push insmod_dp.sh /system/bin/insmod_dp.sh

After that I use this command to set it's file permissions properly:

adb shell chmod 0755 /system/bin/insmod_dp.sh

Now if the CubieTruck reboots, the permissions are still the way we want them.

It's handy to have this insmod_dp.sh script available for us to modify. We can add a lot of OS customizations because of this.

Note that the method I'm using to control the LEDs from an Android application can also be used for a lot of other CubieTruck features. Take a look at the folder /sys/class on the CubieTruck and you'll see quite a bit of things we can control using this same method. One important one looks to be /sys/class/gpio. I will look into that later when I have more time.

Offline actkk2000

  • Hero Member
  • *****
  • Posts: 572
  • Karma: +2/-11
    • View Profile
Re: Controlling the CubieTruck LEDs from an Android App
« Reply #1 on: April 20, 2016, 09:59:48 pm »
Great work, thx! :)