microui  14.2.0
microui
microui_event_decoder.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2024 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
18 #include "ui_configuration.h"
19 
20 #if defined(UI_FEATURE_EVENT_DECODER)
21 
22 // -----------------------------------------------------------------------------
23 // Includes
24 // -----------------------------------------------------------------------------
25 
26 // calls Microui events decoder functions
27 #include "microui_event_decoder.h"
28 
29 // -----------------------------------------------------------------------------
30 // Macros and Defines
31 // -----------------------------------------------------------------------------
32 
33 /*
34  * @brief Default traces.
35  */
36 #define DESCRIBE_EVENT_GENERATOR(event) (UI_DEBUG_PRINT(" (event generator %u)", EVENT_GENERATOR_ID(event)))
37 #define DESCRIBE_EOL() (UI_DEBUG_PRINT("\n"))
38 
39 /*
40  * @brief Decodes MicroUI event.
41  */
42 #define EVENT_DATA(event) ((uint16_t)(event))
43 #define EVENT_GENERATOR_ID(event) ((uint8_t)((event) >> 16))
44 
45 /*
46  * @brief Decodes MicroUI Command event.
47  */
48 #define COMMAND_GET(event) EVENT_DATA(event)
49 
50 /*
51  * @brief Decodes MicroUI Buttons event.
52  */
53 #define BUTTON_ACTION_PRESSED 0
54 #define BUTTON_ACTION_RELEASED 1
55 #define BUTTON_ACTION_LONG 2
56 #define BUTTON_ACTION_REPEATED 3
57 #define BUTTON_ACTION_CLICK 4
58 #define BUTTON_ACTION_DOUBLECLICK 5
59 #define BUTTON_ACTION(event) ((uint8_t)((event) >> 8))
60 #define BUTTON_ID(event) ((uint8_t)(event))
61 
62 /*
63  * @brief Decodes MicroUI Pointer event.
64  */
65 #define POINTER_ACTION_MOVE 6
66 #define POINTER_ACTION_DRAG 7
67 #define POINTER_X(data) (((data) >> 16) & 0xfff)
68 #define POINTER_Y(data) ((data) & 0xfff)
69 #define POINTER_TYPE(data) (((data) >> 31) & 0x1)
70 
71 /*
72  * @brief Decodes MicroUI user event.
73  */
74 #define USEREVENT_SIZE(event) EVENT_DATA(event)
75 
76 // -----------------------------------------------------------------------------
77 // Internal functions
78 // -----------------------------------------------------------------------------
79 
80 static void decode_event_command(uint32_t event, uint32_t index,
81  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
82  (void)index;
83  (void)fct_data_decoder;
84 
85  UI_DEBUG_PRINT("Command ");
86 
87  uint8_t command = (uint8_t)COMMAND_GET(event);
88 
89  switch (command) {
90  case 0:
91  UI_DEBUG_PRINT("ESC");
92  break;
93 
94  case 1:
95  UI_DEBUG_PRINT("BACK");
96  break;
97 
98  case 2:
99  UI_DEBUG_PRINT("UP");
100  break;
101 
102  case 3:
103  UI_DEBUG_PRINT("DOWN");
104  break;
105 
106  case 4:
107  UI_DEBUG_PRINT("LEFT");
108  break;
109 
110  case 5:
111  UI_DEBUG_PRINT("RIGHT");
112  break;
113 
114  case 6:
115  UI_DEBUG_PRINT("SELECT");
116  break;
117 
118  case 7:
119  UI_DEBUG_PRINT("CANCEL");
120  break;
121 
122  case 8:
123  UI_DEBUG_PRINT("HELP");
124  break;
125 
126  case 9:
127  UI_DEBUG_PRINT("MENU");
128  break;
129 
130  case 10:
131  UI_DEBUG_PRINT("EXIT");
132  break;
133 
134  case 11:
135  UI_DEBUG_PRINT("START");
136  break;
137 
138  case 12:
139  UI_DEBUG_PRINT("STOP");
140  break;
141 
142  case 13:
143  UI_DEBUG_PRINT("PAUSE");
144  break;
145 
146  case 14:
147  UI_DEBUG_PRINT("RESUME");
148  break;
149 
150  case 15:
151  UI_DEBUG_PRINT("COPY");
152  break;
153 
154  case 16:
155  UI_DEBUG_PRINT("CUT");
156  break;
157 
158  case 17:
159  UI_DEBUG_PRINT("PASTE");
160  break;
161 
162  case 18:
163  UI_DEBUG_PRINT("CLOCKWISE");
164  break;
165 
166  case 19:
167  UI_DEBUG_PRINT("ANTICLOCKWISE");
168  break;
169 
170  case 20:
171  UI_DEBUG_PRINT("PREVIOUS");
172  break;
173 
174  case 21:
175  UI_DEBUG_PRINT("NEXT");
176  break;
177 
178  default:
179  UI_DEBUG_PRINT("0x%02x", command);
180  break;
181  }
182 
183  DESCRIBE_EVENT_GENERATOR(event);
184  DESCRIBE_EOL();
185 }
186 
187 static void decode_event_button(uint32_t event, uint32_t index,
188  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
189  (void)index;
190  (void)fct_data_decoder;
191 
192  UI_DEBUG_PRINT("Button %u ", BUTTON_ID(event));
193  uint8_t action = BUTTON_ACTION(event);
194 
195  switch (action) {
196  case BUTTON_ACTION_PRESSED:
197  UI_DEBUG_PRINT("pressed");
198  break;
199 
200  case BUTTON_ACTION_RELEASED:
201  UI_DEBUG_PRINT("released");
202  break;
203 
204  case BUTTON_ACTION_LONG:
205  UI_DEBUG_PRINT("long");
206  break;
207 
208  case BUTTON_ACTION_REPEATED:
209  UI_DEBUG_PRINT("repeated");
210  break;
211 
212  case BUTTON_ACTION_CLICK:
213  UI_DEBUG_PRINT("click");
214  break;
215 
216  case BUTTON_ACTION_DOUBLECLICK:
217  UI_DEBUG_PRINT("double-click");
218  break;
219 
220  default:
221  UI_DEBUG_PRINT("unknown action: %u", action);
222  break;
223  }
224 
225  DESCRIBE_EVENT_GENERATOR(event);
226  DESCRIBE_EOL();
227 }
228 
229 #ifdef UI_EVENTDECODER_EVENTGEN_TOUCH
230 /*
231  * @brief Input pointer event holds a data that contains x and y.
232  */
233 static void decode_event_pointer_data(uint32_t event, uint32_t data, uint32_t index) {
234  (void)event;
235 
236  UI_DEBUG_PRINT("[%02u: 0x%08x] at %u,%u (", index, data, POINTER_X(data), POINTER_Y(data));
237  if (0 == POINTER_TYPE(data)) {
238  UI_DEBUG_PRINT("absolute)");
239  } else {
240  UI_DEBUG_PRINT("relative)");
241  }
242 
243  DESCRIBE_EOL();
244 }
245 
246 #endif // UI_EVENTDECODER_EVENTGEN_TOUCH
247 
248 static void decode_event_pointer(uint32_t event, uint32_t index,
249  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
250  (void)index;
251  (void)fct_data_decoder;
252 
253  UI_DEBUG_PRINT("Pointer ");
254  uint8_t action = BUTTON_ACTION(event);
255 
256  switch (action) {
257  case BUTTON_ACTION_PRESSED:
258  UI_DEBUG_PRINT("pressed");
259  break;
260 
261  case BUTTON_ACTION_RELEASED:
262  UI_DEBUG_PRINT("released");
263  break;
264 
265  case BUTTON_ACTION_LONG:
266  UI_DEBUG_PRINT("long");
267  break;
268 
269  case BUTTON_ACTION_REPEATED:
270  UI_DEBUG_PRINT("repeated");
271  break;
272 
273  case BUTTON_ACTION_CLICK:
274  UI_DEBUG_PRINT("click");
275  break;
276 
277  case BUTTON_ACTION_DOUBLECLICK:
278  UI_DEBUG_PRINT("double-click");
279  break;
280 
281  case POINTER_ACTION_MOVE:
282  UI_DEBUG_PRINT("moved");
283  break;
284 
285  case POINTER_ACTION_DRAG:
286  UI_DEBUG_PRINT("dragged");
287  break;
288 
289  default:
290  UI_DEBUG_PRINT("unknown action: %u", action);
291  break;
292  }
293 
294  DESCRIBE_EVENT_GENERATOR(event);
295  DESCRIBE_EOL();
296 }
297 
298 static void decode_event_state(uint32_t event, uint32_t index,
299  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
300  (void)index;
301  (void)fct_data_decoder;
302 
303  UI_DEBUG_PRINT("TODO %s 0x%08x\n", __FUNCTION__, event);
304 }
305 
306 /*
307  * @brief Decodes the input events: the events sent by the BSP. To decode the events,
308  * the decoder must know the event generators identifier defined during the MicroEJ
309  * platform build. These identifiers are available in microui_constants.h.
310  *
311  * @see ui_configuration.h
312  */
313 static void decode_event_input(uint32_t event, uint32_t index,
314  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
315  UI_DEBUG_PRINT("Input event: ");
316 
317  uint8_t generator_id = EVENT_GENERATOR_ID(event);
318 
319  switch (generator_id) {
320 #ifdef UI_EVENTDECODER_EVENTGEN_COMMAND
321  case MICROUI_EVENTGEN_COMMANDS:
322  decode_event_command(event, index, fct_data_decoder);
323  break;
324 #endif // UI_EVENTDECODER_EVENTGEN_COMMAND
325 
326 #ifdef UI_EVENTDECODER_EVENTGEN_BUTTONS
327  case UI_EVENTDECODER_EVENTGEN_BUTTONS:
328  decode_event_button(event, index, fct_data_decoder);
329  break;
330 #endif // UI_EVENTDECODER_EVENTGEN_BUTTONS
331 
332 #ifdef UI_EVENTDECODER_EVENTGEN_TOUCH
333  case UI_EVENTDECODER_EVENTGEN_TOUCH:
334  *fct_data_decoder = decode_event_pointer_data;
335  decode_event_pointer(event, index, fct_data_decoder);
336  break;
337 #endif // UI_EVENTDECODER_EVENTGEN_TOUCH
338 
339  default:
340  UI_DEBUG_PRINT("unknown ");
341  DESCRIBE_EVENT_GENERATOR(event);
342  DESCRIBE_EOL();
343  break;
344  }
345 }
346 
347 static void decode_event_user_data(uint32_t event, uint32_t data, uint32_t index) {
348  (void)event;
349  UI_DEBUG_PRINT(" [%02u] 0x%08x\n", index, data);
350 }
351 
352 static void decode_event_user(uint32_t event, uint32_t index,
353  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
354  (void)index;
355  UI_DEBUG_PRINT("User input event");
356 
357  uint8_t size = (uint8_t)USEREVENT_SIZE(event);
358  if (size > (uint32_t)1) {
359  UI_DEBUG_PRINT("s (size = %u)", size);
360  }
361  DESCRIBE_EVENT_GENERATOR(event);
362  UI_DEBUG_PRINT(": ");
363 
364  *fct_data_decoder = decode_event_user_data;
365 }
366 
367 // -----------------------------------------------------------------------------
368 // API
369 // -----------------------------------------------------------------------------
370 
371 void MICROUI_EVENT_DECODER_describe_dump_start(void) {
372  UI_DEBUG_PRINT("============================== MicroUI FIFO Dump ===============================\n");
373 }
374 
375 void MICROUI_EVENT_DECODER_describe_dump_past(void) {
376  UI_DEBUG_PRINT("---------------------------------- Old Events ----------------------------------\n");
377 }
378 
379 void MICROUI_EVENT_DECODER_describe_dump_future(void) {
380  UI_DEBUG_PRINT("---------------------------------- New Events ----------------------------------\n");
381 }
382 
383 void MICROUI_EVENT_DECODER_describe_dump_events_objects(void) {
384  UI_DEBUG_PRINT("--------------------------- New Events' Java objects ---------------------------\n");
385 }
386 
387 void MICROUI_EVENT_DECODER_describe_dump_end(void) {
388  UI_DEBUG_PRINT("================================================================================\n");
389 }
390 
391 void MICROUI_EVENT_DECODER_drop_data(uint32_t data, uint32_t index) {
392  UI_DEBUG_PRINT("[%02u: 0x%08x] garbage\n", index, data);
393 }
394 
395 void MICROUI_EVENT_DECODER_decode_event(uint32_t event, uint32_t index,
396  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
397  UI_DEBUG_PRINT("[%02u: 0x%08x] ", index, event);
398 
399  uint8_t event_type = (uint8_t)(event >> 24);
400 
401  switch (event_type) {
402  case 0x00:
403  decode_event_command(event, index, fct_data_decoder);
404  break;
405 
406  case 0x01:
407  decode_event_button(event, index, fct_data_decoder);
408  break;
409 
410  case 0x02:
411  decode_event_pointer(event, index, fct_data_decoder);
412  break;
413 
414  case 0x03:
415  decode_event_state(event, index, fct_data_decoder);
416  break;
417 
418  case 0x04:
419  // not used
420  break;
421 
422  case 0x05:
423  UI_DEBUG_PRINT("Call serially (Runnable index = %u)\n", EVENT_DATA(event));
424  break;
425 
426  case 0x06:
427  UI_DEBUG_PRINT("MicroUI STOP\n");
428  break;
429 
430  case 0x07:
431  decode_event_input(event, index, fct_data_decoder);
432  break;
433 
434  case 0x08:
435  UI_DEBUG_PRINT("Display SHOW Displayable (Displayable index = %u)\n", EVENT_DATA(event));
436  break;
437 
438  case 0x09:
439  UI_DEBUG_PRINT("Display HIDE Displayable (Displayable index = %u)\n", EVENT_DATA(event));
440  break;
441 
442  case 0x0a:
443  // not used
444  break;
445 
446  case 0x0b:
447  UI_DEBUG_PRINT("Display FLUSH\n");
448  break;
449 
450  case 0x0c:
451  UI_DEBUG_PRINT("Display FORCE FLUSH\n");
452  break;
453 
454  case 0x0d:
455  UI_DEBUG_PRINT("Display REPAINT Displayable (Displayable index = %u)\n", EVENT_DATA(event));
456  break;
457 
458  case 0x0e:
459  UI_DEBUG_PRINT("Display REPAINT Current Displayable\n");
460  break;
461 
462  case 0x0f:
463  UI_DEBUG_PRINT("Display KF SWITCH Display\n");
464  break;
465 
466  default:
467  decode_event_user(event, index, fct_data_decoder);
468  break;
469  }
470 }
471 
472 // -----------------------------------------------------------------------------
473 // EOF
474 // -----------------------------------------------------------------------------
475 
476 #endif // UI_FEATURE_EVENT_DECODER
MicroEJ MicroUI library low level API: enable some features according to the hardware capabilities.
#define UI_DEBUG_PRINT
Standard "printf" indirection.