Cool!
I just got my first programming running on an OLPC laptop. I say “programming” rather than “program”, because I merely copied and modified a “Hello World” activity. But if you can get a copy-and-modify to work, you’ve got the foundation, and the rest is details, right?
Here are some notes on how to get started quickly and painlessly on XO development. Note, this is the quick-and-dirty approach that lets you fiddle with live code and see the results right away. For significant projects, you probably want to set up a real development environment. But to me, lowering the entry barrier is essential to attracting more developers. There are a lot more people who will commit the time to learn a new system if they can wade in gradually, getting their toes wet and seeing immediate results from their small time investment. If you have to set up an emulated dev environment before you write your first line of code… and that process is not simple… the number of developers you’ll attract and the amount of code you’ll get contributed drops significantly.
Developers—especially the kind who volunteer their coding for open-source projects like OLPC —do it for the joys of the craft. We love to see a machine respond to our direction, turning our ideas into a tangible form. Seeing the XO respond to one’s changes is the hook that snags a developer and reels him in. Once he’s hooked, he will soon reach the point where he sees the value in setting up a development environment, and will be willing to jump through a few hoops to do so.
And yes, you will need to set up a real dev environment if you’re going to do any real development… e.g. if you’re going to keep your code in a shared repository, or make any frequent updates between the XO and the rest of the world (email, the web, or another development machine).
Prerequisites:
- A working XO laptop (try ebay if you don’t already have one).
- A basic familiarity with its user interface (“Sugar”)
- Familiarity with the GNU/Linux command line, and an editor such as vi(m) or nano
The easiest way to get started, for me, is copy-test-and-modify. Along the way I’m reading the Sugar Activity Tutorial, and you’ll want to look at it too, if I skip a detail that’s new to you. But for now, ignore the statement there that you first need to have installed a Sugar development environment.
(See also Building the XO: The Anatomy of an Activity for some helpful nuggets.)
Obtaining and installing the HelloWorld activity
Start by downloading the
HelloWorld bundle (or check the
Sugar Activity Tutorial for the latest link). To download the bundle on an XO, launch the Browse activity (click the wire globe icon in the dock), and type
http://divieira.googlepages.com/HelloWorld-1.xo in the address bar. (Alternatively, you can type
wiki.laptop.org and then search for HelloWorld.) Once the HelloWorld activity bundle has downloaded, click the “Open” button in the black download bar near the top of the browser screen. (You may have to click “Continue” first.)
If you can’t get your XO connected to the internet, you can download the HelloWorld bundle via another computer, and transfer it over using a USB flash drive. The file then shows up in the journal, where you can click on it, then click Resume to install.
The .xo file is a “essentially a zip file built from the MANIFEST with some extra metadata, like a JAR file” (Sugar Activity Tutorial). [Side note: I find that the .xo bundles are incompatible with some zip programs, like ZipGenius, but work pretty well with jar.] Installing the activity (following the steps described above) unzips the .xo file in your Activities folder, /home/olpc/Activities. There should now be a folder there called HelloWorld.activity. It contains the Python code and metadata for HelloWorld.
Launching HelloWorld
Now that HelloWorld is installed in /home/olpc/Activities, you can launch it. Go to the Home screen (to get here, push the key on the top row of the keyboard that has a solid circle with a single hollow dot in the middle). HelloWorld should show up in the dock at the bottom of the screen as a hollow square icon. If you hover the mouse over it, you get a “HelloWorld” tooltip. You might have to scroll the dock left or right by clicking its arrow buttons. If “HelloWorld” is not there, you might try restarting Sugar (Ctrl+Alt+Erase).
Click on the HelloWorld dock icon to launch it. You’ll see it appear, flashing, in the circle of current activities in mid-screen. Soon it will launch, and will show you its simple display: a huge button labeled “Hello World”, and some standard decorations across the top. We have ignition! Click the button, and the screen flashes.
Tip: you can use Ctrl+Q to exit the program.
Modifying HelloWorld
Now to get our hands dirty…
Return to the Home screen and launch the Terminal activity (a rectangle with a shell prompt, “$_”).
Tip: Use Alt+Tab to switch between running activities without returning to the Home screen.
Another tip: don’t launch activities prodigally. The machine is limited to 256MB of RAM. Treat it like a PDA rather than a laptop in this regard. (The activity circle on the Home screen is supposed to indicate the amount of memory used by each activity by the size of its on-screen sector. To me, though, they all look the same size.)
Once the terminal opens, cd to
/home/olpc/Activities and type
ls. You will see a list of user-installed activities. (The ones that come with the system are in /usr/share/activities.)
Type
cd HelloWorld.activity
and
ls again. (Remember Tab-completion is there—a welcome relief given the small keyboard.)
The Sugar Activity Tutorial has info about the various files in this directory structure, but the one that gives us immediate gratification is HelloWorldActivity.py. Edit this file with your favorite terminal-based Linux editor (
ed seems to be unavailable… sorry,
real programmers!). If you’re not familiar with vi(m), I suggest nano, because it has on-screen help.
nano HelloWorldActivity.py
Behold the Python code for HelloWorld, which is admirably simple and nicely commented. To see the most visible bang for your buck, I suggest finding the line that sets the button label:
self.button = gtk.Button("Hello World")
and changing the string to something unmistakable. I used
self.button = gtk.Button(u"Hello World Unicode IPA: u03B8u026Au014B")
because I’m working on an
IPA exploration activity (help wanted!). But
self.button = gtk.Button("Hello Jim")
will work just fine, especially if your name is Jim.
Save the file, and that’s it… no need to explicitly recompile. Make sure any running HelloWorld is stopped, and launch it again from the Home page dock. Your change should show up on the screen! It’s putty in your hands!
Where to go from here
If you know Python, you can keep hacking HelloWorld right on the XO and have lots of fun. But eventually you’ll want to
get a development environment going. You’ll want to change the name of the activity and rebuild the bundle. Happy hacking!
The point of this post, though, is that you can get a taste of XO programming without all that setup.
Note for when you get a little deeper into development: Unfortunately, in the downloadable Hello World, setup.py is out of date (with respect to bundlebuilder.start(), which now requires the Activity name as an argument). Use the version of setup.py on the activity tutorial page instead. But I don’t think you need that unless you are rebuilding the activity bundle.