Difference between revisions of "Tutorials/Your First Egg (for uGFX badges)"

From Badge team
Jump to navigation Jump to search
Line 81: Line 81:
 
<pre>
 
<pre>
 
import badge
 
import badge
import ugfx</pre>
+
import ugfx</pre><pre style="color:red">
<span style="color:red">import appglue</pre>
+
import appglue</pre><pre>
<span>
 
 
badge.init()
 
badge.init()
 
ugfx.init()
 
ugfx.init()
</pre>
+
</pre><pre style="color:red">
<pre style="color:red">ugfx.input_init()</pre>
+
ugfx.input_init()</pre><pre>
<pre>
 
 
ugfx.clear(ugfx.WHITE)
 
ugfx.clear(ugfx.WHITE)
  

Revision as of 14:31, 1 March 2019

Introduction

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.

Compatibility

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

Your First Egg

__init__.py

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.

Libraries

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:

badge.init()
ugfx.init()

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:

ugfx.clear(ugfx.WHITE)

Saying Hello World!

Having now set everything up, we can now make our egg perform its purpose in life and display the string "Hello World!". In the first line we're assigning the string to a variable called displaytext though this is not essential and we could pass it directly. Then we are using uGFX's string() function to write the text to the display buffer. This takes five arguments, of which the first two are the coordinates at which we want the text to appear, the third is the text contained in our displaytext variable, the fourth is the name of one of the built-in fonts, and the fifth is another uGFX colour constant. We want black text, so we've used ugfx.BLACK. The final line executes uGFX's flush() function to send the display buffer to the screen.

So, enter the following lines into your file:

displaytext = "Hello World!"

ugfx.string(0, 0, displaytext,"PermanentMarker36", ugfx.BLACK)

ugfx.flush()

The completed egg

Your Hello World egg should now consist of an __init__.py file containing the following code:

import badge
import ugfx

badge.init()
ugfx.init()

ugfx.clear(ugfx.WHITE)

displaytext = "Hello World!"

ugfx.string(0, 0, displaytext,"PermanentMarker36", ugfx.BLACK)

ugfx.flush()

Of course, you now want to test it, and this can be done by uploading it to your badge using a tool called mpfshell. We have created a tutorial on that process, so head on over to the simple egg deployment page and follow the procedure there. When you have done so, you should see something like the screenshot below.

Hello World, on a Hacker Hotel 2019 badge.

Adding some interactivity

This egg now runs, but once it's running you can't exit it. The only way to do so is to reset your badge, either by power-cycling it or by pressing the reset button on the reverse. We'll now add some code to exit the egg on a button press.

import badge
import ugfx
import appglue

badge.init() ugfx.init()

ugfx.input_init()

ugfx.clear(ugfx.WHITE)

displaytext = "Hello World!"

ugfx.string(0, 0, displaytext,"PermanentMarker36", ugfx.BLACK)

ugfx.flush()

def exitegg(pressed):
    if(pressed):
        appglue.start_app("")
    ugfx.flush()

ugfx.input_attach(ugfx.BTN_A, lambda pressed: exitegg(pressed))

Further reading

What we have shown you here are only a few of your badge's capabilities, we have only tried to familiarise you with the egg development process. You may find the following further reading to be useful as you explore egg development further for your badge.