Random Etc. Notes to self. Work, play, and the rest.

Posts Tagged ‘Programming’

Android Tinkering

I got an Android dev phone. Geeks talk.

I've heard it said that this would be the best phone in the world if it wasn't for the iPhone*. I can believe it. So this is the second best phone in the world, and you write software for it using Java and (optionally) Eclipse. I know Java and I use Eclipse all the time for writing Flash apps, so this is a tempting prospect: world class hardware, easy to use software. Let's go!

* We can debate what it means to be the best phone in the world at the moment - suffice to say that I know that hardware alone will never do it. Apple's retail experience, customer support, iTunes store, developer tools etc. all leave Android and others with a lot of work to do. But it is a nice phone, certainly. I also haven't been using the phone for voice, nor have I been syncing my emails and calendar with the phone... so this isn't a review by any stretch of the imagination.

There's a brand new update available for the Android OS, version 1.5 aka "Cupcake". My phone came with 1.1 and despite some prodding it wouldn't go ahead and upgrade itself. So I had to download the 1.5 updates and do it myself. That page is full of long and complicated explanations but basically you're just copying files, renaming them to update.zip and rebooting the phone, twice. (Mine got confused in the middle because it finally started to automatically update itself and I let it. If that happens just ignore it and continue with the manual process and everything should be fine.)

My main focus with tinkering with the phone has been to get the API demos running so I can get a sense for how easy it is to work with the Google MapView classes and also how much boilerplate code I need in order to load data over the network and draw pretty things with OpenGL. The Hello World tutorial worked straight away: if you have the phone plugged in it automatically installs your app and runs it on the device, if not then it fires up an emulator. Getting the API demos up and running was a little trickier because it involved importing the project from android-sdk-mac_x86-1.5_r1/platforms/android-1.5/samples/ApiDemos first, but it did work after I upgraded to Cupcake.

For the MapView to work you need to jump through some app signing hoops before you can get a Google Maps API key that will allow the device to load map tiles. The documentation is quite dense but if you're just playing around in Eclipse you can sign things with your debug key; in this case the API key signup page tells you what to do. Just be sure to log in with the same Google account you'll be using in the Android Marketplace, if you get that far.

Once I'd kicked the tyres with the demos I decided to jump straight in and try my hand at an app that loads data from a web service and displays it on a map. The learning curve was OK, here's a list of things I wish I'd known about before I started:

All in all it took me just over a day to get to the point where I felt confident that the phone was doing what I was telling it, and that there wasn't too much magic and surprise crashes were rare. The next thing I want to investigate is the OpenGL ES implementation, which I'm hoping is as slick as the iPhone's. I've been keeping a list of android links I think are worth reading at del.icio.us/TomC/android - let me know if there are any other neat/essential APIs in the Android universe that you think I should take a look at.

LÖVE

If you're the kind of (mainly 2d) graphics programmer that I am, the thing you find most attractive about Processing is the one-click publishing to make a webpage and show people what you've been doing. Everything else after that is a bonus.

If you're not that kind of programmer, and the web isn't your primary concern, then you should definitely check out LÖVE. It looks like they're having a lot of fun over there, and Lua is just nicely mind-bending enough but still familiar if you're coming from Java or Actionscript.

ArtNano (notes on Wordpress as a CMS)

I recently finished helping out on the back-end of the NISE Network's ArtNano site for San Francisco's Exploratorium. Working on a 'straight-up' website is a rare departure for us at Stamen, but we're big fans of the Exploratorium and were delighted at the opportunity to work with them.

ArtNano: New Approaches for Visualizing the Nanoscale

The ArtNano site features contributions from several artists tasked with exploring the nanoscale. My favourites are Semiconductor's gorgeously glitchy 200 Nanowebbers video, Scott Snibbe's multi-scale Three Drops installation and Santiago Ortiz's Time Spiral. Also featured are artworks by Eric Heller, Victoria Vesna and Stephanie Maxwell.

