In my last post I shared how to get a working version of leJOS running on your wifi-enabled EV3.  Now we need to make it do something, so in this tutorial I will cover the basic tools for compiling, uploading and executing leJOS Java programs on the EV3.

Get the leJOS source

Andy has uploaded the current working source code for leJOS into a Github repository. The repository lives at http://sourceforge.net/p/lejos/ev3/ci/master/tree/. You can browse the repository in your web browser to view individual files, but if you want to work with the examples and build new code it is best to use the git command to check out a local copy of the repo:

git clone git://git.code.sf.net/p/lejos/ev3 lejos-ev3

 

Alternatively you can download a zip file of the current snapshot from Github by clicking on the Download Snapshot link at the top of the frame:

Download snapshot

 

When you unpack the snapshot tarball, or run the git clone command, you’ll end up with the leJOS EV3 class files and sample programs in a directory named lejos-ev3.

Basic Tools

To compile Java programs for the EV3 you will need…Java! The version that is installed on my Mac is Java 1.6:

$ java -version
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)

 

The beauty of Java is that it compiles code to bytecode, and that bytecode can run on almost any platform. It is easy to compile your Java code on one platform (a Mac, Linux or Windows box) and then run it on another (the EV3). Download the latest Java JDK for your platform from the Oracle website.

To get data to and from the EV3 you will need an ssh program. The Linux version running on the EV3 runs a copy of the Dropbear ssh server. Dropbear is optimised to run on embedded platforms, but supports all of the standard ssh protocol commands to login into the terminal and copy files. ssh is installed by default on Mac OS and Linux systems. For Windows you will need to download the amazing PuTTY terminal emulator that provides ssh support.

Let’s log into our EV3 and get to a command prompt (doing this for the first time is the most exciting bit!):

root@EV3

There is no password set for root, so just hit enter when prompted for the password at login. As you can see the EV3 is running a Linux ARM build.

Compiling HelloWorld

Let’s turn our attention back to our host PC where we previously downloaded the Github image of the EV3 leJOS source. We will start by compiling the classic HelloWorld program that Andy has provided and then we will upload the resulting class file to the EV3 where we can run it.

Change to the directory lejos-ev3 containing the source code and let’s look at the EV3HelloWorld.java file:

src $ pwd
/Users/mcrosbie/lejos-ev3/EV3HelloWorld/src
src $ cat EV3HelloWorld.java
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import lejos.nxt.LCD;
import lejos.nxt.Button;
import lejos.nxt.Sound;
import lejos.util.Delay;
public class EV3HelloWorld
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println("Running...");
        final int SW = LCD.SCREEN_WIDTH;
        final int SH = LCD.SCREEN_HEIGHT;
        Button.LEDPattern(4);
        Sound.beepSequenceUp();
        Graphics g = new Graphics();
        g.setFont(Font.getLargeFont());
        g.drawString("leJOS/EV3", SW/2, SH/2, Graphics.BASELINE|Graphics.HCENTER);
        Button.LEDPattern(3);
        Delay.msDelay(4000);
        Button.LEDPattern(5);
        g.clear();
        LCD.refresh();
        Sound.beepSequence();
        Delay.msDelay(500);
        Button.LEDPattern(0);

    }

}

 

Next we will compile this with Java. The key step here is to provide Java a pointer to the ev3classes.jar file which contain the EV3-specific leJOS code:

javac

As you can see we now have a EV3HelloWorld.class file ready to upload to the EV3.

Uploading to the EV3

To copy our class file onto the EV3 we use the scp command provided by the ssh toolset:

src $ scp EV3HelloWorld.class root@192.168.2.13:/lejos/samples
Coming soon: how to set up an NFS server on your EV3...

Running the Java class file on the EV3

To run the Java file on the EV3 we use the jrun command that Andy has provided:

root@EV3:/lejos/samples# jrun EV3HelloWorld

jrun

After a short delay to start the Java interpreter your EV3 should make a series of beeps upwards, display leJOS/EV3 on the screen and then a sequence of beeps downwards. Success!

Stay tuned for more tutorials on programming the EV3 in leJOS…