Bio photo

Dan Nixon

Code Monkey, Electronics Engineer, Mad Scientist.

Creating apps for the Dilbert badge

By Dan Nixon on 2016-07-19

This will be a quick tutorial on how to create and deploy an "application" for the Dilbert badge.

Requirements

Before starting ensure you have the following:

  • Latest version of the Arduino IDE (I used version 1.6.9)
  • Downloaded the following Arduino libraries (all should be available through the library manager):
  • Add the Dilbert firmware library by:
    • Downloading a copy of the Dilbert repository
    • Renaming the library directory to Dilbert
    • Moving the directory into the Arduino library directory

App capabilities

Applications are generally fairly unrestricted as to what they can do with the hardware, however there are a few notable design considerations:

  • Apps can only run in the foreground
  • Apps should reserve all their memory at creation time
  • Likewise they should release it all when they are destroyed

The only thing that is actually enforced is that apps can only run in the foreground, switching applications forces the currently active one to exit and it cannot execute any code until it is the active application again.

The memory allocation is a general good coding practice regardless, the preference of allocating all memory at creation time is to help avoid heap fragmentation from multiple allocations during run time.

When updating the display it can often be faster to only update a subset of the display, I also believe that updating entire rows/columns (one of the two, I can't remember which from memory) is faster due to the way in which the display is addressed. For this reason it is worth only drawing areas of the screen that have actually changed, a good example of this is the App Manager application in which only the selected arrow and the area of the screen it can possibly reside ever has to br redrawn.

Application Life Cycle

Application Life Cycle

Flow diagram between application states

Creating an app

  1. Open the AppManager example and save a copy of it
  2. Create a new class that inherits from App
  3. Override the relevant functions:
    • create() is called when the app is created, ideally all memory allocation should be performed here
    • onEntry() is called when the application becomes the foreground application, this should be used for setting the initial state of the application (for example, drawing items to the LCD that will not change throughout the runtime of the application)
    • run() is called periodically whilst the application is in the foreground, delays can (and often should) be added where possible/required
    • onExit() is called when the application is no longer the foreground application, this can be used to save the application state or reset any hardware used by the application
    • destroy() is called when the application is destroyed, all memory allocated in create() should be freed here
  4. Add the application to the AppManager instance in AppManager.ino:
    appManager.addApp(new DemoApp("ONE"));
    

Examples

There are a few examples in the library already that can be used as example implementations:

Comments