|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Intel Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdlib.h> |
| 7 | +#include "wasm_app.h" |
| 8 | +#include "wa-inc/lvgl/lvgl.h" |
| 9 | +#include "wa-inc/timer_wasm_app.h" |
| 10 | + |
| 11 | +extern char g_widget_text[]; |
| 12 | + |
| 13 | +static void btn_event_cb(lv_obj_t *btn, lv_event_t event); |
| 14 | + |
| 15 | +uint32_t count = 0; |
| 16 | +char count_str[11] = { 0 }; |
| 17 | +lv_obj_t *hello_world_label; |
| 18 | +lv_obj_t *count_label; |
| 19 | +lv_obj_t *btn1; |
| 20 | +lv_obj_t *label_count1; |
| 21 | +int label_count1_value = 100; |
| 22 | +char label_count1_str[11] = { 0 }; |
| 23 | + |
| 24 | +void timer1_update(user_timer_t timer1) |
| 25 | +{ |
| 26 | + if ((count % 100) == 0) { |
| 27 | + snprintf(count_str, sizeof(count_str), "%d", count / 100); |
| 28 | + lv_label_set_text(count_label, count_str); |
| 29 | + } |
| 30 | + ++count; |
| 31 | +} |
| 32 | + |
| 33 | +void on_init() |
| 34 | +{ |
| 35 | + char *text; |
| 36 | + |
| 37 | + hello_world_label = lv_label_create(NULL, NULL); |
| 38 | + lv_label_set_text(hello_world_label, "Hello world!"); |
| 39 | + text = lv_label_get_text(hello_world_label); |
| 40 | + printf("Label text %lu %s \n", strlen(text), text); |
| 41 | + lv_obj_align(hello_world_label, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0); |
| 42 | + |
| 43 | + count_label = lv_label_create(NULL, NULL); |
| 44 | + lv_obj_align(count_label, NULL, LV_ALIGN_IN_TOP_MID, 0, 0); |
| 45 | + |
| 46 | + btn1 = lv_btn_create(NULL, NULL); /*Create a button on the currently loaded screen*/ |
| 47 | + lv_obj_set_event_cb(btn1, btn_event_cb); /*Set function to be called when the button is released*/ |
| 48 | + lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, 0); /*Align below the label*/ |
| 49 | + |
| 50 | + /*Create a label on the button*/ |
| 51 | + lv_obj_t *btn_label = lv_label_create(btn1, NULL); |
| 52 | + lv_label_set_text(btn_label, "Click --"); |
| 53 | + |
| 54 | + label_count1 = lv_label_create(NULL, NULL); |
| 55 | + lv_label_set_text(label_count1, "100"); |
| 56 | + lv_obj_align(label_count1, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0); |
| 57 | + |
| 58 | + /* set up a timer */ |
| 59 | + user_timer_t timer; |
| 60 | + timer = api_timer_create(10, true, false, timer1_update); |
| 61 | + if (timer) |
| 62 | + api_timer_restart(timer, 10); |
| 63 | + else |
| 64 | + printf("Fail to create timer.\n"); |
| 65 | +} |
| 66 | + |
| 67 | +static void btn_event_cb(lv_obj_t *btn, lv_event_t event) |
| 68 | +{ |
| 69 | + if(event == LV_EVENT_RELEASED) { |
| 70 | + label_count1_value--; |
| 71 | + snprintf(label_count1_str, sizeof(label_count1_str), |
| 72 | + "%d", label_count1_value); |
| 73 | + lv_label_set_text(label_count1, label_count1_str); |
| 74 | + if (label_count1_value == 0) |
| 75 | + label_count1_value = 100; |
| 76 | + } |
| 77 | +} |
0 commit comments