Hack49.Write Your Own Programs


Hack 49. Write Your Own Programs

Writing programs for your Palm is not as hard as you might think. You can write programs on the desktop and download them to your Palm device. You can also write small programs directly on your Palm device itself.

There are many different development environments for Palm OS devices. You can get Integrated Development Environments (IDEs) for your desktop machine which let you build programs to download to your Palm device. You can also get some interesting applications that let you develop programs (or scripts) directly on your Palm device. Of the development environments covered in this hack, the Palm OS Developer Suite, NS Basic, and Pocket Smalltalk are desktop environments. DragonForth and Pippy both run directly on a Palm device. Even if your development environment runs on a Palm, you can still write your programs at your desktop by running the Palm OS Emulator or Simulator. You can find additional development environments. Two good resources are the Palm OS developer site (http://www.palmsource.com) and an independent page that covers lots of resources at http://www.winikoff.net/palm/dev.html.

7.9.1. Palm OS Developer Suite

The newest development environment for Palm OS 5 and Palm OS 6 is Eclipse. The good news is that Eclipse is open source. The bad new is that Palm's customized version of Eclipse only runs on Windows XP. Eclipse itself is cross-platform, but the extensions for Palm development only run under Windows. Eclipse is an Integrated Development Environment (IDE) that supports various languages and platforms through plug-ins. A custom version of Eclipse is available from PalmSource called the Palm OS Developer Suite (http://www.palmos.com/dev/tools/dev_suite.html), which you can see in Figure 7-17. The first step in developing applications from the desktop is to download and install the developer suite (you'll need to register for a free account at that web site before you can download the suite). This will set you up for programming in C or C++.

Mac, Linux, and Unix users aren't left out in the cold, however. See http://www.palmos.com/dev/tools/gcc/ for information on PRC-Tools, a GCC-based development toolchain for non-Windows platforms.


Figure 7-17. Palm OS Developer Suite, no project open


The Palm Developer Suite supports several ways you can work with projects. It can fully manage the project, handling all of the dependencies and makefiles. The developer suite can also manage projects which use an external makefile.

