Python and PyGame meet WebOS =)

The PalmPDK includes a toolchain of GCC in order to build C and C++ applications, and Python is a nice one. The idea behind this is to allow webOS to run Python apps, but what is more important for my is PyGame. If Python is able to run in webOS and PyGame works without much modifications, then a lot of game can be ported to webOS, and very nice applications can be done.
I wrote this idea in the Palm WebOS C/C++ a long time ago, and after a few tries without success, Thomas Perl got it working with the help of the WebOS Internals PDK http://thp.io/2011/webos/) He did a great job there, and also he is making a library for binding the Plug-In API using SWIG.
My idea now that this is working, and it works nice as you can see in Thomas’ website, is to create a “Python environment” application and a “template application”. The Python environment application will just check if you have the lastest binaries of Python + PyGame inside your SD Card (The complete Python and PyGame lib is nearly 27MBytes). If not, it will download it, and install in on the SD Card.
Once this is done, then it comes the time for the “template application”. This will be a standard SDL project, that will check if you have the “Python environment”. If not it will redirect you to the App. Catalog in order to install it. But if you have it, it will launch “main.py”, a file bundled in this project from where you can start writing your own python application.
The idea behind this is to make Python a popular language for creating games and application for WebOS. May be in a future even it’s bundled by default in WebOS and this will turn into a nice experience but useless. Until that happens, this solution seems to be very good.
 

webOS low level programming – basic app

For Windows and Mac OS, Palm provides a low level SDK called Plug-in Development Kit (PDK) which allows you to develop C and C++ native applications or plug-ins that can be called from a web app. In the OS X version of the PDK, a plug-in is installed in XCode (yes, you can use another IDE, but the installer gives a beautiful wizard for Xcode). So let’s create a simple application!

Inside Xcode, go to ‘File’ and click on ‘New Project’ (or Shift + Command + N). You will see a new template called SDL Application, and that is what we are going to create.

This action will create a project with a simple structure that includes some necessary files in order to run the application on your Mac. Yes, you can (fully) develop an SDL application in your Mac (or PC) and then use nearly the same code inside the Palm Pre. What is interesting about this project wizard is that includes all the header files needed, and a script file to build the project for the device.

I will add ‘-lpdl’ to ‘Other Linker Flags’which will let me do some specific calls to the OS. This has to be handle will care. Due to the fact that this functions are only available in webOS, you should place some define to avoid executing and compiling that code when building the app for the desktop simulator.

Let’s create an empty main.cpp file with it’s header file, and the start building the application step by step

Main.h includes SDL header file and also some Palm’s headers.  What is more important, it will have definitions that will help running the app in the Mac and in the device properly.

Main.cpp  has functions to initialize the screen, setup the device and handles events from the user interface.

The most important part of this tutorial comes now. How it looks like in the emulator? and how to build and deploy this application into the device?

For running this application on the emulator, comment the RUNNING_ON_DEVICE definition and press ‘Build and Run’ or ‘Build and Debug’ in Xcode. You will only see something a blank screen of 320×480 pixels as this application doesn’t do something more instersting.

Running inside the Palm Pre requires going to the terminal and editing a few script files. The wizard generates a file called ‘buildit_for_device.sh’ that gives us a hand with the build process but doesn’t help too much in the deployment process. This file must be editing in order to know where is the source code and how the output file is called. It will looks similar to this

As you may noticed, if you run this script, you will get a binary file called ‘simple’ app. Eventhough you can copy this file to the device and run the code, the recommended way is to generate a package and install that package using palm tools. So the next step involves packaging the application. As in the web application a description file is requiered. I created a STAGING directory and inside that directory created a file called ‘appinfo.json’ with the following content.

As this is a native application it is very important to make our binary fila as executable, using ‘echo filemode.755=simpleapp > package.properties’.

Once this files are created, we can call palm-package with STAGING as argument, and the install the package using palm-install.

This action installs the application and places an icon in the launcher. Clicking the icon runs the full screen application.

a Caller ID that speaks (for Android)

So I have just arrived home and while preparing a tuna fish sandwich somebody calls home and the answer machine started to say the number. This is something very normal as I had this answer machine for 5 years more or less. The first days it was amazing to be 20 meters away from the phone and listen the phone number of the person who was calling me. It is very nice, it prevents me and my family from running to the phone every time somebody calls.
So I was thinking about doing the same for a cellphone, and I am sure there are a few apps out there that does this. But I wanted to do it in a few minutes and post the code.
I choose Android as the platform as I think it is flexible enough to archive this task. This cannot be done in iPhone with such a limited API and no access to key features of the phone.
Basically you need to register a Listener which will handle “Phone State Changes”. If one of those events indicates a “Phone ringing” you have to parse the number digit by digit and play an audio file.
The audio you will find in this example is my voice, and the numbers are in German (just to make this a bit funnier, and to practice, of course).
This application does not have an activity. It has only two classes, a Broadcast Receiver and a Service. The Broadcast Receiver it will lunch the Service as soon as the phone finished booting. The Service will register a Listener to the Telephony Manager, and that Listener will handle the incoming call.
How to register the Listener for Incoming Calls

How to handle an Incoming Call event

The complete project
vCallerID

Bluetooth in pre-Android 2.0 devices

Android OS introduced Bluetooth as an open API in version 2.0, but early versions like the well known Donut (1.6) and, one of the most famous in Motorola devices, Cupcake (1.5) included Bluetooth support in the OS for hands free communications, files exchange and communication with PCss (and Macs).
Of all projects around the web that enable Bluetooth in pre-2.0 devices, I liked android-bluetooth (http://code.google.com/p/android-bluetooth/) very much. Basically because it was design for compatibility with 2.x releases of Android. This was archived by keeping an API as similar as the official one but changing the package name of the classes (Android does not allow 3rd parties classes inside android package).

It is very important to check if the Android devices allows Bluetooth comms, either because the module is not present or because it is not available. For doing so, just use this piece of code:

If everything goes right, you have to register for broadcast messages like “discovering devices”, “discovering finished”, “did finish connection with device” and so on. That is very easy to do in Android

and then, it is necessary to implement the Broadcast interface

Some other configurations are needed by this framework, including a configuration file and registering receivers and activities in the Android Manifest. For a complete working example check out my BTExample project.
BTExample