Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate AWTK into iotjs

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to integrate AWTK into iotjs, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Integrate AWTK into iotjs

Iotjs is Samsung's open source javascript Internet of things development platform. It provides javascript applications with the ability to access hardware, network, file system and asynchronization. The function is similar to nodejs, but iotjs is much smaller in terms of code volume and memory requirements, so it is the first choice to develop iot device applications with javascript.

AWTK, full name Toolkit AnyWhere, is an open source GUI engine developed by ZLG, designed for embedded systems, WEB, various Mini Program, mobile phones and PC to create a general GUI engine, providing users with a powerful, efficient and reliable, easy-to-use GUI engine that can easily make cool effects.

It's not too difficult to extend iotjs in C, but it's troublesome to integrate GUI into iotjs. The main reason is that iotjs has a main cycle (main loop), GUI also has a main cycle (main loop), the two are different, can only choose one. Here AWTK is an example of how to integrate GUI into iotjs, which is also of reference value for integrating GUI into nodejs.

First, who gives priority to

When integrating AWTK into iotjs, I chose to focus on the main loop of iotjs. This is not the only correct solution, and the main reasons for making this choice are:

Let AWTK be the module of iotjs and maintain the way iotjs itself is developed.

Avoid modifying iotjs. Iotjs is a third-party module to me, and modifying the third-party module will increase the maintenance cost in the later stage.

We extract a loop of the AWTK main loop into a step, and then put it in the timer of iotjs, calling it every 16 milliseconds (maximum 60FPS), so that we can simulate the main loop of GUI.

Const char* s_step_script = "(function () {\ var awtkStepId = 0;\ function awtkStep () {\ if (! awtk_main_loop_step ()) {\ clearInterval (awtkStepId);\ console.log (\" awtk quit\ ");\ awtkStepId = 0;\}\}\ awtkStepId = setInterval (awtkStep, 16);\} ()" JS_FUNCTION (wrap_awtk_main_loop_step) {bool_t ret = FALSE; main_loop_t* loop = main_loop (); if (loop! = NULL) {main_loop_step (loop); ret =! (loop- > app_quited); if (loop- > app_quited) {tk_exit ();}} return jerry_create_number (ret);}

It is worth noting that this timer must be called in javascript. At first, I used libuv native timer to implement, the code is simple and efficient, but when the application exits, iotjs will crash, because iotjs thinks that all timers are in javascript, while the timer in javascripte is packaged on the basis of native timer, and does some extra release work when the application exits.

II. Modal dialog box

The modal dialog box calls the run of the main loop again until the dialog box closes. At this point, you need to call the main loop of iotjs in the main loop of GUI. But the loop of iotjs is wrapped on the basis of the main loop of libuv, and it is not suitable for external calls, so I imitated iotjs to implement one:

Static ret_t main_loop_iotjs_run (main_loop_t* loop) {ret_t ret = RET_OK; iotjs_environment_t* env = iotjs_environment_get (); if (! iotjs_environment_is_exiting (env)) {bool more = 0; do {more = uv_run (iotjs_environment_loop (env), UV_RUN_ONCE); more | = iotjs_process_next_tick () Jerry_value_t ret_val = jerry_run_all_enqueued_jobs (); if (jerry_value_is_error (ret_val)) {ret_val = jerry_get_value_from_error (ret_val, true); iotjs_uncaught_exception (ret_val); jerry_release_value (ret_val) } if (more = = false) {more = uv_loop_alive (iotjs_environment_loop (env));} if (! (loop- > running)) {break;} while (more & &! iotjs_environment_is_exiting (env));} return ret

! (loop- > running) indicates that the modal dialog box has been exited and can be called back to the previous main loop.

3. GUI needs to provide the binding of Jerryscript

To integrate GUI into iotjs,GUI, you must bind all the external AIP to jerryscript. This is an easy task for AWTK. AWTK generates IDL by extracting comments, and then generates bindings in various languages based on IDL, which makes binding easier and reduces later maintenance costs.

The AWTK-JS project provides binding to jerryscript, and we compile it into a library that can be used in AWTK-iotjs.

Static int gui_app_start (int32_t lcd_w, int32_t lcd_h) {tk_init (lcd_w, lcd_h, APP_SIMULATOR, NULL, APP_ASSETS_ROOT); assets_init (); awtk_js_init (); main_loop ()-> running = TRUE; awtk_jerryscript_eval (AWTK_JS_FILE); tk_main_loop_run (); return 0;}

When you start GUI, call awtk_js_init to initialize the jerryscript binding of AWTK, and call awtk_jerryscript_eval to preload the awtk.js file.

The above is how to integrate AWTK into iotjs. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report