A makefile is a file containing a collection of directives, dependencies, and commands. It is used by your development environment to compile your project from source code into its final form (an executable). In the simplest of all possible programs, where you have one source file and one executable, a makefile is not strictly necessary. However, real-world programs are often made up of several source files and in some cases, multiple projects (for example, if you are a game developer, you may have one project containing all the library code that is shared between all your games and a separate project for each game). In such cases, a makefile is essential. For more information on makefiles, and the program (make) that drives them, see Managing Projects with GNU make, Third Edition (O'Reilly, 2004).


The Palm operating system itself comes in two versions. Up through Palm OS 4.x, Palm devices ran on Motorola's 68K series of chips. With Palm OS 5.0, the 68K chips were replaced with an ARM processor. Palm OS 5 and 6 devices can run 68K programs in a special compatibility mode. Programs written specifically for the ARM processor will be much faster than those written for the 68K, however. When you create a new project, you will need to choose between a 68K application and an ARM application. If you want to support the widest range of Palm OS versions (e.g., Palm OS 4 or previous versions), then choose 68K. If you need the fastest possible application (and are willing to live with the Palm OS 5 or later restriction), choose ARM.

To get started quickly, choose New Project from the File menu in the developer suite. This menu item will let you choose an appropriate wizard depending on what kind of project you want to create. You can create an empty project and fill in all the details yourself. You can choose to create a simple application which provides the basic code to create a main form and an About box. You can also choose to create a simple puzzle game which provides a game screen, an instructions screen, and an About dialog box. If you choose to create the simple game, then you will get a screen that looks like Figure 7-18.

Figure 7-18. Sample project in the Palm OS Developer Suite


For now, choose the simple application. Step through the remaining steps to finish creating your first project. When the project has been created, then you can build and run it. Select Rebuild All from the Project menu. This will compile the program and create a .prc file. The compilation messages will appear in a pane in the lower part of the main window.

If the compilation completed successfully, then you are ready to run the application. It is better to test the application on the desktop before downloading it to your Palm device. Select Run… from the Run menu. This brings up a dialog box that lets you select a simulator to run on, as you can see in Figure 7-19. Select between a Palm OS 5 simulator [Hack #42] and a Palm OS 6 simulator. You can also choose between debug and release versions. The debug versions have all of the standard controls set to different background colors. The debug version also detects a number of errors, which can be helpful in tracking down problems in your code. The release version tries to recover from errors silently, behaving more like a real PDA.

Figure 7-19. Dialog for running an application from the developer suite


After you have chosen an appropriate simulator, you can run your application. You should be able to select it from the launcher, and you should be able to bring up the About box. Congratulations, you have just written your first Palm OS application.

Once you have gone through the basic process of building and launching a sample application you can start tweaking it to do something useful. The Palm Developer Suite has a list of the files in a pane on the left side of the main window. Click on the files to bring them into the main workspace. From there, you can edit the files to put in interesting behavior.

One file deserves special mention. The resource file (.rsc) contains all of the user interface controlsforms, buttons, edit controls, and so forth. Double-click on the resource file to bring up the resource editor (see Figure 7-20) which lets you graphically edit the resources.

Figure 7-20. Palm OS Developer Suite Resource Editor


7.9.1.1. Testing and debugging.

You can connect the debugger to the simulator. If your program hits any bugs, then you will be given a set of options, depending on the nature of the bug. The options include Ignore, Reset, Quit, and Debug. For non-fatal errors, you can choose to ignore the bug and let the program continue running. Sometimes this works and sometimes it doesn't. You can also reset the simulator which is the equivalent of doing a reset on a Palm device. You can quit the simulator or enter the debugger. Entering the debugger lets you use the Palm OS Developer Suite to view your program's source, set breakpoints, and inspect variables.

A useful technique for testing Palm applications is called Gremlins. A Gremlin is a random event. PalmSource recommends testing any Palm application for at least one million Gremlins before releasing it to the public. You set up Gremlins by right-clicking on the Palm Emulator and selecting the Gremlins menu item. You can choose to run one or more series of gremlins, as you can see in Figure 7-21. If you run more than one series at a time, then you get to choose how often to switch between the different series. You also get to choose how many events to generate and which applications to run.

7.9.1.2. Further information.

The developer suite provides online help for the Palm API for both Palm OS 5 and Palm OS 6. It also provides a general programming guide for the Palm operating system that discusses the Palm OS philosophy. The PalmSource web site (http://www.palmsource.com) has a knowledge base with articles, answers to questions, links to tools, and information on known bugs.

Figure 7-21. Gremlins panel for testing in the emulator


There are several good books which cover Palm OS programming. The most comprehensive book is The Palm OS Programming Bible, Second Edition by Lonnon Foster (Wiley 2002). Another interesting book is Palm OS Game Programming by Nicholas Pleis (Premier Press 2001).

7.9.1.3. Distributing your software.

If you write a program that you think might be of interest to other people, then you can look at different ways of distributing it. If you want to make it open source or distribute it for free, you can put it up on your own web site. Open source projects can also be put up on SourceForge (http://sourceforge.net). You can also put programs of any flavor (free, open source, or commercial) on the main Palm software sites like PalmGear (http://www.palmgear.com), Handago (http://www.handago.com), and PalmSource's directory (http://www.palmsource.com). You can also try to get links from the Open Directory (http://www.dmoz.org).

7.9.2. NS Basic

If you are more comfortable programming in Basic, then you could look at NS Basic (http://www.nsbasic.com/palm/). This is a desktop development environment for building programs which you can download to your Palm device. You can see a sample project in Figure 7-22.

Figure 7-22. NS Basic development environment


NS Basic seems a lot like a version of Visual Basic for Palm OS. There is a visual form designer for creating resources. You can double-click on an item in the form designer to add an event handler. NS Basic requires a runtime library on the Palm device. NS Basic only runs on Windows. NS Basic comes with the Palm OS Emulator and integrates nicely with it. You can launch the emulator from within the IDE and transfer your programs (and the runtime libraries) directly to the emulator or set up your program to be transferred to your Palm device at the next HotSync.

7.9.3. Pocket Smalltalk

Explore Smalltalk on your Palm device with Pocket Smalltalk (http://www.pocketsmalltalk.com). This is an open-source IDE for developing Smalltalk programs that run on your Palm device. You can see a sample project in Figure 7-23.

The latest version of Pocket Smalltalk only runs on Windows. Pocket Smalltalk itself is written in Smalltalk. There are two versions of Pocket Smalltalk, each of which uses a different underlying version of Smalltalk. The latest version of the IDE uses Dolphin Smalltalk, which only has a Windows version. There is an older version of the IDE available which runs under the Squeak version of Smalltalk. Squeak has been ported to many platforms including Windows, Linux, and Mac.

Figure 7-23. Part of the Pocket Smalltalk IDE showing the class and package browsers


7.9.4. DragonForth

If you want to play with Forth, DragonForth (http://sourceforge.net/projects/dragonforth/) is an implementation which runs on your Palm device, as you can see in Figure 7-24. You can build your own programs without needing to use your desktop environment.

Forth is an interesting language. It is stack based and works using Reverse Polish Notation, like most HP calculators. That means that operands (2 and 2 in the example) go on the stack first, and that the operators come at the end. Operators take the last items that were added to the stack and return their results to the stack.

Figure 7-24. DragonForth console


7.9.5. Pippy

Pippy (http://pippy.sourceforge.net) is an open-source implementation of Python 1.5 for Palm OS (see Figure 7-25). There are some limitationsnot everything in Python has been ported to try to keep the size down. Pippy was built and compiled under the Metrowerks compiler. If the project becomes active again, then a port to the Palm OS Developer Suite might be in order.

Figure 7-25. A simple Python expression in Pippy


I think that Python is a nice language for someone learning to program. The syntax is fairly simple, but there is a lot of power as well. There are some excellent tutorials on Pythoncheck out the main Python web site (http://www.python.org) for more information.

There are a couple of tips to make writing Python programs in Pippy easier.

All multi-line Python scripts, whether entered directly into Pippy or imported from a memo, must be terminated with a blank line (i.e., the cursor needs to be on a line by itself, following the last statement). Otherwise, you will get a syntax error on the last line of your script.

You can write scripts in MemoPad and import them into Pippy. Create a category named Python in MemoPad. The first line of each script needs to be something like #module.py. Run Pippy and execute the following commands:

 import memoimp memoimp.install() import module 

Then you should be able to use the module you wrote. To make life easier, you can create shortcuts [Hack #24]. Useful shortcuts include memoimp, memoimp.install( ), and the names of any modules that you are working on.

Another tip is to use single quotes instead of double quotes for strings. It is easy to confuse two single quotes in a row for a double quotethis can lead to hard-to-find bugs.




Palm and Treo Hacks
Palm and Treo Hacks: Tips & Tools for Mastering Your Handheld
ISBN: 059610054X
EAN: 2147483647
Year: 2006
Pages: 115

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net