If you're interested in that sort of thing, read the full version of this post for some notes on using the Wordpress blog platform as a CMS for a small website project like ArtNano.

(more...)

Unintuitive E4X Gotcha in Actionscript 3

This is the second in a series of posts about actionscript 3 that I announced earlier, and my last for today. Here's the feed of posts tagged as3.

Testing for the existence of a property on an Object is simple in actionscript 3:

 
 
var object:Object = { sub: "sub" };
 
trace("testing object properties");
 
if (object.sub) {
	trace("CORRECT: object has a sub element");
}
else {
	trace("WRONG: object has no sub element");
}
 
if (object.nosub) {
	trace("WRONG: object has a nosub element");
}
else {
	trace("CORRECT: object has no nosub element");
}
 

This yields the correct output:

testing object properties
CORRECT: object has a sub element
CORRECT: object has no nosub element

However, the same thing isn't as intuitive with XML and E4X:

 
 
var xml:XML = <xml><sub>sub</sub></xml>;
 
trace("testing xml element");
 
// don't do this, it will give false positives!
if (xml.sub) {
	trace("CORRECT: xml has a sub element");
}
else {
	trace("WRONG: xml has no sub element");
}
 
// don't do this, it will give false positives!
if (xml.nosub) {
	trace("WRONG: xml has a nosub element");
}
else {
	trace("CORRECT: xml has no nosub element");
}
 

This gives incorrect output:

testing xml element
CORRECT: xml has a sub element
WRONG: xml has a nosub element

The way to test reliably for the existence of xml elements is to check the length() method on the element:

 
 
trace("testing xml length()");
 
// this works to test for the existence of an element
if (xml.sub.length()) {
	trace("CORRECT: xml has a sub element");
}
else {
	trace("WRONG: xml has no sub element");
}
 
// this also works to test for the non-existence of an element
if (xml.nosub.length()) {
	trace("WRONG: xml has a nosub element");
}
else {
	trace("CORRECT: xml has no nosub element");
}
 

This yields the correct output:

testing xml length()
CORRECT: xml has a sub element
CORRECT: xml has no nosub element

Hey, it's boring but true!

Optimising Actionscript 3

This is the first in a series of posts about actionscript 3 that I announced earlier. Here's the feed of posts tagged as3.

Even with the great speed improvements that actionscript 3 and Flash 9 offer over actionscript 2 and Flash 8, I've occasionally had the need to revisit a project and look for places to optimise.

Miscellaneous Tips

Here's a list of suggestions I recently made to Gabe, who has his own actionscripting notes, as he was revisiting an old project looking to reduce CPU usage.

Tweaking the framerate

Simply dropping the framerate of an application might work as far as CPU usage goes too.

