Difference between revisions of "Tutorials/Your First Egg (for uGFX badges)"
|Line 41:||Line 41:|
=== Saying Hello World! ===
=== Saying Hello World! ===
Revision as of 13:00, 1 March 2019
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.
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:
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.
displaytext = "Hello World!" ugfx.string(0, 0, displaytext,"PermanentMarker36", ugfx.BLACK) ugfx.flush()