These days, almost everyone has a cell phone; cell phones keep getting faster, smarter, and more capable, yet relatively few applications exist for them. The Hecl programming language makes it easy to script applications for your cell phone - with just a few lines of code, you can create applications that you can carry with you, everywhere.
I first fell in love with computers when my parents bought me a Commodore 64, a fairly nice computer for the time. Thanks to Moore's law, and the relentless pace of development, the average cell phone is now more powerful than that machine from just 20 some years ago. While it's understandable that many people just want to make phone calls, think of all the programs out there waiting to be written that take advantage of the fact that you almost always have a cell phone with you. I think I'm just beginning to scratch the surface of what's possible, especially as phones continue to get faster, and have better connections to the internet.
I became interested in writing cell phone applications several years ago, after a rainy day high in the Italian Dolomites near Cortina d'Ampezzo - my old phone ended up in a mud puddle and died, leading me to purchase a new phone with J2ME (Java) capabilities. Writing applications in Java was okay, but I thought to myself that it would be an interesting experiment to try and create a scripting language that runs on top of the J2ME (now known as Java Micro Edition or Java ME) environment.
When I created Hecl, I did so with several goals in mind:
Make it even easier and faster for experienced programmers to create cell phone applications.
Make it possible for novice programmers to create cell phone applications without the burden of dealing with Java.
Hecl has other benefits too - it's faster to develop applications, because you don't have to recompile after each change. In the hands of a clever programmer, it's also possible to do interesting things with Hecl because of its interpreted nature. You could start an application on your phone, and download additional bits of code off the web.
The aim of this tutorial is to help you create cell phone applications, so let's get started right away. You'll need a few things first:
Sun's Java. This is heading towards free software, but isn't quit there yet. If you run Ubuntu, like me, you can get Java with apt: apt-get install sun-java5-jdk, if you've added the "multiverse" repositories to your /etc/apt/sources.list file: deb
Sun's WTK toolkit. While you don't need the tools to compile Hecl (unless you want to hack on it!), you do want the emulator, so that you don't have to load your app onto your phone each time you want to test it. It's not open source software (yet?), but it does run on Linux, Mac and Windows, and of course it is free.
Hecl itself. You can get it from the Sourceforge download page.
Note
Hecl is always improving, so you should also consider checking out Hecl directly from subversion: svn co https://hecl.svn.sourceforge.net/svnroot/hecl/trunk/hecl hecl
Sun's WTK requires installation - you can put it somewhere like /opt, so it won't get mixed up with the rest of your system. The installation process is very simple - just say yes to a few questions, and you're done. Hecl doesn't require installation: everything you need is already there in the distribution.
To see if everything's working, you can try launching the emulator with the sample application: /opt/WTK2.5.1/bin/emulator -classpath build/j2me/final/cldc1.1-midp2.0/Hecl.jar Hecl
Hecl J2ME MIDP1.0 Commands
alert — Creates an alert
choicegroup — Displays a choicegroup in the current form
cmd — Adds a command to a form/listbox/textbox
datefield — Displays an datefield in the current form
exit — Exits the application
form — Creates a form
gauge — Displays an gauge in the current form
getindex — Fetches a reference to the N'th element in a given form/listbox
getprop — Fetches the value of a given property from a widget
listbox — Creates a listbox
noscreen — Runs without a current screen widget
screenappend — Appends an item to a form or listbox
setcurrent — Displays an alert/form/listbox/textbox
setindex — Sets the indexth element in a form/listbox to specified item
setprop — Sets a given property of a widget
string — Adds a string to the current form.
stringitem — Displays a stringitem in the current form
textbox — Creates a textbox
textfield — Displays a textfield in the current form
Note
Hecl has different GUI commands for the MIDP1.0 (older phones) and MIDP2.0 (newer). We are in the process of documenting the MIDP2.0 commands.
Commands available in the J2ME MIDP1.0 version of Hecl to interact with the phone. Look in the midp10/examples directory for examples of these commands and widgets in use.
Hecl Java ME MIDP2.0 Commands
lcdui.alert — Pops up an alert
lcdui.canvas — Creates a canvas
lcdui.choicegroup — Creates a group of potential choices that can be added to a form.
lcdui.command — Creates a command that can be attached to a screen
lcdui.date — Date/Time widget for lcdui forms
$eventcmd — Command/object describing a Canvas event.
lcdui.font — Font information and manipulation command.
lcdui.form — Creates a form that can contain various widgets
lcdui.gauge — Creates a gauge widget that can be attached to a form.
lcdui.image — Creates an image.
lcdui.imageitem — An item that can contain an image, in order to attach it to a form.
lcdui.list — Creates a full-screen list
lcdui.settings — Returns or sets information about the graphical environment.
lcdui.spacer — Creates a spacer element in a form.
lcdui.stringitem — Creates a string item that can be added to a form.
lcdui.textbox — Creates a text box for editing larger blocks of text.
lcdui.textfield — Creates a small text editing widget that can be attached to a form.
lcdui.ticker — Creates a ticker that scrolls horizontally.
Hacking Hecl's Java ME code
Since Java ME comes in several flavors that Hecl can be compiled for, it's necessary to understand what Hecl does, and how it does it.
JavaME has two layers that are of interest to us, "CLDC" and "MIDP". CLDC is available in 1.0 and 1.1 configurations, whereas MIDP comes in 1.0 and 2.0 configurations. The most common configurations are CLDC 1.0 and MIDP 1.0, CLDC 1.0 and MIDP 2.0, and CLDC 1.1 with MIDP 2.0. Here are the Wikipedia entries describing CLDC and MIDP.
Hecl tries to match code to system resources: in other words, the code in the midp10/ and midp10gui (MIDP 1.0) directories is smaller, simpler, and has fewer features than the code in midp20 and midp20gui (MIDP 2.0), reflecting the fact that many 1.0 devices only allow very small jar files ("midlets").
For MIDP 1.0, the midp10gui directory contains the GUICmds.java, which has most of the functionality that maps J2ME functionality to Hecl and back. The midp10/Hecl.java file contains the code that starts up Hecl on the cell phone. For MIDP 2.0, the midp20gui directory contains the GUI commands, and midp20/Hecl.java is where the application is launched from on the phone.
In order to be able to deal with all these different versions, Hecl is more or less forced to utilize a Java preprocessor, which explains all the ifdef's in the code. The various symbols are defined in the settings.xml file.
To compile different combinations of things, Hecl makes a couple of property files available that are used like so:
ant -propertyfile ./cldc10midp10.properties midlet
Which compiles the CLDC 1.0 / MIDP 1.0 version of Hecl and places the jar in the jars/cldc1.0-midp1.0/ directory, or:
ant -propertyfile ./cldc11midp20.properties midlet
Which compiles the CLDC 1.1 / MIDP 2.0 version, and places the jar in the jars/cldc1.1-midp2.0/ directory.