(Be careful with this though - if you're watching exclusively watching the CPU meters, and not the app, you might be optimising for the wrong audience!)

You can do that with the SWF metadata tag above your main app class in Flex Builder. The main bit is [SWF(frameRate='15')] but check the help file as you might have background colours and width/height in there already.

Memoization

You probably won't need it, but the trick used in Digg Arc (for calculating arc points before redrawing them) was to use what's called memoization — it helps with space-time trade-offs.

The idea is to remember the results of calling a function with certain arguments and only recalculate when the arguments change.

e.g. You might have a function that you're calling it a lot with the same values e.g. calculatePoints(1,2,3,4) that looks a bit like this:

 
 
function calculatePoints(a,b,c,d):Array
{
  var points:Array = [];
  /*
    lots of heavy math with a,b,c,d and points
  */
  return points;
}
 

Instead of calculating every possible variation, you can create a hash of the common results:

 
private var results:Object = {};
 
function calculatePoints(a,b,c,d):Array
{
  var key:String = [a, b, c, d].join();
  var points:Array = results[key] as Array;
  if (points) {
    return points;
  }
  else {
    points = [];
  }
  /*
    lots of heavy math with a,b,c,d and points
  */
  results[key] = points;
  return points;
}
 

Then if calculatePoints is called again with 1,2,3,4 you grab the answer from your results Object and return that instead of calculating it again. You probably want to set a limit on the number of things you cache though, so don't use this technique without thinking about that, and only consider this if your performance bottleneck is definitely one maths-heavy function!

Caveat Optimisor!

The only way to be sure about optimisations is to time things, manually or in your development environment, and bear in mind that even to experienced coders some performance issues can be unintuitive.

A quick (less certain) note on using Lucene in Processing

In the spirit of continuing our impromptu database week on Processing Blogs (my post, toxi's post, then Florian Jennet updating the sql library), I thought I'd post another quick example using Lucene.

Last week Ryan and I needed a reliable way to search inside a data set we were working with. I had previously tried and failed to write my own useful search routine for the same data, so I wanted to take a look at Lucene instead. (This week, I might have used SQLite, but I hadn't tried it last week!).

Lucene isn't a relational database like MySQL or SQLite, although it has a few similarities with the way most database engines speed up queries using indexes. That's because Lucene is "just" the indexing part. You tell Lucene about your data, one part at a time, and then construct queries to ask it which parts of your data match the query. The key thing is that you keep hold of your data yourself and structure it any way you like, Lucene keeps its own representation of the data for searching. Because of this it can index much more data than you can store in memory.

Anyway, I put up an simple example Lucene applet here that indexes the text of Time Machine by H.G. Wells from Project Gutenberg and lets you type queries against it. The text itself and the Lucene index it builds are both quite small (tens of KB, compressed), but the applet is around 750KB. This is because Lucene's core jar file is about 500KB so it's more suited to standalone projects and applications.

The code is kind of documented, but Lucene was really too much for me to understand properly in just one day. Nevertheless, again, I hope people find it useful!

A Quick Note on using SQLite in Processing

SQLite is a great standalone SQL database engine - not ideal for every situation (particularly large websites), but more than good enough to have already made its way into desktop projects from Apple, and more recently Adobe and Google.

My colleague Mike recently used it as a way to distribute data from a 511.org transit website scraping project he's been doing. I wanted to see if I could download his data (gathered and processed using python) and access it using Processing.

Web searches for SQLite and Java (java wrapper sqlite, etc.) turn up lots of matches, including this promising tutorial from Tim Anderson, but sadly the most prominent matches didn't work for me. It's a real pain to get up and running and installed in a reliable way, mainly due to the need to compile SQLite natively for each platform and then talk to the native code using Java.

Enter this amazing project, a JDBC library for SQLite written in pure java that uses NestedVM to compile the C code for SQLite into something any Java VM can use. It's frightening to think about the amount of misdirection, abstraction and interpretation going on here, but it worked first time and plenty fast enough for my purposes.

Take a look after the jump for the code I ended up with. I hope people find it useful:

(more...)

Processing 0121 is ready

A new version of Processing is available now.

If you haven't looked at Processing for a while, it's definitely worth picking up again.  It's maturing very nicely - this week I've been using it at work, and the ability to create a single app with full-screen OpenGL graphics, PDF and movie output (thanks to Dan Shiffman's movie maker library) is absolutely great.

AI library for Processing

Over on the Processing.org forum, Aaron Steed is putting together some of the AI code he's written into a library, starting with examples of A* pathfinding, genetic algorithms and neural networks.

I did my own implementation of A* based on the same tutorial recently. I've put it online here since our coding styles are quite different and it might be useful to have an alternative example to work with.

Big Balls of Mud

In an ongoing discussion with Ben Gimpert on teaching with Processing, I note:

"I do have something to say about methodologies and Processing, I'm just not sure what it is. There's probably something profound about the Big Ball of Mud methodology and the Japanese 'craze' for hikaru dorodango."

← Before