Debugging Android wirelessly

Recently I flashed my Motorola Xoom with Android 3.2 in order to start playing with USB Host API and Open Accessory API, but both functionalities are implemented on the unique USB port that the Xoom has. This is not a problem when you own a Dock, or you own a Acer A500 which has one USB host and one USB device. Debugging an Android application using abd can be done wirelessly over WiFi using the followings instructions.

When debugging applications that use USB accessory or host features, you most likely will have USB hardware connected to your Android-powered device. This will prevent you from having an adb connection to the Android-powered device via USB. You can still access adb over a network connection. To enable adb over a network connection:

  1. Connect the Android-powered device via USB to your computer.
  2. From your SDK platform-tools/ directory, enter adb tcpip 5555 at the command prompt.
  3. Enter adb connect <device-ip-address>:5555 You should now be connected to the Android-powered device and can issue the usual adb commands like adb logcat.
  4. To set your device to listen on USB, enter adb usb.

 

UC3B @ High Speed

From time to time I forget how to set the UC3B CPU to run at full speed. The EVK1101 comes with a 12MHz crystal, and I use the same crystal for my board because it is a nice number that let me easily reach the 48Mhz needed for driving the USB port correctly. So I will not change it, but I will try to get the most of the CPU.
The UC3B comes with the following oscilators:

  • 2 PLL [80MHz – 240MHz]
  • Crystal Osc. [0.4MHz – 20MHz] (but I will let it fixed at 12MHz)
  • Crystal Osc. [32KHz] (The RTC clock)
  • RC Osc. [115KHz] (May be useful for low power consumption apps, but not this case.)

The Power Management unit handles all clock generators. A close look at the part of the unit responsible for driving CPU, HSB, PBA and PBB clocks is shown here:

So if we want to run our UC3B as fast as cars do in “The Fast & The Furious” (but without Nitro), we have to use PLL. The rules for setting PLL propertly are:

  1. Crystal must be between 0.4MHz and 16MHz
  2. PLL multiplication factor goes from 3 to 15
  3. PLL frequency must go from 80MHz to 240MHz

PLL output frecuency is the result of  ((Crystal frequency/ OSC_DIVIDE) *  PLL_MUL_FACTOR). As the datasheet states the maximum frequency for the UC3B is 60MHz. So the following should configuration can be applied in order to get 60MHz for CPU and 30MHz for PBA (USB will use PBB and PLL1 in order to get 48Mhz from the 12Mhz source)

Crystal Osc: 12MHz
PLL multiplication factor: 9
PLL divisor: 1
PLL0 = (12MHZ / 1) * (9+1) = 120MHz (and we are safe at this frequency)
PLL0 output divided by two = 1 (yes)
CPU frequency = PLL0 output /2 = 60MHz
PBA frequency = 30MHz (Pheriperial Bus A clock)

 
In order to get this configuration we have to choices. We can do it manually, or we can tell ASF (AVR Software Framework) to set the registers based on our needs.
The old way:

And the simplest way of configuring the frequency we want is by doing the following.

 
 
 
 
 
 
 
Of course, both ways do the same thing. But this lastest one, lets the ASF do the maths and the configuration for you.
It is important not to forget to set the CPU frequency for NewLib and to initialize the USB clock after setting the main clock source.
 

webOS template project + Python + PyGame = Happy Developers

So I have created a sample C project for webOS 2 that bundles all your python files into an ipk (so you can distribute this app using the App Catalog) and also launched the Python interpreter when you click on it!
The only condition is that you have to copy all the Python interpreter + WebOS API + PyGame into /media/internal. You can do this just following this simple steps
1] Download this compressed files that contains: Python, PyGame and WebOS API to your computer
Python + PyGame for WebOS (Binaries)
2] Unpack and copy “python” directory to the root directory of your SD Card (must previously mount the phone as Mass Storage Device)
3] Download PDL-Python Template
PDL Template Project
4] Modify, add source code…. IMPORTANT: modify app name, package name
5] Copy the binary to “release”
6] Copy all python source code to “release” (or just work in that directory
7] Create a package with palm-package
8] Install it with palm-install
Have fun!
 

A picture is worth a thousand words…

Running Python from a SSH session on the Pre2

The python program…

 
 
 
 
 
 
 
Running a simple Python program on the Mac

Running the same simple Python program on the Pre2

The C code that launches the Python script (this code was embedded into a ipk file with the Python code)

 

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.
 

First contact with AVR Studio 5 (beta)

After years of being the lastest (and only) official IDE for AVR microcontrollers, Atmel decided to launch a new version of the famous AVR Studio. Of course between this AVR Studio 5 release (which is still beta) and AVR Studio 4, Atmel tried a new IDE for AVR32 based on Eclipse.
From my point of view the AVR32 Studio based on Eclipse was the best decision Atmel did. It brought something new all the developers that love and use AVR products. The fact is that having an IDE based on Eclipse (which is based on Java) let users run the IDE in several platforms and not only Windows (as AVR Studio 4 did). Moreover, Atmel delivered an AVR32 Toolchain for Windows and Linux, and the lastest one was easily ported to OS X.
The new version of AVR Studio (5) is based on…. Visual Studio! Yes… it can only runs on Windows (don’t try Wine, it’s like trying to drive a new BMW X5 with an old FIAT 600 engine, you will not go very far). But, let’s judge it by  how it works, and not how it was constructed.

