main.c 2.3 KB

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