Tutorials/Your First Egg (for uGFX badges)

From Badge team
Jump to navigation Jump to search


One of the aims of the badge.team project is to ensure that as many people as possible can develop software for the platform. This ensures that badges and other hardware running our firmware are more likely to have a life beyond the event for which they were created. The problem with many event badges has been that the learning curve for new developers is too steep and the acceptance process for new software too difficult. When detailed knowledge of a toolchain is required to write code and each addition must wait to be built into a fresh badge firmware update, most would-be developers go away and enjoy the event instead. With an online app store we refer to as the hatchery and MicroPython apps we refer to as eggs, publishing new software for badges running our firmware is a much simpler process.

Not everybody is immediately familiar with a new platform though, so to help with your first badge egg we've produced this tutorial. The aim here is not to teach you Python, but to introduce you to the structure of an extremely basic badge.team egg as well as get you going with the user interface. We'll be following the time-honoured tradition of introducing you to badge programming with a Hello World egg.


The software here has been tested on both the SHA2017 and Hacker Hotel 2019 badges.

Your First Egg


The first thing the firmware does when it tries to run an egg is look for a file named __init__.py, and run it. Some eggs also have other files, but at its simplest all the code for an egg can be contained within this one file.

So the first thing you should do is take an empty new directory, and create a file called __init__.py within it using your favourite text editor.


Your badge comes with a set of MicroPython libraries that contain functions allowing you to access its hardware. The first lines you must put in your __init__.py file simply tell MicroPython that your egg will need to use two of them. The badge library contains low-level badge-specific functions, and the 'ugfx library contains functions relating to the display and keys of the user interface. Type the following into your file:

import badge
import ugfx

Initialising the libraries

The two libraries must now be initialised, and to that end both have an init() function which must be called before they can be used. Now enter the following code into the next lines of your file:


Clearing the screen

Before writing to the screen, whatever is still in place from any previous software must be erased. The uGFX library has a clear() function to perform this task, which can be passed a colour as a predefined constant. In our case we want to clear the screen to white, so we are passing it the ugfx.WHITE constant. Add the following to a fresh line of your file: