NerdKit Gaming

Published Monday, February 14, 2011 by Bryan

Contrary to the evidence on this blog, not all of the code I write is in Erlang. It's not even all web-based or dealing with distributed systems. In fact, this week I spent my evenings writing C for an embedded device.

I've mentioned NerdKits here before (affiliate link). This week I finally dug into the kit I ordered so long ago, and took it somewhere: gaming.

The result is a clone of a simple tile-swap matching game. I used very little interesting hardware outside the microcontroller and LCD — mostly just a pile of buttons. The purpose of this experiment was to test the capabilities of the little ATmega168 (and my abilities to program it).

I've put the code on github, if you're interested in browsing. If you don't have a NerdKit of your own to load it up on, I've also made a short demo video, and snapped a few up-close screenshots:

What did I learn? Mostly I remembered that writing a bunch of code to operate on a small amount of data can be just as fun as writing a bunch of code to operate on a large amount of data. Lots of interaction with the same few bytes from different angles has a different feel than the same operation repeated time and time again on lots of different data. I also learned that I've been spoiled by interactive consoles and fast compile/reload times. When it takes a minute or more to restart (after power cycles and connector un-re-plugging) and I don't have an effectively infinite buffer to dump logs in, I think a little longer about each experiment.

So what's next? Well, not much for this game, unless I slim down the code some more. Right now it compiles to 14310 bytes. Shortly before this, it was 38 bytes larger, and refused to load onto the microcontroller properly, since it plus the bootloader exceeds the 16K of flash memory available. My first attack would probably be to simply move the game board to a global variable instead of passing it as a function argument. The savings in stack-pushing should gain a little room.

If I were to make room for new operations, then a feature that saved a bit of state across power cycles would be a fun target. What's a game without a high-score board?