11.A2 Flashing the LEDs using Java


Java Implementation

You need to have installed Java on the Beaglebone Black before you try this. Please follow the steps at this Running Java Applications on the Beaglebone Black or watch the following YouTube video:


Now that we have this working at the command line it would be useful if we could write programs that interface to the LEDs in a similar fashion. My Java implementation code is below. It uses the Java.io  libraries to read and write data to the input/outputs using sysfs.

BasicLEDExample.java

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
package ee402;
 
import java.io.*;
 
public class BasicLEDExample {
 
    private static String LED0_PATH = "/sys/class/leds/beaglebone:green:usr0";
    
    public static void main(String[] args) {
        
        if(args.length!=1) {
            System.out.println("Incorrect Usage - use:\n\t BasicLEDExample 
                    On \n\t BasicLEDExample Off");
            System.exit(0);
        }
        try{
            if (args[0].equalsIgnoreCase("On") || 
                            args[0].equalsIgnoreCase("Off")){
                BufferedWriter bw = new BufferedWriter ( 
                        new FileWriter (LED0_PATH+"/trigger"));
                bw.write("none");
                bw.close();
                bw = new BufferedWriter ( 
                        new FileWriter (LED0_PATH+"/brightness"));
                bw.write(args[0].equalsIgnoreCase("On")? "1":"0");
                bw.close();
            }
            else {
                System.out.println("Invalid command");
            }
        }
        catch(IOException e){
            System.out.println("Failed to access the Beaglebone LEDs");
        }
    }
 
}

Effectively, this code carries out the exact same steps as what I did in the first part of this post; however, now it is wrapped up as a command that we can add to our $PATH in order that it can be called from any location. Build this code on your desktop computer or use the class file that I have distributed in the bin directory. The way you execute this code on the Beaglebone Black is to go to the directory below ee402 and type:

1
2
java ee402.BasicLEDExample On
java ee402.BasicLEDExample Off

I have made all of the code available via github, so you can download this by the following steps:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@beaglebone:~/temp# git clone git://github.com/derekmolloy/ee402
Cloning into ee402...
remote: Counting objects: 52, done.
remote: Compressing objects: 100% (43/43), done.
remote: Total 52 (delta 19), reused 35 (delta 8)
Receiving objects: 100% (52/52), 33.71 KiB, done.
Resolving deltas: 100% (19/19), done.
root@beaglebone:~/temp# ls
ee402
root@beaglebone:~/temp# cd ee402/
root@beaglebone:~/temp/ee402# ls
LEDcpp  LICENSE  README.md  scripts  testcpp  testjava
root@beaglebone:~/temp/ee402# cd LEDjava

The way we can interact with GPIOs is not much different than this; however, we have complexities to deal with to do with the Device Tree, and we have to use Device Tree Overlays. See my post: GPIOs on the Beaglebone Black using the Device Tree Overlays on this topic.

These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
Comments