As you might image when listening that it was based on Visual Studio, the ASM/C/C++ compiler is integrated with the instaler. And also the software framework, even though this last one is only available for AVR32 and the XMega series. There is no ASF for the 8bit family (yet).

Once the project is created, everything is very similar to AVR Studio 4. May be this is one of the reason why Atmel choose Visual Studio instead of Eclipse for the new IDE.

At a glance there are a few interesting things that were necessary to have. For example, you can import an AVR Studio 4 project into this new IDE.

As it happens with AVR32 Studio (based on Eclipse) you have a few predefined code snippets, and can add yours.

The project properties panel is more complete, you can choose different microcontrollers, and configure the toolchain in the same way you do it with Eclipse (for AVR32)

To sum up, AVR Studio 5 it’s a better IDE than Studio 4, it’s not perfect or portable to other platforms, but it gives me a good impression.

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.

First webOS (web) App

So finally I have started to play with some programming on the Palm Pre 2. It is interesting to find that web applications runs as smooth as native onces in this device. What is more interesting is that, like Android, you can program in Windows, Linux and Mac. It is nice to know you have official tools that runs in all platforms, and you don’t have to stick with XCode or Visual Studio.
Apart from web applications, Palm’s (now HP) webOS let’s you program in C/C++ with native SDL and OpenGL 1 and 2 support. Most of low level hardware of the device can be access using standard linux procedure to open a stream and write or read. This is particularly interested because because it changes the mobile world a bit in two ways. You don’t have to root your phone (like in Android OS) or jailbreak it (like in iPhone) to access low level functions. webOS is built over a standard Linux, so if you want to take Python sources, compile it for ARM and put the library inside your C/C++ project (something I’m planning to do in the few months) YOU CAN, and it’s legal in the Application Catalog!
The second good thing about having such a strong native part in the platform is not only that web applications can make use of low level functions written in C or C++, But also that in average a C/C++ application with SDL or OpenGL can be ported to webOS in 5 hours. I tried making a simple C++ program, and the same program that runs on my Mac, runs in the Palm Pre with only two lines of code modified. So even though te platform is oriented to a high level development based on HTML, CSS and JavaScript, there is a string background that can be used without limitations by programmers used to deal with low-level software (like me).
The lastest SDK, emulators and documentation of webOS can be found at Palm’s Developer Site. These tools are completely free and you only have to sign-up to get them, no need of 99U$D per year. Just download the tools for your platform, read the tutorials, understand what MOJO is about, and go on! It also has a development tool called ARES, which is a full feature IDE in the web, so may be you can even program in an iPad using Safari.
This first and very simple program is a web app that converts decimal to hex and binary and viceversa.  It is very simple and I bet in runs in a browser with a small modifications to the HTML, and a few lines in Javascript.
Creating an (empty) application
Let’s run Eclipse with Palm SDK plug-in and create a basic application.

Once created, you will see a project structure like this:
webOS development is MVC oriented. Inside “app” you will find views, models and assistants. Views are HTML files that represents a scene (in webOS applications have stages/cards and scenes, learn more about The Anatomy of a webOS App). An assistant is a Javascript file that contains the controller. As you may guess, some applications also have a model folder, but this is not necessary to build an application. The basic stuffs are Views and Assistants, and the plug-in provides a Wizzard for that.
A basic (and empty) scene can be created by clicking on the Palm icon, and selecting. I will name this scene “home”

The “home” scene recently created adds two files to the project. “home-asssiant.js” (aka.  a Controller for the Home scene) and “home-scene.html” (the view). So now the project looks like this

from now on, everything is HTML CSS and Javascript. You will see a few more screenshots with HTML and Javascript code from this app. But the most important part is the deploying of the application into the device.

From a terminal window (or command line) , place in a directory before the project directory and type

palm-package <name of the project>

This will generate a “ipk” file which is the application packaged. Then in order to install the package into the device, just type

palm-install <ipk file>

This will produce the output you will see in the following screenshot

Now, a few screenshots that shows interesting parts of the view and the controller. There is not much to say about this, it’s just HTML and the MOJO framework that webOS uses in Javascript is very well documented on Palm’s website.

Here you will see the setup method of the assistant, widgets should be initialized here, and you can add some validations methods inside this setup. Like, for example, chars allowed in a TextField.

In the setup method you can also declare event handlers for the widgets, as I do in this piece of code

A function that handles one of these events, it’s like any other javascript function. In this case, it takes the value from a TextField, makes some transformations, and update the value of others TextFields.

The view of this project it’s quite simple as you can see in this screenshot. Everything is HTML, every widget is identified by id, and you will use that id with the MOJO framework to customize the widget and manage it. The most relevant part if that you define a x-mojo-element property that tells the framework which type of object it is.

The final app looks like this without modifying the stylesheet or applying any custom style.

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