Create a simple application with Hecl

Introducing Hecl, a mobile phone scripting language

Hacker's code | Easy

By David Welton

Online on: recently published

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.

Easy cell phone applications with Hecl

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:

  1. Make it even easier and faster for experienced programmers to create cell phone applications.
  2. 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 http://us.archive.ubuntu.com/ubuntu/ feisty multiverse
  • 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 free software (yet?), but it does run on Linux, Mac and Windows. You can download the WTK for free.
  • Hecl itself. You can get it from the Sourceforge download page.

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

That should bring up something like this:

Figure 1: Hecl demo screen shot
Figure 1: Hecl demo screen shot

This is Hecl’s built in demo—its source code is located in midp20/script.hcl, but before I get too far ahead of myself, let’s go back and create the classic “Hello World” application, just to get started and see how to work with Hecl.

Note: Hecl actually comes in several flavors, with slightly different GUI commands—MIDP1.0 (older phones), which has fewer commands and doesn’t do as much, and MIDP2.0, for newer phones, which has a lot more features. This tutorial utilizes the MIDP2.0 commands, because that’s what current phones are based on. The concepts described are very similar for the MIDP1.0 commands, but the commands are slightly different. Please contact me if you are interested in a MIDP1.0 version of this tutorial.

The “Hello World” cell phone application

To write your first Hecl program, open a text editor, and type the following program into a file—I’ll call it hello.hcl:

proc HelloEvents {cmd form} {
    [lcdui.alert -text "Hellllllllooooo, world!" -timeout forever] setcurrent
}

set form [lcdui.form -title "Hello world" -commandaction HelloEvents]
set cmd [lcdui.command -label "Hello" -longlabel "Hello Command" -type screen]

$form setcurrent
$form addcommand $cmd

$form append [lcdui.stringitem -label "Hello" -text "World"]

Not bad—8 lines of code, and most of it’s pretty clear just from looking at it. I’ll go through it line by line, so you understand exactly what’s happening.

Tags: hecl, java, mobile

License

(C) David Welton 2007

Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved.

Biography

David Welton: David N. Welton lives in Innsbruck, Austria, with his wife Ilenia, after a number of years of living in Padova, Italy. His personal web site is here: welton.it and his business web site is here: dedasys.com. He has been involved with the Debian project since 1997, the Apache Software Foundation since 2001, and generally loves working with open source software.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options


[The Top 10 Everything] [Zoeshire] [FSDaily RSS] [Book Reviews - Illiterarty]