Embedded SDK
Embedded SDK
Advanced BOLOS GL API

Introduction

This chapter describes briefly the main interface functions of NBGL Graphical Engine. A full description of each function can be found in this document

Concepts

In this section, we provide information on how to use NBGL API

Graphic objects hierarchy

Creating graphic objects

Graphic objects cannot be created dynamically so they must be statically declared.

There must be at least as many declared objects as objects presented on a given screen. But objects can be reused in another screen, as long as they are properly set.

For example, if a "Back" button is present in many different screens of the application, it can be reused, the only change being the touch callback.

For all objects, its type field must be explicitly specified, even if it seems redundant with the nbgl_<type>_t.

For example:

nbgl_button_t backButton = {
.type = BUTTON, // Type of object, MANDATORY
.innerColor = WHITE, // Color used inside of the button, MANDATORY
.borderColor = LIGHT_GRAY, // Color used for border, MANDATORY
.foregroundColor = BLACK, // Color used for text and/or icon, MANDATORY
.width = 128, // Width in pixels, MANDATORY
.height = 64, // Height in pixels, MANDATORY
.radius = 32, // Radius of corners in pixels, can be null
.text = "Back", // Text of the button, if NULL, only an icon
.fontId = BAGL_FONT_INTER_REGULAR_24, // Font used for text, if text is not null
.icon = &C_leftArrow32px, // Icon of the button, if NULL, only text
.alignmentMarginX = 20, // Horizontal margin (see layout & alignment)
.alignmentMarginY = 20, // Vertical margin (see layout & alignment)
.alignment = TOP_LEFT, // Type of alignment (see layout & alignment)
.alignTo = NULL, // Alignment reference, if NULL, use parent (see layout & alignment)
.touchMask = (1<<VALUE_CHANGED) // Types of touchscreen events that we want to receive
};
struct PACKED__ nbgl_button_s nbgl_button_t
struct to represent a button (BUTTON type) that can contain a text and/or an icon
#define C_leftArrow32px
Definition: nbgl_obj.h:156
@ WHITE
Definition: nbgl_types.h:105
@ LIGHT_GRAY
Definition: nbgl_types.h:104
@ BLACK
Definition: nbgl_types.h:102
@ VALUE_CHANGED
corresponding to a change of state of the object (indirect event)
Definition: nbgl_types.h:232
@ TOP_LEFT
Definition: nbgl_types.h:142
@ BUTTON
Rounded rectangle button with icon and/or text.
Definition: nbgl_types.h:121

For some types of objects (CONTAINER), the children field must be set with an array of pointers on nbgl_obj_t, which are all the objects contained in these containers.

Once all your objects are defined, they shall be added as children of the main (and unique) SCREEN object, which is a special container, thanks to nbgl_screenSet() or nbgl_screenPush().

For example, with a static allocation of objects (not recommended, see Dynamic objects management):

nbgl_button_t backButton = {
.type = BUTTON
/*....*/
};
.type = TEXT_AREA
/*....*/
};
.type = TEXT_AREA
/*....*/
};
nbgl_obj_t* containerChildren[] = {
(nbgl_obj_t*)&text1,
(nbgl_obj_t*)&text2
};
// container containing 2 text areas
nbgl_container_t container = {
.type = CONTAINER,
.children = containerChildren,
.nbChildren = 2
/*....*/
};
// allocate screen 0 for 2 children
nbgl_screenSet(&screenChildren, 2, NULL, &touchCallback);
// set children of screen
screenChildren[0] = (nbgl_obj_t*)&backButton;
screenChildren[1] = (nbgl_obj_t*)&container;
struct PACKED__ nbgl_text_area_s nbgl_text_area_t
struct to represent a text area (TEXT_AREA type)
struct PACKED__ nbgl_container_s nbgl_container_t
struct to represent a container (CONTAINER type)
struct PACKED__ nbgl_obj_s nbgl_obj_t
Common structure for all graphical objects.
int nbgl_screenSet(nbgl_obj_t ***elements, uint8_t nbElements, const nbgl_screenTickerConfiguration_t *ticker, nbgl_touchCallback_t touchCallback)
Configures the lowest layer screen. To be used by applications A nbgl_screenRedraw() can be called af...
Definition: nbgl_screen.c:225
@ CONTAINER
Empty container.
Definition: nbgl_types.h:117
@ TEXT_AREA
Area to contain text line(s)
Definition: nbgl_types.h:120

The same example with dynamic allocation of objects:

/* layer 0 is used (background)
uint8_t layer = 0;
/* allocate layer 0 for 2 children (it also releases any objects/containers allocated previously in layer 0) */
nbgl_screenSet(&screenChildren, 2, NULL, &touchCallback);
/* allocate backButton dynamically */
nbgl_button_t *backButton = nbgl_objPoolGet(BUTTON, layer);
/* configure backButton properties here */
/* allocate text1 dynamically */
/* configure text1 properties here */
/* allocate text2 dynamically */
/* configure text2 properties here */
/* allocate container object dynamically */
/* configure container properties here */
container->nbChildren = 2;
container->children = (nbgl_obj_t**)nbgl_containerPoolGet(container->nbChildren, layer);
/* set text areas as children of container */
container->children[0] = (nbgl_obj_t*)&text1;
container->children[1] = (nbgl_obj_t*)&text2;
/* set backButton and container as children of screen */
screenChildren[0] = (nbgl_obj_t*)backButton;
screenChildren[1] = (nbgl_obj_t*)container;
nbgl_obj_t ** nbgl_containerPoolGet(uint8_t nbObjs, uint8_t layer)
Gets a new container from the pool, with the given number of obj pointers.
nbgl_obj_t * nbgl_objPoolGet(nbgl_obj_type_t type, uint8_t layer)
Gets a new graphic object from the pool, with the given type. The type field of the object is set.

Drawing graphic objects

Once defined and set as children (or sub-children) of the main SCREEN, all objects can be drawn in framebuffer with a simple call to nbgl_screenRedraw().

But if only a given object has been modified since the last redraw, for example by changing its text or its color, it can be redrawn (with all of its children and sub-children, if any) with a call to nbgl_objDraw().

The only properties that should not have changed for this object are its dimensions and position.

Except in some specific cases, the previousObj parameter of nbgl_objDraw() can be set to NULL and computeDimensions set to false.

Refreshing screen

After having drawn graphic objects in framebuffer, it is necessary to transfer the content of the framebuffer on display. This operation is called a refresh.

The API to do that is nbgl_refresh().

It will only refresh the rectangle part of the screen having changed (with any object redrawn) since the last refresh.