summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/littlevgl/wasm-apps/src/main.c
blob: 8676d41b5302de4174319d6cdf331f1a99e22add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

/**
 * @file main
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include <stdlib.h>
//#include <unistd.h>
#include <inttypes.h>
#include "lvgl/lvgl.h"
#include "display_indev.h"
#include "wasm_app.h"
#include "wa-inc/timer_wasm_app.h"
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void
hal_init(void);
// static int tick_thread(void * data);
// static void memory_monitor(void * param);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
uint32_t count = 0;
char count_str[11] = { 0 };
lv_obj_t *hello_world_label;
lv_obj_t *count_label;
lv_obj_t *btn1;

lv_obj_t *label_count1;
int label_count1_value = 0;
char label_count1_str[11] = { 0 };

void
timer1_update(user_timer_t timer1)
{
    if ((count % 100) == 0) {
        snprintf(count_str, sizeof(count_str), "%d", count / 100);
        lv_label_set_text(count_label, count_str);
    }
    lv_task_handler();
    ++count;
}

static lv_res_t
btn_rel_action(lv_obj_t *btn)
{
    label_count1_value++;
    snprintf(label_count1_str, sizeof(label_count1_str), "%d",
             label_count1_value);
    lv_label_set_text(label_count1, label_count1_str);
    return LV_RES_OK;
}

void
on_init()
{
    /* Initialize LittlevGL */
    lv_init();

    /* Initialize the HAL (display, input devices, tick) for LittlevGL */
    hal_init();

    hello_world_label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(hello_world_label, "Hello world!");
    lv_obj_align(hello_world_label, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);

    count_label = lv_label_create(lv_scr_act(), NULL);
    lv_obj_align(count_label, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);

    btn1 = lv_btn_create(
        lv_scr_act(),
        NULL); /* Create a button on the currently loaded screen */
    lv_btn_set_action(btn1, LV_BTN_ACTION_CLICK,
                      btn_rel_action); /* Set function to be called when the
                                          button is released */
    lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0,
                 20); /* Align below the label */

    /* Create a label on the button */
    lv_obj_t *btn_label = lv_label_create(btn1, NULL);
    lv_label_set_text(btn_label, "Click ++");

    label_count1 = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(label_count1, "0");
    lv_obj_align(label_count1, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);

    /* set up a timer */
    user_timer_t timer;
    timer = api_timer_create(10, true, false, timer1_update);
    if (timer)
        api_timer_restart(timer, 10);
    else
        printf("Fail to create timer.\n");
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/**
 * Initialize the Hardware Abstraction Layer (HAL) for the Littlev graphics
 * library
 */
void
display_flush_wrapper(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
                      const lv_color_t *color_p)
{
    display_flush(x1, y1, x2, y2, color_p);
    lv_flush_ready();
}

void
display_vdb_write_wrapper(uint8_t *buf, lv_coord_t buf_w, lv_coord_t x,
                          lv_coord_t y, lv_color_t color, lv_opa_t opa)
{
    display_vdb_write(buf, buf_w, x, y, &color, opa);
}

void
display_fill_wrapper(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
                     lv_color_t color)
{
    display_fill(x1, y1, x2, y2, &color);
}

static void
hal_init(void)
{
    /* Add a display */
    lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv); /* Basic initialization */
    disp_drv.disp_flush =
        display_flush_wrapper; /* Used when `LV_VDB_SIZE != 0` in lv_conf.h
                                  (buffered drawing) */
    disp_drv.disp_fill =
        display_fill_wrapper; /* Used when `LV_VDB_SIZE == 0` in lv_conf.h
                                 (unbuffered drawing) */
    disp_drv.disp_map = display_map; /* Used when `LV_VDB_SIZE == 0` in
                                        lv_conf.h (unbuffered drawing) */
#if LV_VDB_SIZE != 0
    disp_drv.vdb_wr = display_vdb_write_wrapper;
#endif
    lv_disp_drv_register(&disp_drv);

    /* Add the mouse as input device
     * Use the 'mouse' driver which reads the PC's mouse */
    // mouse_init();
    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv); /* Basic initialization */
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read =
        display_input_read; /* This function will be called periodically (by the
                               library) to get the mouse position and state */
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv);
}

/* Implement empry main function as wasi start function calls it */
int
main(int argc, char **argv)
{
    (void)argc;
    (void)argv;
    return 0;
}