px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_gfx_obj.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2019 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_gfx_obj.h : Graphical object layer on drawing layer
14 Author(s): Pieter Conradie
15 Creation Date: 2019-06-30
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20#include <stdlib.h>
21
22/* _____PROJECT INCLUDES_____________________________________________________ */
23#include "px_gfx_obj.h"
24#include "px_log.h"
25
26/* _____LOCAL DEFINITIONS____________________________________________________ */
27PX_LOG_NAME("px_gfx_obj");
28
29/* _____MACROS_______________________________________________________________ */
30
31/* _____GLOBAL VARIABLES_____________________________________________________ */
32
33/* _____LOCAL VARIABLES______________________________________________________ */
34
35/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
36
37/* _____LOCAL FUNCTIONS______________________________________________________ */
38
39/* _____GLOBAL FUNCTIONS_____________________________________________________ */
40px_gfx_obj_handle_t _px_gfx_obj_create(px_gfx_obj_type_t obj_type,
41 size_t size,
42 px_gfx_obj_event_handler_t event_handler)
43{
45
46 PX_LOG_ASSERT(size >= sizeof(*obj));
47
48 // Allocate memory for object and set fields to zero
49 obj = calloc(1, size);
50 PX_LOG_ASSERT(obj != NULL);
51 // Set object type
52 obj->obj_type = obj_type;
53 // Set defaults
54 obj->visible = true;
55 obj->update = true;
56 // Set pointer to object event handler function
57 obj->event_handler = event_handler;
58
59 return obj;
60}
61
62bool px_gfx_obj_visible_get(px_gfx_obj_handle_t obj)
63{
64 PX_LOG_ASSERT(obj != NULL);
65
66 return obj->visible;
67}
68
69void px_gfx_obj_visible_set(px_gfx_obj_handle_t obj, bool flag)
70{
71 PX_LOG_ASSERT(obj != NULL);
72
73 obj->visible = flag;
74}
75
76bool px_gfx_obj_update_get(px_gfx_obj_handle_t obj)
77{
78 PX_LOG_ASSERT(obj != NULL);
79
80 return obj->update;
81}
82
83void px_gfx_obj_update_set(px_gfx_obj_handle_t obj, bool flag)
84{
85 PX_LOG_ASSERT(obj != NULL);
86
87 obj->update = true;
88}
89
90void px_gfx_obj_draw(px_gfx_obj_handle_t obj)
91{
92 PX_LOG_ASSERT(obj != NULL);
93
94 if(obj->visible && obj->event_handler)
95 {
96 (*obj->event_handler)(obj, PX_GFX_OBJ_EVENT_DRAW, NULL);
97 obj->update = false;
98 }
99}
#define NULL
NULL pointer.
Definition: px_defs.h:49
void(* px_gfx_obj_event_handler_t)(px_gfx_obj_handle_t obj, px_gfx_obj_event_t event, void *data)
Definition of a pointer to the object's event handler function.
Definition: px_gfx_obj.h:78
struct px_gfx_obj_s * px_gfx_obj_handle_t
Declaration of the object handle.
Definition: px_gfx_obj.h:69
px_gfx_obj_type_t
List of object types.
Definition: px_gfx_obj.h:51
#define PX_LOG_NAME(name)
Macro to declare a log name string once for each file to reduce code size.
Definition: px_log.h:349
#define PX_LOG_ASSERT(expression)
Macro that will test an expression, and block indefinitely if false.
Definition: px_log.h:440