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