Embedded SDK
Embedded SDK
nbgl_obj_pool.c
Go to the documentation of this file.
1 
9 /*********************
10  * INCLUDES
11  *********************/
12 #include "app_config.h"
13 #include "nbgl_obj.h"
14 #include "nbgl_debug.h"
15 #include <string.h>
16 
17 /*********************
18  * DEFINES
19  *********************/
24 #ifdef HAVE_SE_TOUCH
25 #define OBJ_POOL_LEN 80
26 #else // HAVE_SE_TOUCH
27 #define OBJ_POOL_LEN 40
28 #endif // HAVE_SE_TOUCH
33 #ifdef HAVE_SE_TOUCH
34 #define OBJ_CONTAINER_POOL_LEN 128
35 #else // HAVE_SE_TOUCH
36 #define OBJ_CONTAINER_POOL_LEN 64
37 #endif // HAVE_SE_TOUCH
38 
39 #define INVALID_LAYER 0xFF
40 
41 /**********************
42  * TYPEDEFS
43  **********************/
44 
45 typedef struct {
46  union {
52 #ifdef HAVE_SE_TOUCH
59 #else // HAVE_SE_TOUCH
60  nbgl_text_entry_t entryObj;
61 #endif // HAVE_SE_TOUCH
62 #ifdef NBGL_KEYBOARD
64 #endif // NBGL_KEYBOARD
65 #ifdef NBGL_KEYPAD
67 #endif // NBGL_KEYPAD
68  };
69 } genericObj_t;
70 
71 /**********************
72  * STATIC PROTOTYPES
73  **********************/
74 
75 /**********************
76  * STATIC VARIABLES
77  **********************/
78 
79 // This is the pool of graphic objects
80 static genericObj_t objPool[OBJ_POOL_LEN];
81 // This is the array of layer per object in pool
82 static uint8_t objPoolLayers[OBJ_POOL_LEN];
83 // This is the number of graphic objects currently used from the objPool
84 static uint8_t nbUsedObjsInPool = 0;
85 
86 // This is the pool of graphic objects pointers for containers
87 static nbgl_obj_t *objContainerPool[OBJ_CONTAINER_POOL_LEN];
88 // This is the array of layer per object pointers in pool
89 static uint8_t objPointersPoolLayers[OBJ_CONTAINER_POOL_LEN];
90 // This is the number of graphic objects currently used from the objContainerPool
91 static uint8_t nbUsedObjsInContainerPool = 0;
92 
93 static bool initialized;
94 
95 /**********************
96  * MACROS
97  **********************/
98 
99 /**********************
100  * GLOBAL FUNCTIONS
101  **********************/
102 
103 /*********************
104  * DEFINES
105  *********************/
106 
107 /**********************
108  * TYPEDEFS
109  **********************/
110 
111 /**********************
112  * VARIABLES
113  **********************/
114 /**********************
115  * STATIC PROTOTYPES
116  **********************/
117 
118 /**********************
119  * GLOBAL FUNCTIONS
120  **********************/
121 
127 {
128  uint8_t i;
130  "nbgl_objPoolRelease(): used objs in pool %d, for layer (%d)\n",
131  nbUsedObjsInPool,
132  layer);
133  if (nbUsedObjsInPool == 0) {
134  return;
135  }
136  for (i = 0; i < OBJ_POOL_LEN; i++) {
137  if (layer == objPoolLayers[i]) {
138  objPoolLayers[i] = INVALID_LAYER;
139  nbUsedObjsInPool--;
140  }
141  }
142 }
143 
148 {
149  uint8_t i, nbFound = 0;
150  for (i = 0; i < OBJ_POOL_LEN; i++) {
151  if (layer == objPoolLayers[i]) {
152  nbFound++;
153  }
154  }
155  return nbFound;
156 }
157 
165 {
166  uint8_t i;
167  nbgl_obj_t *obj;
168 
169  if (initialized == false) {
170  memset(objPoolLayers, INVALID_LAYER, OBJ_POOL_LEN);
171  memset(objPointersPoolLayers, INVALID_LAYER, OBJ_CONTAINER_POOL_LEN);
172  initialized = true;
173  }
174  if (nbUsedObjsInPool == (OBJ_POOL_LEN - 1)) {
175  LOG_FATAL(
176  OBJ_POOL_LOGGER, "nbgl_objPoolGet(): NO ENOUGH OBJ in POOL. max = %d\n", OBJ_POOL_LEN);
177  // TODO: crash here?
178  return NULL;
179  }
180  for (i = 0; i < OBJ_POOL_LEN; i++) {
181  if (INVALID_LAYER == objPoolLayers[i]) {
182  obj = &objPool[i].obj;
183  memset(obj, 0, sizeof(genericObj_t));
184  obj->type = type;
185  objPoolLayers[i] = layer;
186  nbUsedObjsInPool++;
187  return obj;
188  }
189  }
190  // should never happen
191  return NULL;
192 }
193 
202 {
203  uint8_t index;
204 
205  // retrieve object index
206  index = (genericObj_t *) obj - objPool;
207 
208  while (index > 0) {
209  index--;
210  if (objPoolLayers[index] == layer) {
211  return &objPool[index].obj;
212  }
213  }
214 
215  return NULL;
216 }
217 
224 {
225  uint8_t index;
226 
227  // retrieve object index
228  index = (genericObj_t *) obj - objPool;
229 
230  return index;
231 }
232 
242 int nbgl_objPoolGetArray(nbgl_obj_type_t type, uint8_t nbObjs, uint8_t layer, nbgl_obj_t **objArray)
243 {
244  uint8_t i;
245 
246  if ((nbUsedObjsInPool + nbObjs) >= OBJ_POOL_LEN) {
248  "nbgl_objPoolGetArray(): NO ENOUGH OBJ in POOL. Asked: %d, max = %d\n",
249  (nbUsedObjsInPool + nbObjs),
250  OBJ_POOL_LEN);
251  // TODO: crash here?
252  return -1;
253  }
254  for (i = 0; i < nbObjs; i++) {
255  objArray[i] = nbgl_objPoolGet(type, layer);
256  }
257 
258  return 0;
259 }
260 
266 {
267  uint8_t i;
269  "nbgl_containerPoolRelease(): %d used obj containers in pool, trying to release for "
270  "layer %d\n",
271  nbUsedObjsInContainerPool,
272  layer);
273  if (nbUsedObjsInContainerPool == 0) {
274  return;
275  }
276  for (i = 0; i < OBJ_CONTAINER_POOL_LEN; i++) {
277  if (layer == objPointersPoolLayers[i]) {
278  objContainerPool[i] = NULL;
279  objPointersPoolLayers[i] = INVALID_LAYER;
280  nbUsedObjsInContainerPool--;
281  }
282  }
283 }
284 
291 {
292  uint8_t i = 0, nbContiguousFree = 0;
293  nbgl_obj_t **container;
295  "nbgl_containerPoolGet(): getting %d obj containers for layer %d\n",
296  nbObjs,
297  layer);
298  if (initialized == false) {
299  memset(objPoolLayers, INVALID_LAYER, OBJ_POOL_LEN);
300  memset(objPointersPoolLayers, INVALID_LAYER, OBJ_CONTAINER_POOL_LEN);
301  initialized = true;
302  }
303  if ((nbUsedObjsInContainerPool + nbObjs) >= (OBJ_CONTAINER_POOL_LEN - 1)) {
305  "nbgl_containerPoolGet(): NO ENOUGH OBJ in POOL. Asked: %d, max = %d\n",
306  (nbUsedObjsInContainerPool + nbObjs),
308  // TODO: crash here?
309  return NULL;
310  }
311  while (i < OBJ_CONTAINER_POOL_LEN) {
312  if (INVALID_LAYER == objPointersPoolLayers[i]) {
313  nbContiguousFree++;
314  }
315  else {
316  nbContiguousFree = 0;
317  }
318  i++;
319  if (nbContiguousFree == nbObjs) {
320  container = &objContainerPool[i - nbObjs];
321  memset(container, 0, nbObjs * sizeof(nbgl_obj_t *));
322  // mark slots as used
323  memset(&objPointersPoolLayers[i - nbObjs], layer, nbObjs);
324  nbUsedObjsInContainerPool += nbObjs;
325  return container;
326  }
327  }
328  return NULL;
329 }
330 
335 {
336  uint8_t i, nbFound = 0;
337  for (i = 0; i < OBJ_CONTAINER_POOL_LEN; i++) {
338  if (layer == objPointersPoolLayers[i]) {
339  nbFound++;
340  }
341  }
342  return nbFound;
343 }
debug traces management
#define LOG_DEBUG(__logger,...)
Definition: nbgl_debug.h:86
#define LOG_FATAL(__logger,...)
Definition: nbgl_debug.h:88
@ OBJ_POOL_LOGGER
Definition: nbgl_debug.h:31
API to draw all basic graphic objects.
struct PACKED__ nbgl_line_s nbgl_line_t
struct to represent a vertical or horizontal line
struct PACKED__ nbgl_navigation_bar_s nbgl_page_indicator_t
struct to represent a navigation bar (PAGE_INDICATOR type) There can be up to 5 page indicators,...
struct PACKED__ nbgl_radio_s nbgl_radio_t
struct to represent a radio button (RADIO_BUTTON type)
struct PACKED__ nbgl_text_area_s nbgl_text_area_t
struct to represent a text area (TEXT_AREA type)
struct PACKED__ nbgl_progress_bar_s nbgl_progress_bar_t
struct to represent a progress bar (PROGRESS_BAR type)
struct PACKED__ nbgl_keypad_s nbgl_keypad_t
struct to represent a keypad (KEYPAD type)
struct PACKED__ nbgl_text_entry_s nbgl_text_entry_t
struct to represent a text entry area (TEXT_ENTRY type)
struct PACKED__ nbgl_keyboard_s nbgl_keyboard_t
struct to represent a keyboard (KEYBOARD type)
struct PACKED__ nbgl_image_s nbgl_image_t
struct to represent an image (IMAGE type)
struct PACKED__ nbgl_button_s nbgl_button_t
struct to represent a button (BUTTON type) that can contain a text and/or an icon
struct PACKED__ nbgl_container_s nbgl_container_t
struct to represent a container (CONTAINER type)
struct PACKED__ nbgl_switch_s nbgl_switch_t
struct to represent a switch (size is fixed) (SWITCH type)
struct PACKED__ nbgl_obj_s nbgl_obj_t
Common structure for all graphical objects.
struct PACKED__ nbgl_spinner_s nbgl_spinner_t
struct to represent a "spinner", represented by the Ledger corners, in gray, with one of the corners ...
uint8_t nbgl_containerPoolGetNbUsed(uint8_t layer)
returns the number of containers currently used in the pool
#define OBJ_CONTAINER_POOL_LEN
Max number of objects pointers usable for container pool.
Definition: nbgl_obj_pool.c:34
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.
void nbgl_containerPoolRelease(uint8_t layer)
Release the objects pointers from the pool for the given layer.
int nbgl_objPoolGetArray(nbgl_obj_type_t type, uint8_t nbObjs, uint8_t layer, nbgl_obj_t **objArray)
Gets nbObjects new graphic object from the pool, with the given type, for the given layer (screen)....
uint8_t nbgl_objPoolGetId(nbgl_obj_t *obj)
Gets a unique index for the given object, in the pool.
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.
uint8_t nbgl_objPoolGetNbUsed(uint8_t layer)
returns the number of objects currently used in the pool
#define OBJ_POOL_LEN
Max number of objects retrievable from pool.
Definition: nbgl_obj_pool.c:25
#define INVALID_LAYER
Definition: nbgl_obj_pool.c:39
void nbgl_objPoolRelease(uint8_t layer)
Release the objects from the pool for the given layer.
nbgl_obj_t * nbgl_objPoolGetPrevious(nbgl_obj_t *obj, uint8_t layer)
Gets a link to the previous object in the pool, for the given layer. (to be used with care)
nbgl_obj_type_t
All types of graphical objects.
Definition: nbgl_types.h:115
nbgl_text_area_t textAreaObj
Definition: nbgl_obj_pool.c:48
nbgl_image_t imageObj
Definition: nbgl_obj_pool.c:51
nbgl_switch_t switchObj
Definition: nbgl_obj_pool.c:54
nbgl_page_indicator_t navBarObj
Definition: nbgl_obj_pool.c:56
nbgl_spinner_t spinnerObj
Definition: nbgl_obj_pool.c:58
nbgl_container_t containerObj
Definition: nbgl_obj_pool.c:50
nbgl_keyboard_t keyboardObj
Definition: nbgl_obj_pool.c:63
nbgl_line_t lineObj
Definition: nbgl_obj_pool.c:57
nbgl_obj_t obj
Definition: nbgl_obj_pool.c:47
nbgl_keypad_t keypadObj
Definition: nbgl_obj_pool.c:66
nbgl_button_t buttonObj
Definition: nbgl_obj_pool.c:55
nbgl_progress_bar_t progressBarObj
Definition: nbgl_obj_pool.c:49
nbgl_radio_t radioObj
Definition: nbgl_obj_pool.c:53
unsigned char uint8_t
Definition: usbd_conf.h:53